问题描述
我正在使用以下代码,通过 gridsearchcv
获取 randomforest
的优化参数.
I am using the following code to get the optimised parameters for randomforest
using gridsearchcv
.
x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=0)
rfc = RandomForestClassifier(random_state=42, class_weight = 'balanced')
param_grid = {
'n_estimators': [200, 500],
'max_features': ['auto', 'sqrt', 'log2'],
'max_depth' : [4,5,6,7,8],
'criterion' :['gini', 'entropy']
}
k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)
CV_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 10, scoring = 'roc_auc')
CV_rfc.fit(x_train, y_train)
print(CV_rfc.best_params_)
print(CV_rfc.best_score_)
现在,我想将调整后的参数应用于 X_test
.为此,我做了以下事情,
Now, I want to apply the tuned parameters to X_test
. For that I did the following,
pred = CV_rfc.decision_function(x_test)
print(roc_auc_score(y_test, pred))
但是,由于出现以下错误, decision_function
似乎不支持 randomforest
.
However, decision_function
does not seem to support randomforest
as I got the following error.
还有其他方法吗?
如果需要,我很乐意提供更多详细信息.
I am happy to provide more details if needed.
推荐答案
如果您打算获得模型评分功能,以便可以对 auc_roc_score
使用评分,则可以进行 predict_proba()代码>
If your intention is to get a model scoring function so that the scoring can be used for auc_roc_score
, then you can go for predict_proba()
y_pred_proba = CV_rfc.predict_proba(x_test)
print(roc_auc_score(y_test, y_pred_proba[:,1]))
这篇关于如何在sklearn中的randomforest中获得决策函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!