-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake_news_classifier.py
169 lines (110 loc) · 3.45 KB
/
fake_news_classifier.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
"""Fake news classifier
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/11_HwdJSMsCTCAZ1-q7GmhBxJk_O9Qqb7
"""
from google.colab import files
files.upload()
# Let's make sure the kaggle.json file is present.
!ls -lha kaggle.json
!pip install -q kaggle
# The Kaggle API client expects this file to be in ~/.kaggle,
# so move it there.
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
# This permissions change avoids a warning on Kaggle tool startup.
!chmod 600 ~/.kaggle/kaggle.json
!pip install kaggle==1.5.6
!kaggle competitions download -c fake-news
!unzip fake-news.zip -d FakeNews
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
df = pd.read_csv('/content/FakeNews/train.csv')
df.head()
df.describe()
df.count()
df.isnull().sum()
df.shape
df = df.dropna()
df.shape
df = df.drop('id', axis=1)
df.head()
X = df.iloc[:, 0:3]
Y = df.iloc[: , 3]
X.head()
Y.head()
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
news = df.copy()
news.head()
news.reset_index(inplace=True)
news.head(10)
len(news)
news['title'][5]
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
import re
from nltk.stem.porter import PorterStemmer
corpus = []
ps = PorterStemmer()
for i in range(0, len(news)):
check = re.sub('[^a-zA-Z]', ' ', news['title'][i])
check = check.lower()
check = check.split()
check = [ps.stem(word) for word in check if not word in stopwords.words('english')]
check = ' '.join(check)
corpus.append(check)
corpus[1]
from sklearn.feature_extraction.text import CountVectorizer
countvec = CountVectorizer(max_features = 5000, ngram_range = (1,3))
X = countvec.fit_transform(corpus).toarray()
X.shape
y = news.iloc[: , 4]
y.shape
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30, random_state = 3)
countvec.get_params()
from sklearn.naive_bayes import MultinomialNB
from sklearn import metrics
clf = MultinomialNB()
X_train.shape, y_train.shape
X_test.shape, y_test.shape
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))
score = metrics.accuracy_score(y_test, y_pred)
score
cm = metrics.confusion_matrix(y_test, y_pred)
cm
"""# **TF-IDF**"""
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer(max_features = 5000, ngram_range = (1,3))
X = tfidf.fit_transform(corpus).toarray()
X.shape
y = news.iloc[:, 4]
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 3)
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB()
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
print(classification_report(y_test, pred))
score = metrics.accuracy_score(y_test, pred)
score
cm = metrics.confusion_matrix(y_test, pred)
cm
"""# **Hyperparameter tuning**"""
import numpy as np
clf = MultinomialNB(alpha=0.1)
previous_score=0
for alpha in np.arange(0,1,0.1):
sub_classifier=MultinomialNB(alpha=alpha)
sub_classifier.fit(X_train,y_train)
y_pred=sub_classifier.predict(X_test)
score = metrics.accuracy_score(y_test, y_pred)
if score>previous_score:
clf = sub_classifier
print("Alpha: {}, Score : {}".format(alpha,score))