I started learning ML and I have some problems with evaluating / finding the accuracy of regression and classification models. Till now I used .score() in both cases but people told me that Its not the accuracy. Then I tried to use this:

from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_squared_error

var_train, var_test, res_train, res_test = train_test_split(variables, results, test_size = 0.2, random_state = 4)

regression = linear_model.LinearRegression()
regression.fit(var_train, res_train)

input_values = [14, 2]

prediction = regression.predict([input_values])
accuracy_regression = mean_squared_error(var_test, prediction)

But I always get this error:

ValueError: Found input variables with inconsistent numbers of samples: [2, 1]

I have looked all over the udemy and youtube, and a lot of people are calculating accuracy like .score(). Then I looked all over scikit-learn website and stackoverflow and I saw the other solution with metrics but I keep getting the same error. What am I doing wrong?

More about the problem:


  • $\begingroup$ What is the line that returns error? $\endgroup$ – Leevo Jun 17 '19 at 9:29
  • $\begingroup$ The last line : accuracy_regression = mean_squared_error(var_test, prediction) Im trying to find a way to see accuracy / evaluation / score of my regression model. For example , your model is 89.53% accurate. Is that possible to do with regression models? $\endgroup$ – taga Jun 17 '19 at 9:36
  • $\begingroup$ Can you check the shape/length of your objects var_test and prediction ? And if they contain any missing values? $\endgroup$ – Leevo Jun 17 '19 at 9:40

Linear regressions are incompatible with accuracy measures. Accuracy is a metric for classification tasks only - it represents the percentage of observations that your model was able to classify correctly.

In case of Linear regression instead, you are predicting a continuous output. No accuracy can be computed on this. You need other metrics, such as MSE (the one you used), that can be interpreted as "how distant you are from perfect prediction". Sometimes statisticians use the R-squared metric, which represents the percentage of the dependent variable's variance that your model is able to explain (i.e.: when all your data are on a straight line, the R-squared is = 1).

The fact that you are getting an error at mean_squared_error() is suggesting me that your input objects (input_values and var_test) must have either: different shapes, and/or contain missing values. In particular, you are only feeding two observations as input_values: [14, 2]. Is var_test a vector of length 2 ?

| improve this answer | |
  • $\begingroup$ var_test is (2,2). You can check the whole code here: stackoverflow.com/questions/56622349/… I managed to make it work, i didnt use prediction = regression.predict([input_values]), I have used prediction = regression.predict(var_test) but i get prediction value [697.5 426. ] and mean_squared_error 2086.625. $\endgroup$ – taga Jun 17 '19 at 9:55

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.