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

New*_*bie 3 python-3.x random-forest scikit-learn

运行其他功能良好的代码,我不断在 PyCharm 的控制台中收到以下消息(查看“控制台中的响应”:FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22. "10 in version 0.22 中的 0.20 到 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),"%")
Run Code Online (Sandbox Code Playgroud)

控制台中的响应:

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 %
Run Code Online (Sandbox Code Playgroud)

PV8*_*PV8 12

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

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

import warnings
warnings.filterwarnings("ignore")
Run Code Online (Sandbox Code Playgroud)