Continuous variable not supported in confusion matrix 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)
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 &gt; 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 &gt; 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]])