TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun · 77.8K subscribers
Post #3966 6.24K
Data Science Project Series Part 6: Sentiment Analysis using NLP ✅

Project Goal
Classify text as positive or negative.

Business Value
• Track customer feedback
• Monitor brand sentiment
• Automate review analysis
• High NLP interview relevance

Dataset
Movie reviews or product reviews.
Typical columns:
• review
• sentiment
Target: sentiment (1 positive, 0 negative)

Tech Stack
• Python
• Pandas
• NumPy
• NLTK
• Scikit-learn

Step 1. Import libraries

import pandas as pd
import numpy as np
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
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 classification_report, confusion_matrix, accuracy_score

nltk.download('stopwords')


Step 2. Load data

df = pd.read_csv("sentiment.csv")
df.head()


Example review: "The movie was amazing" sentiment: 1

Step 3. Basic checks

df.shape
df['sentiment'].value_counts()


Step 4. Text cleaning

stemmer = PorterStemmer()
stop_words = set(stopwords.words('english'))

def clean_text(text):
text = text.lower()
text = re.sub('[^a-z]', ' ', text)
words = text.split()
words = [stemmer.stem(w) for w in words if w not in stop_words]
return ' '.join(words)

df['clean_review'] = df['review'].apply(clean_text)


Step 5. Train test split

X = df['clean_review']
y = df['sentiment']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)


Step 6. Text vectorization TF IDF
tfidf = TfidfVectorizer(max_features=5000)
X_train_tfidf = tfidf.fit_transform(X_train)
X_test_tfidf = tfidf.transform(X_test)


Why TF IDF
• Reduces common word weight
• Keeps meaningful words

Step 7. Model building

model = LogisticRegression(max_iter=1000)
model.fit(X_train_tfidf, y_train)


Step 8. Predictions

y_pred = model.predict(X_test_tfidf)


Step 9. Evaluation

accuracy_score(y_test, y_pred)
confusion_matrix(y_test, y_pred)
print(classification_report(y_test, y_pred))


Typical results
• Accuracy 85 to 90 percent
• Precision strong on positive reviews
• Neutral text harder to classify

Step 10. Test on custom text
sample = ["The product quality is terrible"]
sample_clean = [clean_text(sample[0])]
sample_vec = tfidf.transform(sample_clean)
model.predict(sample_vec)


Output: 0 negative

Common interview questions

• Why TF IDF over CountVectorizer
• How stopwords affect meaning
• Why Logistic Regression works well

Improvements
• Use n grams
• Try Naive Bayes
• Use LSTM or Transformers

Resume bullet example

• Built sentiment analysis model using TF IDF and Logistic Regression
• Achieved 88 percent accuracy on review data
• Automated text preprocessing pipeline

Mini task for you
• Add bigrams
• Compare Naive Bayes
• Plot ROC curve

Double Tap ♥️ For More
  • ❤ 13
More from @datasciencefun
  1. Oct 17, 2026𝗜𝗕𝗠 𝗙𝗥𝗘𝗘 𝗧𝗘𝗖𝗛 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝟮𝟬𝟮𝟲! 🎓 Explore IBM Cognitive Class and build…
  2. Oct 16, 2026𝗗𝗮𝘁𝗮 𝘀𝗰𝗶𝗲𝗻𝗰𝗲 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗶𝗼𝗻 𝗪𝗶𝘁𝗵 𝗔𝗜😍 Future Ready Data Scien…
  3. Oct 15, 2026Post #4665
  4. Oct 15, 2026Post #4664
  5. Oct 15, 2026Post #4663
  6. Oct 15, 2026Post #4662
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →