Python中基于网格搜索算法优化的深度学习模型分析糖尿病数据

### 安装

`\$ pip install numpy pandas tensorflow keras scikit-learn`

### 什么是网格搜索？

1. 隐藏层数[2，4]
2. 每层中的神经元数量[5，10]
3. 神经元数[10，50]

### 网格搜索实施

`from sklearn.model_selection import GridSearchCV, KFoldfrom keras.models import Sequentialfrom keras.layers import Dense, Dropoutfrom keras.wrappers.scikit_learn import KerasClassifierfrom keras.optimizers import Adamimport sysimport pandas as pdimport numpy as np`

`df = pd.read_csv(data_path, names=columns)`

`df.head()`

`for col in columns:    df[col].replace(0, np.NaN, inplace=True)df.dropna(inplace=True) # Drop all rows with missing values`

`# Transform and display the training dataX_standardized = scaler.transform(X)`

`def create_model(learn_rate, dropout_rate):    # Create model    model = Sequential()    model.add(Dense(8, input_dim=8, kernel_initializer='normal', activatinotallow='relu'))    model.add(Dropout(dropout_rate))    model.add(Dense(4, input_dim=8, kernel_initializer='normal', activatinotallow='relu'))    model.add(Dropout(dropout_rate))    model.add(Dense(1, activatinotallow='sigmoid'))    # Compile the model    adam = Adam(lr=learn_rate)    model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])    return model`

`model = create_model(learn_rate, dropout_rate)`

`Epoch 1/1130/130 [==============================] - 0s 2ms/step - loss: 0.6934 - accuracy: 0.6000`

`# Create the modelmodel = KerasClassifier(build_fn=create_model, verbose=1)`

`# Build and fit the GridSearchCVgrid = GridSearchCV(estimator=model, param_grid=param_grid,                    cv=KFold(random_state=seed), verbose=10)`

`Best: 0.7959183612648322, using {'batch_size': 10, 'dropout_rate': 0.2, 'epochs': 10, 'learn_rate': 0.02}`