2

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?

2
  • 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 get the training accuracy. Alternatively, you can use clf.score(X_train, y_train), which should give you the same result. Commented Nov 3, 2021 at 18:16
  • That works, please convert to an answer and I can accept it. Commented Nov 3, 2021 at 18:22

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.