-
Notifications
You must be signed in to change notification settings - Fork 0
/
NaiveBayes-FakeorRealNews.py
33 lines (26 loc) · 1.14 KB
/
NaiveBayes-FakeorRealNews.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
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
# Load your dataset
# The dataset should have two columns: 'text' for the news articles and 'label' for the corresponding labels
data = pd.read_csv('your_dataset.csv')
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(data['text'], data['label'], test_size=0.2, random_state=42)
# Create a pipeline for preprocessing and classification
pipeline = Pipeline([
('tfidf', TfidfVectorizer(stop_words='english')),
('classifier', MultinomialNB())
])
# Train the Naive Bayes classifier
pipeline.fit(X_train, y_train)
# Make predictions on the test set
y_pred = pipeline.predict(X_test)
# Evaluate the classifier's performance
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred, target_names=['fake', 'real'])
print(f"Accuracy: {accuracy}")
print("Classification Report:")
print(report)