-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
29 lines (23 loc) · 991 Bytes
/
model.py
File metadata and controls
29 lines (23 loc) · 991 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
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.metrics import accuracy_score
import pickle
# Load dataset
df = pd.read_csv('fake_news_dataset.csv') # contains 'text' and 'label' columns
# Split data
x_train, x_test, y_train, y_test = train_test_split(df['text'], df['label'], test_size=0.2, random_state=42)
# TF-IDF Vectorizer
vectorizer = TfidfVectorizer(stop_words='english', max_df=0.7)
x_train_tfidf = vectorizer.fit_transform(x_train)
x_test_tfidf = vectorizer.transform(x_test)
# Train model
model = PassiveAggressiveClassifier(max_iter=50)
model.fit(x_train_tfidf, y_train)
# Accuracy
y_pred = model.predict(x_test_tfidf)
print(f"Model Accuracy: {accuracy_score(y_test, y_pred)*100:.2f}%")
# Save model and vectorizer
pickle.dump(model, open('model.pkl', 'wb'))
pickle.dump(vectorizer, open('vectorizer.pkl', 'wb'))