如何在sklearn中获取随机森林的决策函数

我使用以下代码通过gridsearchcv来获取randomforest的最佳参数。

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上。为此,我做了如下操作,

pred = CV_rfc.decision_function(x_test)print(roc_auc_score(y_test, pred))

然而,decision_function似乎不支持randomforest,因为我得到了以下错误。

AttributeError: ‘RandomForestClassifier’ object has no attribute ‘decision_function’.

还有其他方法可以做到这一点吗?

如果需要,我很乐意提供更多细节。


回答:

如果您的目的是获取一个模型评分函数,以便该评分可以用于auc_roc_score,那么您可以使用predict_proba()

y_pred_proba = CV_rfc.predict_proba(x_test)print(roc_auc_score(y_test, y_pred_proba[:,1]))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注