-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaive_Bayes.py
More file actions
19 lines (18 loc) · 808 Bytes
/
Copy pathNaive_Bayes.py
File metadata and controls
19 lines (18 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from sklearn.datasets import fetch_20newsgroups
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report
news=fetch_20newsgroups(subset='all')
print(len(news.data))
print(news.data[0])
X_train, X_test, y_train, y_test=train_test_split(news.data, news.target, test_size=0.25,random_state=33)
vec=CountVectorizer()
X_train=vec.fit_transform(X_train)
X_test=vec.transform(X_test)
mnb=MultinomialNB()
mnb.fit(X_train,y_train)
y_predict=mnb.predict(X_test)
print('The Accuracy of Naive Bayes Classifier is',mnb.score(X_test,y_test))
print(classification_report(y_test,y_predict,target_names=news.target_names))