Code
nlp
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
texts = [
"I love this movie", "great film", "awesome and fun",
"terrible acting", "boring and slow", "I hated every minute",
"wonderful story", "bad plot",
]
labels = ["pos", "pos", "pos", "neg", "neg", "neg", "pos", "neg"]
X_tr, X_te, y_tr, y_te = train_test_split(texts, labels, test_size=0.25,
random_state=42)
pipe = Pipeline([
("tfidf", TfidfVectorizer(ngram_range=(1, 2), min_df=1)),
("clf", LogisticRegression(C=1.0, max_iter=1000)),
])
pipe.fit(X_tr, y_tr)
pred = pipe.predict(X_te)
print(classification_report(y_te, pred))
# Predict new text
print(pipe.predict(["an amazing experience", "a dull waste of time"]))