3

Running an otherwise well-functioning code, I keep getting the following message in the console in PyCharm (view 'Response in console': 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)). Also, the warning is listed several times.

Looking here and there, I understand that a way to remove it is to change n_estimators from 10 to 100, thereby expanding 'trees in the forest'. Yet several attempts to do that failed.

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),"%")

Response in console:

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 %
Newbie
  • 61
  • 1
  • 1
  • 6

2 Answers2

10

In general, this warning is not a big problem, warnings are not always a issue in python.

Followed by this post, there are several options to supress all warnings (How to disable python warnings), this one supress all:

import warnings
warnings.filterwarnings("ignore")
PV8
  • 5,799
  • 7
  • 43
  • 87
  • You say so, but I am not sure I agree. If you noticed, I used several ignores and warning disables, but they were NOT the reason themselves for eliminating my issue. At the end of the day, code update was...as I showed…;o) – Newbie Sep 18 '19 at 06:57
2

I actually solved the problem, and did like this, i.e. placing the n_estimators parameter in the cross_val_rfc line - 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),"%")
Newbie
  • 61
  • 1
  • 1
  • 6