I am following this tutorials: https://www.datacamp.com/community/tutorials/random-forests-classifier-python on using Scikit-learn with random forests. However, the current code only shows the test accuracy whereas I want to know the training accuracy as well since may dataset is very small.
The code to get the test accuracy is:
from sklearn import metrics
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))
How would I modify this to get the training accuracy?
CodePudding user response:
You can get the training set predictions as
y_train_pred = clf.predict(X_train)
where clf
is your fitted RandomForestClassifier
. After that you can use
metrics.accuracy_score(y_train, y_train_pred)
to calculate the training accuracy. Alternatively, you can use
clf.score(X_train, y_train)
which should give you the same result.