-
Notifications
You must be signed in to change notification settings - Fork 1
/
naive_bayes.py
executable file
·54 lines (46 loc) · 1.73 KB
/
naive_bayes.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
import inline as inline
import matplotlib
import numpy as np
import pandas as pd
import re
import nltk
import matplotlib.pyplot as plt
import Sastrawi.Stemmer
import string
from nltk.tokenize import word_tokenize
from string import digits
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
from preprocessing import preprocess, extractTFIDF, extractBOW, extractNGram
sundanese = pd.read_csv('newdataset.csv')
fitur = sundanese.iloc[:,1].values
labels = sundanese.iloc[:,0].values
# fitur_ekstraksi = preprocess(fitur)
# fitur_ekstraksi = extractBOW(fitur_ekstraksi)
fitur_ekstraksi = extractNGram([1,2])
# print(fitur_ekstraksi.toarray)
# print(fitur_ekstraksi[:1])
#
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(fitur_ekstraksi, labels, train_size=0.8, random_state=0)
from sklearn.naive_bayes import GaussianNB
# print(X_train)
klasifier = GaussianNB()
klasifier.fit(X_train.toarray(), y_train)
hasil_prediksi = klasifier.predict(X_test.toarray())
# print(*hasil_prediksi[:5])
# print('\n')
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
print(confusion_matrix(y_test,hasil_prediksi))
print(classification_report(y_test,hasil_prediksi))
print(accuracy_score(y_test, hasil_prediksi))
# from sklearn.metrics import plot_confusion_matrix
# titles_options = [("Confusion matrix", None)]
# for title, normalize in titles_options:
# disp = plot_confusion_matrix(klasifier, X_test, y_test,
# display_labels=labels,
# cmap=plt.cm.Blues,
# normalize=normalize,
# values_format='g')
# disp.ax_.set_title(title)
#
# plt.show()