3
from sklearn.metrics import accuracy_score
accuracy_score(y_true, y_pred)

I believe this code will return the accuracy of our predictions. However, I am comparing predicted and actual values of continuous values and I believe that most of them are not going to be exactly same.

Should I fit the test set values and plot the predicted values to get the R-squared?

Can anyone please advise me on how to measure the accuracy of predictions in the case of continuous variables?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Aditya
  • 89
  • 1
  • 1
  • 6
  • Accuracy is not defined for regression problems (i.e. continuous output), such as yours; have a look at the discussion in [this thread](https://stackoverflow.com/questions/48775305/what-function-defines-accuracy-in-keras-when-the-loss-is-mean-squared-error-mse) (it is for Keras, but the argument is the same). – desertnaut Mar 05 '18 at 11:27

1 Answers1

10

In machine learning, accuracy is defined for discrete values (classes). Its defined as the fraction of correct predictions from total predictions made.

So, a prediction of value 319 where true value is 320 is still an incorrect prediction.

So its not advised to calculate accuracy for continuous values. For such values you would want to calculate a measure of how close the predicted values are to the true values. This task of prediction of continuous values is known as regression. And generally R-squared value is used to measure the performance of the model.

You can use r2_score(y_true, y_pred) for your scenario.

There are various metrics for regression tasks (continuous variables prediction) like:-

  • Mean squared error,
  • Mean absolute error,
  • Variance score, etc

You can get more info about the sklearn implementation of these metrics here.

Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
  • I would omit 'mostly' – desertnaut Mar 05 '18 at 11:24
  • 1
    @desertnaut Done. Thanks. I was using mostly because some tools dont make such difference known explicitly. I was also thinking of scenarios in which labels are encoded and then the user can calculate the accuracy although which dont apply to this case. – Vivek Kumar Mar 05 '18 at 11:33