Ravindra BagaleCourses & study guides English Track your progress

53. Sentiment Analysis Project in Python

Data Science

53.2 Copy-Run TF-IDF + LogisticRegression

53.2 Copy-run TF-IDF + LogisticRegression

Jupyter मध्ये paste करून extend करता येईल असा spine.

हे का शिकायचं?

Why. Jupyter मध्ये paste करून extend करता येईल असा copy-run spine हवा.

हे काय आहे?

What. एक script: data → split → vectorise → fit classifier → predict → score.

कसं करायचं?

How.

Mini project copy-run

  1. docs आणि y create.
  2. Stratified train_test_split.
  3. TfidfVectorizer train वर fit.
  4. LogisticRegression fit; print accuracy on test.
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report

docs = [
 "fresh pedha on time delivery",
 "sour dahi and torn packing",
 "sweet pedha excellent taste",
 "late delivery cold chain fail",
 "good packing fresh dahi",
 "bad smell sour tray returned",
 "on time from nashik hub",
 "not good packing damaged box",
 "loved the pedha box",
 "refund please spoiled dahi",
 "fresh and neat packing",
 "worst delivery experience",
]
y = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]

X_train, X_test, y_train, y_test = train_test_split(
 docs, y, test_size=0.33, random_state=42, stratify=y
)

vec = TfidfVectorizer(ngram_range=(1, 2), stop_words="english")
Xtr = vec.fit_transform(X_train)
Xte = vec.transform(X_test)

clf = LogisticRegression(max_iter=200)
clf.fit(Xtr, y_train)
pred = clf.predict(Xte)
print("accuracy:", round(accuracy_score(y_test, pred), 3))
print(classification_report(y_test, pred, zero_division=0))

Output / जे दिसायला हवं. Accuracy print आणि छोटा classification report. Tiny data वर scores bounce — goal pipeline, leaderboard नाही.