【发布时间】:2018-07-21 16:55:05
【问题描述】:
我的程序流程分为两个阶段。
我正在使用 Sklearn ExtraTreesClassifier 和 SelectFromModel 方法来选择最重要的功能。这里需要注意的是,ExtraTreesClassifier 将许多参数作为输入,如n_estimators 等用于分类,并最终通过SelectFromModel 为n_estimators 的不同值提供不同的重要特征集。这意味着我可以优化n_estimators 以获得最佳功能。
在第二阶段,我正在根据第一阶段选择的特征训练我的 NN keras 模型。我使用 AUROC 作为网格搜索的分数,但这个 AUROC 是使用基于 Keras 的神经网络计算的。我想在我的ExtraTreesClassifier 中使用n_estimators 的网格搜索来优化keras 神经网络的AUROC。我知道我必须使用 Pipline,但我对同时实现两者感到困惑。我不知道在我的代码中将 Pipeline 放在哪里。我收到一条错误消息,上面写着TypeError: estimator should be an estimator implementing 'fit' method, <function fs at 0x0000023A12974598> was passed
#################################################################################
I concatenate the CV set and the train set so that I may select the most important features
in both CV and Train together.
##############################################################################
frames11 = [train_x_upsampled, cross_val_x_upsampled]
train_cv_x = pd.concat(frames11)
frames22 = [train_y_upsampled, cross_val_y_upsampled]
train_cv_y = pd.concat(frames22)
def fs(n_estimators):
m = ExtraTreesClassifier(n_estimators = tree_number)
m.fit(train_cv_x,train_cv_y)
sel = SelectFromModel(m, prefit=True)
##################################################
The code below is to get the names of the selected important features
###################################################
feature_idx = sel.get_support()
feature_name = train_cv_x.columns[feature_idx]
feature_name =pd.DataFrame(feature_name)
X_new = sel.transform(train_cv_x)
X_new =pd.DataFrame(X_new)
######################################################################
So Now the important features selected are in the data-frame X_new. In
code below, I am again dividing the data into train and CV but this time
only with the important features selected.
####################################################################
train_selected_x = X_new.iloc[0:train_x_upsampled.shape[0], :]
cv_selected_x = X_new.iloc[train_x_upsampled.shape[0]:train_x_upsampled.shape[0]+cross_val_x_upsampled.shape[0], :]
train_selected_y = train_cv_y.iloc[0:train_x_upsampled.shape[0], :]
cv_selected_y = train_cv_y.iloc[train_x_upsampled.shape[0]:train_x_upsampled.shape[0]+cross_val_x_upsampled.shape[0], :]
train_selected_x=train_selected_x.values
cv_selected_x=cv_selected_x.values
train_selected_y=train_selected_y.values
cv_selected_y=cv_selected_y.values
##############################################################
Now with this new data which only contains the important features,
I am training a neural network as below.
#########################################################
def create_model():
n_x_new=train_selected_x.shape[1]
model = Sequential()
model.add(Dense(n_x_new, input_dim=n_x_new, kernel_initializer='glorot_normal', activation='relu'))
model.add(Dense(10, kernel_initializer='glorot_normal', activation='relu'))
model.add(Dropout(0.8))
model.add(Dense(1, kernel_initializer='glorot_normal', activation='sigmoid'))
optimizer = keras.optimizers.Adam(lr=0.001)
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
seed = 7
np.random.seed(seed)
model = KerasClassifier(build_fn=create_model, epochs=20, batch_size=400, verbose=0)
n_estimators=[10,20,30]
param_grid = dict(n_estimators=n_estimators)
grid = GridSearchCV(estimator=fs, param_grid=param_grid,scoring='roc_auc',cv = PredefinedSplit(test_fold=my_test_fold), n_jobs=1)
grid_result = grid.fit(np.concatenate((train_selected_x, cv_selected_x), axis=0), np.concatenate((train_selected_y, cv_selected_y), axis=0))
【问题讨论】:
-
我发现我可以使用来自 sklearn 的
BaseEstimator构建自己的自定义估算器。我不知道如何将我的两个阶段都包含在一个自定义估算器中。或者可能有一种方法可以制作 custum keras 模型或包装器,其中将包括我使用基于树的方法进行重要特征选择的第 1 阶段。
标签: scikit-learn neural-network keras grid-search