1
$\begingroup$

I am using linear regression algorithm for a data set. And trying to compute confusion matrix between y_pred and y_test. I am getting "ValueError : continuous is not supported" error.

I have included the code below. Help to solve this problem.

x = data_set.iloc[:, :-1].values
y = data_set.iloc[:, 7].values

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_state = 0) 

from sklearn.linear_model import LinearRegression
from sklearn.metrics import confusion_matrix

regression = LinearRegression()
regression.fit(x_train, y_train)

y_pred = regression.predict(x_test)

cm = confusion_matrix(y_test, y_pred)
$\endgroup$
  • $\begingroup$ Can you post more information from the error? And perhaps a little more about the type of data you have? I imagine you have continuous values in y_pred which you are using as input to the confusion matrix. $\endgroup$ – n1k31t4 Feb 22 at 12:09
  • $\begingroup$ y_pred = array([0.65975096, 0.83294571, 0.77782128, 0.87880993, 0.60173406]) and y_test = array([0.64, 0.85, 0.8 , 0.91, 0.68]) $\endgroup$ – Harshith Feb 22 at 12:10
  • $\begingroup$ Above are the values in y_pred and y_test. How do i solve this error? Type of y_pred is numpy.ndarray. $\endgroup$ – Harshith Feb 22 at 12:15
2
$\begingroup$

The confusion matrix is used to tell you how many predictions were classified correctly or incorrectly. You are looking at a regression model, which gives you a continous output (not classification).

So when you run confusion_matrix(y_test, y_pred) it will throw the ValueError because it expected class predictions, not floating point numbers.

Are you trying to predict classes, or really just a number output? If not, then you should not be using the confusion matrix.


If you want to predict e.g. 1 or 0 for your y values, then you would have to convert your linear regression predictions to either of these classes. You could say any value in y_pred above 0.7 is a 1 and anything below is 0.

cutoff = 0.7                              # decide on a cutoff limit
y_pred_classes = np.zeros_like(y_pred)    # initialise a matrix full with zeros
y_pred_classes[y_pred > cutoff] = 1       # add a 1 if the cutoff was breached

you have to do the same for the actual values too:

y_test_classes = np.zeros_like(y_pred)
y_test_classes[y_test > cutoff] = 1

Now run the confusion matrix as before:

confusion_matrix(y_test_classes, y_pred_classes)

which gives output:

array([[2, 3],
       [0, 0]])
$\endgroup$
  • $\begingroup$ I am trying to predict a number output. Do i have to scale the values? $\endgroup$ – Harshith Feb 22 at 13:07
  • $\begingroup$ No need to scale really, but you cannot compute the confusion matrix over a continuous prediction space. It has to be discrete! $\endgroup$ – n1k31t4 Feb 22 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.