-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake news.py
66 lines (42 loc) · 1.39 KB
/
fake news.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
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 29 18:45:07 2020
@author: shiva dumnawar
"""
import pandas as pd
import nltk
import re
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
df= pd.read_csv('fake news.csv')
df.info()
df.isnull().sum()
df= df.dropna()
df.shape
df.reset_index(inplace= True)
ps= PorterStemmer()
message= df['text'][:2000]
# This is a huge dataset so taking text feature with 2000 rows
corpus= []
for i in range(len(message)):
review= re.sub('[^a-zA-Z]', ' ', message[i])
review= review.lower()
review= review.split()
review= [ps.stem(word) for word in review if word not in stopwords.words('english')]
review= ' '.join(review)
corpus.append(review)
# Bag of words
from sklearn.feature_extraction.text import CountVectorizer
cv= CountVectorizer(max_features=5000)
X= cv.fit_transform(corpus).toarray()
y= df['label'][:2000].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test= train_test_split(X, y, test_size=0.2, random_state=75)
from sklearn.naive_bayes import MultinomialNB
clf= MultinomialNB().fit(X_train, y_train)
y_pred= clf.predict(X_test)
from sklearn.metrics import confusion_matrix, accuracy_score
cm= confusion_matrix(y_test, y_pred)
print(cm)
acc_score= accuracy_score(y_test, y_pred)
print(acc_score)