-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython sentiment_analysis.py
More file actions
31 lines (24 loc) · 999 Bytes
/
Copy pathpython sentiment_analysis.py
File metadata and controls
31 lines (24 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import pandas as pd
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
# Load the dataset
df = pd.read_csv('IMDB Dataset.csv')
# Convert labels to numbers
df['sentiment'] = df['sentiment'].map({'positive': 1, 'negative': 0})
# Split into features and labels
X = df['review']
y = df['sentiment']
# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Convert text to numbers using TF-IDF
vectorizer = TfidfVectorizer(max_features=10000, stop_words='english')
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)
# Train the model
model = LogisticRegression(max_iter=1000)
model.fit(X_train_vec, y_train)
# Test the model
predictions = model.predict(X_test_vec)
print(f"Accuracy: {accuracy_score(y_test, predictions) * 100:.2f}%")