-
Notifications
You must be signed in to change notification settings - Fork 0
/
turning.py
43 lines (38 loc) · 1.82 KB
/
turning.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
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
import pandas as pd
arq = pd.read_csv('cancer.csv')
arq['diagnosis'] = arq['diagnosis'].replace('M', 0)
arq['diagnosis'] = arq['diagnosis'].replace('B', 1)
previsores = arq.drop(['id', 'diagnosis', 'Unnamed: 32'], axis=1)
classe = arq['diagnosis']
def criarRede(optimizer, loos, kernel_initializer, activation, neurons):
classificador = Sequential()
classificador.add(Dense(units = neurons, activation = activation,
kernel_initializer = kernel_initializer, input_dim = 30))
classificador.add(Dropout(0.2))
classificador.add(Dense(units = neurons, activation = activation,
kernel_initializer = kernel_initializer))
classificador.add(Dropout(0.2))
classificador.add(Dense(units = 1, activation = 'sigmoid'))
classificador.compile(optimizer = optimizer, loss = loos,
metrics = ['binary_accuracy'])
return classificador
classificador = KerasClassifier(build_fn = criarRede)
parametros = {'batch_size': [10, 30],
'epochs': [50, 100],
'optimizer': ['adam', 'sgd'],
'loos': ['binary_crossentropy', 'hinge'],
'kernel_initializer': ['random_uniform', 'normal'],
'activation': ['relu', 'tanh'],
'neurons': [16, 8]}
grid_search = GridSearchCV(estimator = classificador,
param_grid = parametros,
scoring = 'accuracy',
cv = 5)
grid_search = grid_search.fit(previsores, classe)
melhores_parametros = grid_search.best_params_
melhor_precisao = grid_search.best_score_