-
Notifications
You must be signed in to change notification settings - Fork 2
/
naive_bayes_pipeline.py
41 lines (29 loc) · 1.19 KB
/
naive_bayes_pipeline.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
from sklearn.datasets import load_files
print('Loading Dataset ...');
# load all data from files
twenty_all = load_files("./remail",
categories=None, load_content=True, shuffle=True, encoding="latin1", random_state=42, decode_error='strict')
print('dataset loaded');
# split the train and test data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(twenty_all.data, twenty_all.target, test_size=0.2)
print('data processing . . .');
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
print('training started');
# feed the train dataset into naive_bayes model
text_clf = Pipeline([('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', MultinomialNB(alpha=0.0001)),
])
text_clf = text_clf.fit(X_train, y_train)
print('training finished');
print('testing trained model');
# validation of the trained model
import numpy as np
docs_test = X_test
predicted = text_clf.predict(docs_test)
print('Test Result:');
print(np.mean(predicted == y_test))