-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathmain.py
71 lines (54 loc) · 2.11 KB
/
main.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Import necessary libraries
import pandas as pd
import numpy as np
import re
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, confusion_matrix
# Load the datasets
fake_df = pd.read_csv('dataset/Fake.csv') # Path to your fake news file
true_df = pd.read_csv('dataset/True.csv') # Path to your true news file
# Add a label column: 1 for fake news and 0 for true news
fake_df['label'] = 1
true_df['label'] = 0
# Combine the two datasets
df = pd.concat([fake_df, true_df], ignore_index=True)
# Display the first few rows
print(df.head())
# Preprocess text data
def preprocess_text(text):
text = text.lower() # Lowercase
text = re.sub(r'[^a-zA-Z\s]', '', text) # Remove special characters
return text
# Apply preprocessing to the text column
df['text'] = df['text'].apply(preprocess_text)
# Define target and features
X = df['text']
y = df['label']
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize TF-IDF Vectorizer
tfidf_vectorizer = TfidfVectorizer(max_features=5000) # Limit features for simplicity
# Fit and transform the data
X_train_tfidf = tfidf_vectorizer.fit_transform(X_train)
X_test_tfidf = tfidf_vectorizer.transform(X_test)
# Initialize and train the model
model = MultinomialNB()
model.fit(X_train_tfidf, y_train)
# Make predictions
y_pred = model.predict(X_test_tfidf)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')
# Classification report
print("Classification Report:\n", classification_report(y_test, y_pred))
# Confusion Matrix
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
def model_details():
print("Model: Naive Bayes Classifier")
print(f"Accuracy: {accuracy:.2f}")
print("Classification Report:\n", classification_report(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
# Call the model details function
model_details()