-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
22 lines (22 loc) · 1.09 KB
/
Copy pathtrain_model.py
File metadata and controls
22 lines (22 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import pandas as pd
import joblib
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
data = pd.read_csv("D:/Sentiment Analysis/data/processed_data.csv")
X = data["cleaned_text"]
y = data["sentiment"]
tfidf_vectorizer = TfidfVectorizer()
X_tfidf = tfidf_vectorizer.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X_tfidf, y, test_size=0.2, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"✅ Model Training Complete! Accuracy: {accuracy:.2f}\n")
print("Classification Report:\n", classification_report(y_test, y_pred))
joblib.dump(model, "D:/Sentiment Analysis/models/sentiment_model.pkl")
joblib.dump(tfidf_vectorizer, "D:/Sentiment Analysis/models/tfidf_vectorizer.pkl")
print("✅ Model saved as sentiment_model.pkl")
print("✅ TF-IDF Vectorizer saved as tfidf_vectorizer.pkl")