数据分析集训营-第五次任务(模型调优)


#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# Author: Jia ShiLin
'''
调参-参数搜索
'''
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
 
from sklearn.model_selection import cross_val_score, GridSearchCV
from sklearn.model_selection import StratifiedKFold
from keras.wrappers.scikit_learn import KerasClassifier
 
 
# 构建模型
def create_model(optimizer='adam', init='glorot_uniform'):
    model = Sequential()
    model.add(Dense(units=12, kernel_initializer=init, input_dim=8, activation='relu'))
    model.add(Dense(units=8, kernel_initializer=init, activation='relu'))
    model.add(Dense(units=1, kernel_initializer=init, activation='sigmoid'))
 
    model.compile(optimizer=optimizer, metrics=['accuracy'], loss='binary_crossentropy', )
 
    return model
 
 
seed = 7
np.random.seed(seed)
 
# data
dataset = np.loadtxt('pima-indians-diabetes.csv', delimiter=',')
# x,y
x = dataset[:, 0:8]
y = dataset[:, 8:]
 
# 创建模型 for scikit-learn
model = KerasClassifier(build_fn=create_model, verbose=0)
 
# 构建需要调参的参数
param_grid = {}
param_grid['optimizer'] = ['rmsprop', 'adam']
param_grid['init'] = ['glorot_unitform', 'normal', 'unitform']
param_grid['epochs'] = [50, 100, 150, 200]
param_grid['batch_size'] = [5, 10, 20]
 
# 调参
 
 
grid = GridSearchCV(estimator=model, param_grid=param_grid)
results = grid.fit(x, y)
 
# 输出
print('best:%f using %s' % (results.best_score_, results.best_params_))
means = results.cv_results_['mean_test_score']
stds = results.cv_results_['std_test_score']
params = results.cv_results_['params']
 
for mean, std, param in zip(means, stds, params):
	print('%f(%f) with: %r' % (mean, std, param))

你可能感兴趣的:(数据挖掘实践)