This program trains a simple model to tell if text is positive or negative. It shows accuracy and compares predictions to actual labels.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
# Sample text data and labels
texts = [
"I love this product",
"This is the worst thing ever",
"Absolutely fantastic experience",
"I hate it",
"Not good at all",
"I am very happy",
"Terrible service",
"Best purchase I've made",
"Awful, will not buy again",
"Really pleased with this"
]
labels = [1, 0, 1, 0, 0, 1, 0, 1, 0, 1] # 1=positive, 0=negative
# Convert text to numeric features
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)
# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.3, random_state=1)
# Train the Naive Bayes model
model = MultinomialNB()
model.fit(X_train, y_train)
# Predict on test data
predictions = model.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)
print(f"Accuracy: {accuracy:.2f}")
print("Test predictions:", predictions)
print("Actual labels: ", y_test)