如何删除关于 n_estimators 的未来警告将从 0.20 版本中的 10 更改为 0.22 中的 100?

Posted

技术标签:

【中文标题】如何删除关于 n_estimators 的未来警告将从 0.20 版本中的 10 更改为 0.22 中的 100?【英文标题】:How do I remove future warning regarding n_estimators will change from 10 in version 0.20 to 100 in 0.22? 【发布时间】:2020-01-18 15:33:37 【问题描述】:

运行其他功能良好的代码,我在 PyCharm 的控制台中不断收到以下消息(查看“控制台中的响应”:FutureWarning:n_estimators 的默认值将从 0.20 版本中的 10 更改为 0.22 中的 100。” 0.20 版中的 10 到 0.22 版中的 100。”,FutureWarning))。此外,该警告被多次列出。

到处看看,我知道删除它的一种方法是将 n_estimators 从 10 更改为 100,从而扩大“森林中的树木”。然而,几次尝试都失败了。

from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100) <== added this line; not sure, if ok!
random_forest_classifier = RandomForestClassifier()
random_forest_classifier.fit(X_train, y_train)
y_pred_rfc = random_forest_classifier.predict(X_test)

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='ignore', category=DeprecationWarning)
warnings.simplefilter(action='ignore', category=RuntimeWarning)

print("\nConfusion Matrix")
cm_random_forest_classifier = confusion_matrix(y_test,y_pred_rfc)
print(cm_random_forest_classifier, end="\n\n")

print("\nCalculating the accuracy from the confusion matrix for Random Forest")
numerator = cm_random_forest_classifier[0][0] + cm_random_forest_classifier[1][1]
denominator = sum(cm_random_forest_classifier[0]) + sum(cm_random_forest_classifier[1])
acc_rfc = (numerator/denominator) * 100
print("Accuracy of Random Forest: ", round(acc_rfc,2),"%")

cross_val_rfc = cross_val_score(estimator=RandomForestClassifier(), X=X_train, y=y_train, cv=10, n_jobs=-1)
print("Cross Validation Accuracy of Random Forest: ",round(cross_val_rfc.mean() * 100 , 2),"%")

控制台响应:

Confusion Matrix
[[1722   51]
 [ 166   48]]

Calculating the accuracy from the confusion matrix for Random Forest
Accuracy of Random Forest:  89.08 %
C:\Users\....\Programs\Python\Python37-64\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22. "10 in version 0.20 to 100 in 0.22.", FutureWarning)
Cross Validation Accuracy of Random Forest:  89.71 %

【问题讨论】:

【参考方案1】:

一般来说,这个警告不是什么大问题,警告在python中并不总是一个问题。

在这篇文章之后,有几个选项可以抑制所有警告(How to disable python warnings),这个抑制所有:

import warnings
warnings.filterwarnings("ignore")

【讨论】:

你这么说,但我不确定我是否同意。如果您注意到,我使用了几个忽略和警告禁用,但它们本身并不是消除我的问题的原因。在一天结束时,代码更新是......正如我所展示的......;o)【参考方案2】:

我实际上解决了这个问题,并且这样做了,即将 n_estimators 参数放在 cross_val_rfc 行 - HEUREKA !!!:

cross_val_rfc = cross_val_score(estimator=RandomForestClassifier(n_estimators=100), X=X_train, y=y_train, cv=10, n_jobs=-1)
print("Cross Validation Accuracy of Random Forest: ",round(cross_val_rfc.mean() * 100 , 2),"%")

【讨论】:

以上是关于如何删除关于 n_estimators 的未来警告将从 0.20 版本中的 10 更改为 0.22 中的 100?的主要内容,如果未能解决你的问题,请参考以下文章

Error:(16, 16) 警告: BASE64Decoder是内部专用 API, 可能会在未来发行版中删除

警告:XXXXX 是Sun的专用API,可能会在未来版本中删除

kubectl (node:7) [MONGODB DRIVER] 警告:当前的服务器发现和监控引擎已被弃用,将在未来版本中删除

如何安装 npm peerDependencies 以删除警告

maven编译错误,警告: BASE64Decoder是内部专用 API, 可能会在未来发行版中删除

如何解决未来的警告错误 [重复]