我正在使用执行超参数调整,RandomForest如下所示GridSearchCV。
X = np.array(df[features]) #all features
y = np.array(df['gold_standard']) #labels
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
param_grid = {
'n_estimators': [200, 500],
'max_features': ['auto', 'sqrt', 'log2'],
'max_depth' : [4,5,6,7,8],
'criterion' :['gini', 'entropy']
}
CV_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 5)
CV_rfc.fit(x_train, y_train)
print(CV_rfc.best_params_)
Run Code Online (Sandbox Code Playgroud)
我得到的结果如下。
{'criterion': 'gini', 'max_depth': 6, 'max_features': 'auto', 'n_estimators': 200}
Run Code Online (Sandbox Code Playgroud)
之后,我将调整后的参数重新应用x_test如下。
rfc=RandomForestClassifier(random_state=42, criterion ='gini', max_depth= 6, max_features = 'auto', n_estimators = 200, class_weight = 'balanced')
rfc.fit(x_train, y_train)
pred=rfc.predict(x_test)
print(precision_recall_fscore_support(y_test,pred))
print(roc_auc_score(y_test,pred)) …Run Code Online (Sandbox Code Playgroud)