-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
model 2 Naive bayes.py
97 lines (74 loc) · 2.32 KB
/
model 2 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# importing required libraries
import numpy as np
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
from keras.utils import np_utils
# Loading dataset
data = np.loadtxt("data.txt")
# Splitting dataset
X_train = data[:4646,:12]
Y_train = data[:4646,12:13]
X_test = data[4646:6936,:12]
Y_test = data[4646:6936,12:13]
len_y_train = len(Y_train)
len_y_test = len(Y_test)
print('Shape of training data :',X_train.shape)
print('Shape of testing data :',Y_test.shape)
# Data Preprocessing
for i in range(0,len_y_train):
if(Y_train[i]==1000):
Y_train[i] = 3
Y_train[i] = int(Y_train[i])
elif(Y_train[i]==100):
Y_train[i] = 2
Y_train[i] = int(Y_train[i])
elif(Y_train[i]==10):
Y_train[i] = 1
Y_train[i] = int(Y_train[i])
elif(Y_train[i]==1):
Y_train[i] = 0
Y_train[i] = int(Y_train[i])
for i in range(0,len_y_test):
if(Y_test[i]==1000):
Y_test[i] = 3
Y_test[i] = int(Y_test[i])
elif(Y_test[i]==100):
Y_test[i] = 2
Y_test[i] = int(Y_test[i])
elif(Y_test[i]==10):
Y_test[i] = 1
Y_test[i] = int(Y_test[i])
elif(Y_test[i]==1):
Y_test[i] = 0
Y_test[i] = int(Y_test[i])
def res(x):
if(x==0):
return 'ThunderStorm'
elif(x==1):
return 'Rainy'
elif(x==2):
return 'Foggy'
else:
return 'Sunny'
model = GaussianNB()
# fit the model with the training data
model.fit(X_train,Y_train.ravel())
# predict the target on the train dataset
predict_train = model.predict(X_train)
print('Target on train data',predict_train)
# Accuray Score on train dataset
accuracy_train = accuracy_score(Y_train,predict_train)
print('accuracy_score on train dataset : ', accuracy_train*100)
# predict the target on the test dataset
predict_test = model.predict(X_test)
# print('Target on test data',[predict_test for predict_test in predict_test])
# Accuracy Score on test dataset
accuracy_test = accuracy_score(Y_test,predict_test)
print('accuracy_score on test dataset : ', accuracy_test*100)
# Load Prediction Data
pre_data = np.loadtxt('predict.txt').reshape(1, -1)
pre_data=pre_data.astype('int32')
# Print Input & Prediction
print('Input: ',pre_data)
print ("\n \t \t \t Weather would be",res(model.predict(pre_data)))
# This is Naive bayes network