-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_ann_with_kfold.py
125 lines (82 loc) · 2.87 KB
/
run_ann_with_kfold.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# import keras
# from keras.models import Sequential
# from keras import layers
# import pandas
# import numpy
# from sklearn.model
# dataset = pandas.read_csv("dataset.csv")
# dataset = dataset.sample(frac=1)
# target = dataset.iloc[:,-1].values
# data = dataset.iloc[:,:-1].values
# data = data/255.0
# machine = Sequential()
# machine.add(layers.Dense(512,
# activation="sigmoid",
# input_shape=(data.shape[1],)
# ))
# machine.add(layers.Dense(128,
# activation="sigmoid"))
# machine.add(layers.Dense(64,
# activation="sigmoid"))
# machine.add(layers.Dense(10,
# activation="softmax"))
# machine.compile(optimizer="sgd",
# loss="sparse_categorical_crossentropy",
# metrics=['accuracy'])
# machine.fit(data_training, target_training, epochs=30, batch_size=64)
# machine.fit(data, target, epochs=90, batch_size=64)
# new_target = numpy.argmax(machine.predict(new_data), axis = -1)
# #Simulating real world with new data
# new_data = pandas.read_csv("new_data.csv")
# filename_list = new_data.iloc[:,-1].values
# new_data = new_data.iloc[:,:-1].values
# new_data = new_data/255.0
# prediction = numpy.argmax(machine.predict(new_data), axis = -1)
# result = pandas.DataFrame()
# result['filename'] = filename_list
# result['prediction'] = predict
# print(results)
import keras
from keras.models import Sequential
from keras import layers
import pandas
import numpy
from sklearn.model_selection import KFold
from sklearn import metrics
dataset = pandas.read_csv("dataset.csv")
dataset = dataset.sample(frac=1)
target = dataset.iloc[:,-1].values
data = dataset.iloc[:,:-1].values
data = data/255.0
split_number = 4
kfold_object = KFold(n_splits=split_number)
kfold_object.get_n_splits(data)
results_accuracy = []
results_confusion_matrix = []
for training_index, test_index in kfold_object.split(data):
data_training = data[training_index]
target_training = target[training_index]
data_test = data[test_index]
target_test = target[test_index]
machine = Sequential()
machine.add(layers.Dense(512,
activation="relu",
input_shape=(data.shape[1],)
))
machine.add(layers.Dense(256,
activation="relu"))
machine.add(layers.Dense(128,
activation="relu"))
machine.add(layers.Dense(64,
activation="relu"))
machine.add(layers.Dense(10, activation="softmax"))
machine.compile(optimizer="sgd",
loss="sparse_categorical_crossentropy",
metrics=['accuracy'])
machine.fit(data_training, target_training, epochs=30, batch_size=64)
new_target = numpy.argmax(machine.predict(data_test), axis=-1)
results_accuracy.append(metrics.accuracy_score(target_test, new_target))
results_confusion_matrix.append(metrics.confusion_matrix(target_test, new_target))
print(results_accuracy)
for i in results_confusion_matrix:
print(i)