Search code examples
pythonmachine-learningregressionrandom-forestconfusion-matrix

Machine Learning: Getting error in Confusion Matrix


While running random forest regression model, I am getting error while running the confusion matrix. The model is able to predict the y_pred but I am not able to check the accuracy since I am not able to run accuracy matrix.

Datatypes:

type(y_test)
Out[124]: numpy.ndarray

type(y_pred)
Out[125]: numpy.ndarray

Below is my code for confusion matrix:

from sklearn.metrics import confusion_matrix
matrix = confusion_matrix(y_test, y_pred)

print(matrix)

from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)

Error:

confusion_matrix(y_test, y_pred)

Traceback (most recent call last):

File "<ipython-input-121-7a3dbcfd18a0>", line 1, in <module>
confusion_matrix(y_test, y_pred)

File "/Users/srikantswamy/anaconda3/lib/python3.6/site-packages/sklearn  /metrics/classification.py", line 253, in confusion_matrix
y_type, y_true, y_pred = _check_targets(y_true, y_pred)

File "/Users/srikantswamy/anaconda3/lib/python3.6/site-packages/sklearn /metrics/classification.py", line 88, in _check_targets
raise ValueError("{0} is not supported".format(y_type))

ValueError: continuous is not supported

Solution

  • To give a simple answer : A confusion matrix is a table that is often used to describe the performance of a classification model (or "classifier") . In your case you are trying to find the confusion matrix for the continuous variables. for regression use mean square error. Have a look at this lecture pdf: http://www.it.uu.se/edu/course/homepage/sml/lectures/lecture3_handout.pdf