-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
executable file
·67 lines (55 loc) · 2.15 KB
/
example.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
# from https://chrisalbon.com/machine_learning/naive_bayes/multinomial_naive_bayes_classifier/
# Load libraries
import numpy as np
from sklearn.naive_bayes import MultinomialNB, BernoulliNB
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report
# Create text
text_data = np.array(['I love Brazil. Brazil!',
'Brazil is best',
'I like Germany more, Germany beats both',
'I like Italy, because Italy is beautiful',
'I am from Germany, so I like Germany more'])
# Create bag of words
count = CountVectorizer()
bag_of_words = count.fit_transform(text_data)
# Create feature matrix
X = bag_of_words
# Create target vector
y = np.array([0,0,1,0,1])
X_train = X[:3]
X_test = X[3:]
y_train = y[:3]
y_test = y[3:]
# Create new unseen test instances
test1 = count.transform(['I like Italy, because Italy is beautiful'])
test2 = count.transform(['I am from Germany, so I like Germany more'])
print("----bnb")
clf = BernoulliNB()
model = clf.fit(X_train, y_train)
print(model.predict(test1))
print(model.predict(test2))
predicted_y = model.predict(X_test)
print(y_test, predicted_y)
print(model.predict_proba(X_test))
print(accuracy_score(y_test, predicted_y))
print(precision_score(y_test, predicted_y))
print(recall_score(y_test, predicted_y))
print(f1_score(y_test, predicted_y, average='micro'))
print(f1_score(y_test, predicted_y, average='macro'))
print(classification_report(y_test, predicted_y))
# Train model
print("----mnb")
clf = MultinomialNB()
model = clf.fit(X_train, y_train)
print(model.predict(test1))
print(model.predict(test2))
predicted_y = model.predict(X_test)
print(y_test, predicted_y)
print(model.predict_proba(X_test))
print(accuracy_score(y_test, predicted_y))
print(precision_score(y_test, predicted_y))
print(recall_score(y_test, predicted_y))
print(f1_score(y_test, predicted_y, average='micro'))
print(f1_score(y_test, predicted_y, average='macro'))
print(classification_report(y_test, predicted_y))