-
Notifications
You must be signed in to change notification settings - Fork 12
/
NaiveBayes_Nursery_sklearn.py
74 lines (59 loc) · 2.55 KB
/
NaiveBayes_Nursery_sklearn.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
68
69
70
71
72
73
74
## Gaussian Naive Bayes Nursery Scikit Learn Version
#
# Author: David Lee
# Create Date: 2018/10/9
#
# Detail:
# Total Data = 12960
# Training Data : Testing Data = 7 : 3
import numpy as np
import pandas as pd # Read csv
from sklearn.naive_bayes import GaussianNB # Gaussian Naive Bayes
from sklearn.preprocessing import LabelEncoder # Transform 'string' into class number
# Because the fit function of GaussianNB only accept numeric input
from sklearn.model_selection import train_test_split # Split training and testing data
from sklearn import metrics # Evaluate model
def loadData(path):
inputData = pd.read_csv(path)
# Transform 'string' into class number
Labels = [
['usual', 'pretentious', 'great_pret'],
['proper', 'less_proper', 'improper', 'critical', 'very_crit'],
['complete', 'completed', 'incomplete', 'foster'],
['1', '2', '3', 'more'],
['convenient', 'less_conv', 'critical'],
['convenient', 'inconv'],
['nonprob', 'slightly_prob', 'problematic'],
['recommended', 'priority', 'not_recom'],
['not_recom', 'recommend', 'very_recom', 'priority', 'spec_prior']
]
le = LabelEncoder()
# Somehow use np.mat to deal with shape problem down below
dataTemp = np.mat(np.zeros((len(inputData), len(inputData.columns))))
for colIdx in range(len(inputData.columns)):
le.fit(Labels[colIdx])
dataTemp[:, colIdx] = np.mat(le.transform(inputData.iloc[:, colIdx])).T
num_data = np.array(dataTemp[:, :-1])
num_label = np.array(dataTemp[:, -1])
data_train, data_test, label_train, label_test = train_test_split(num_data, num_label, test_size=0.3, random_state=87)
return data_train, label_train, data_test, label_test
def trainDecisionTree(data_train, label_train):
gnb = GaussianNB()
gnb.fit(data_train, label_train)
return gnb
def testAccuracy(data_test, label_test, gnb):
return gnb.score(data_test, label_test)
def evaluateModel(data_test, label_test, gnb):
print(metrics.classification_report(label_test, gnb.predict(data_test)))
print(metrics.confusion_matrix(label_test, gnb.predict(data_test)))
def main():
# Load Data
data_train, label_train, data_test, label_test = loadData('Datasets/nursery.csv')
# Train Model
GoussianNaiveBayes = trainDecisionTree(data_train, label_train)
# Test Accuracy
print('Accuracy:', float(testAccuracy(data_test, label_test, GoussianNaiveBayes)))
# Evaluate Model
evaluateModel(data_test, label_test, GoussianNaiveBayes)
if __name__ == '__main__':
main()