-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeras_code.py
107 lines (88 loc) · 3.44 KB
/
keras_code.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
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.utils import shuffle
import pickle
# path to the csv file
DATA_PATH = "diff.csv"
# processors
PROCESSORS = -1
# K folds
KFOLD = 10
# verbosity
VERBOSE = 2
# random sttate
RANDOM_STATE = 314159
# import data
pd_data = pd.read_csv(DATA_PATH)
pd_data = shuffle(pd_data, random_state=RANDOM_STATE)
# x and y data
pd_data_y = pd_data["Winner"]
pd_data_x = pd_data.loc[:, pd_data.columns != "Winner"]
# separate training and test data
pd_data_x_train, pd_data_x_test, pd_data_y_train, pd_data_y_test = train_test_split(pd_data_x, pd_data_y, test_size=.2,
random_state=RANDOM_STATE)
# Create the KerasClassifier
def create_model(optimizer='rmsprop', kernel_init='glorot_uniform', activation='sigmoid', layer=1, node=32, dropout=.5):
# create squential model
model = Sequential()
# add initial layer
model.add(Dense(node, input_dim=DATA_SHAPE, kernel_initializer=kernel_init, activation=activation))
model.add(Dropout(dropout))
# add numbers of intermediate layers
i = 1
for _layer in range(layer):
# print("ADDING LAYER : ", i)
model.add(Dense(node, kernel_initializer=kernel_init, activation=activation))
model.add(Dropout(dropout))
i = i + 1
# add the final layer
model.add(Dense(1, kernel_initializer=kernel_init, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
# evaluate model with standardized dataset
model = KerasClassifier(build_fn=create_model, epochs=100, verbose=VERBOSE) # verbose = 0 for nothing to show
# shape of the data. This defines all of the inputs
DATA_SHAPE = pd_data_x_train.shape[1]
DATA_LENGTH = pd_data_x_train.shape[0]
# grid search parameters
optimizers = ['rmsprop']
kernel_inits = ['uniform']
activations = ['sigmoid']
layers = [10]
nodes = [DATA_SHAPE]
dropouts = [0]
batches = [20]
epochs = [1000]
# define parameter grid
param_grid = dict(optimizer=optimizers, epochs=epochs, batch_size=batches, kernel_init=kernel_inits,
activation=activations, layer=layers, node=nodes, dropout=dropouts)
# define gridsearch
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=PROCESSORS, cv=KFOLD, verbose=VERBOSE)
# fit
grid_result = grid.fit(pd_data_x_train, pd_data_y_train)
# save trained keras classifier to file
keras_outfile = open("keras4_cv_results.pickle", "w+b")
pickle.dump(grid_result.cv_results_, keras_outfile)
keras_outfile.close()
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))
best_grid_result_score = accuracy_score(pd_data_y_test, grid_result.best_estimator_.predict(pd_data_x_test))
print()
print()
print("BEST PARAMETERS : ")
print(grid_result.best_params_)
print()
print()
print("BEST SCORE ON THE TEST DATA IS : ", best_grid_result_score)