-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaive_bayes.py
45 lines (33 loc) · 1.43 KB
/
naive_bayes.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, classification_report
# Load the dataset
df = pd.read_csv("new2_clean_noneutral.csv")
# Check the first few rows to ensure the data is correct
print(df.head())
# Assuming your dataset has columns 'cleaned_text' for the text and 'Sentiment' for labels
# Convert sentiment labels to numeric values (positive=1, negative=0)
df['Sentiment'] = df['Sentiment'].map({'positive': 1, 'negative': 0})
# Define features and target
X = df['cleaned_text']
y = df['Sentiment']
# Split data into training and testing sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize TF-IDF vectorizer
vectorizer = TfidfVectorizer(max_features=5000)
# Fit and transform the training data
X_train_tfidf = vectorizer.fit_transform(X_train)
# Transform the test data
X_test_tfidf = vectorizer.transform(X_test)
# Initialize and train Multinomial Naive Bayes model
model = MultinomialNB()
# Train the model
model.fit(X_train_tfidf, y_train)
# Make predictions on the test set
y_pred = model.predict(X_test_tfidf)
# Evaluate the model's performance
print(f"Accuracy: {accuracy_score(y_test, y_pred):.4f}")
print("Classification Report:")
print(classification_report(y_test, y_pred))