QBoard » Artificial Intelligence & ML » AI and ML - Python » Accuracy Score ValueError: Can't Handle mix of binary and continuous target

Accuracy Score ValueError: Can't Handle mix of binary and continuous target

  • I'm using linear_model.LinearRegression from scikit-learn as a predictive model. It works and it's perfect. I have a problem to evaluate the predicted results using the accuracy_score metric.

    This is my true Data :
    array([1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0])​

    My predicted Data:

    array([ 0.07094605,  0.1994941 ,  0.19270157,  0.13379635,  0.04654469,
        0.09212494,  0.19952108,  0.12884365,  0.15685076, -0.01274453,
        0.32167554,  0.32167554, -0.10023553,  0.09819648, -0.06755516,
        0.25390082,  0.17248324])

    My code:

    accuracy_score(y_true, y_pred, normalize=False)

    Error message:

    ValueError: Can't handle mix of binary and continuous target

     

      August 16, 2021 9:49 PM IST
    0
  • Despite the plethora of wrong answers here that attempt to circumvent the error by numerically manipulating the predictions, the root cause of your error is a theoretical and not computational issue: you are trying to use a classification metric (accuracy) in a regression (i.e. numeric prediction) model (LinearRegression), which is meaningless.

    Just like the majority of performance metrics, accuracy compares apples to apples (i.e true labels of 0/1 with predictions again of 0/1); so, when you ask the function to compare binary true labels (apples) with continuous predictions (oranges), you get an expected error, where the message tells you exactly what the problem is from a computational point of view:

    Classification metrics can't handle a mix of binary and continuous target
    ​


    Despite that the message doesn't tell you directly that you are trying to compute a metric that is invalid for your problem (and we shouldn't actually expect it to go that far), it is certainly a good thing that scikit-learn at least gives you a direct and explicit warning that you are attempting something wrong; this is not necessarily the case with other frameworks - see for example the behavior of Keras in a very similar situation, where you get no warning at all, and one just ends up complaining for low "accuracy" in a regression setting...

    I am super-surprised with all the other answers here (including the accepted & highly upvoted one) effectively suggesting to manipulate the predictions in order to simply get rid of the error; it's true that, once we end up with a set of numbers, we can certainly start mingling with them in various ways (rounding, thresholding etc) in order to make our code behave, but this of course does not mean that our numeric manipulations are meaningful in the specific context of the ML problem we are trying to solve.

    So, to wrap up: the problem is that you are applying a metric (accuracy) that is inappropriate for your model (LinearRegression): if you are in a classification setting, you should change your model (e.g. use LogisticRegression instead); if you are in a regression (i.e. numeric prediction) setting, you should change the metric. Check the list of metrics available in scikit-learn, where you can confirm that accuracy is used only in classification.

    Compare also the situation with a recent SO question, where the OP is trying to get the accuracy of a list of models:

    models = []
    models.append(('SVM', svm.SVC()))
    models.append(('LR', LogisticRegression()))
    models.append(('LDA', LinearDiscriminantAnalysis()))
    models.append(('KNN', KNeighborsClassifier()))
    models.append(('CART', DecisionTreeClassifier()))
    models.append(('NB', GaussianNB()))
    #models.append(('SGDRegressor', linear_model.SGDRegressor())) #ValueError: Classification metrics can't handle a mix of binary and continuous targets
    #models.append(('BayesianRidge', linear_model.BayesianRidge())) #ValueError: Classification metrics can't handle a mix of binary and continuous targets
    #models.append(('LassoLars', linear_model.LassoLars())) #ValueError: Classification metrics can't handle a mix of binary and continuous targets
    #models.append(('ARDRegression', linear_model.ARDRegression())) #ValueError: Classification metrics can't handle a mix of binary and continuous targets
    #models.append(('PassiveAggressiveRegressor', linear_model.PassiveAggressiveRegressor())) #ValueError: Classification metrics can't handle a mix of binary and continuous targets
    #models.append(('TheilSenRegressor', linear_model.TheilSenRegressor())) #ValueError: Classification metrics can't handle a mix of binary and continuous targets
    #models.append(('LinearRegression', linear_model.LinearRegression())) #ValueError: Classification metrics can't handle a mix of binary and continuous targets​


    where the first 6 models work OK, while all the rest (commented-out) ones give the same error. By now, you should be able to convince yourself that all the commented-out models are regression (and not classification) ones, hence the justified error.

    A last important note: it may sound legitimate for someone to claim:

    OK, but I want to use linear regression and then just round/threshold the outputs, effectively treating the predictions as "probabilities" and thus converting the model into a classifier

    Actually, this has already been suggested in several other answers here, implicitly or not; again, this is an invalid approach (and the fact that you have negative predictions should have already alerted you that they cannot be interpreted as probabilities). Andrew Ng, in his popular Machine Learning course at Coursera, explains why this is a bad idea - see his Lecture 6.1 - Logistic Regression | Classification at Youtube (explanation starts at ~ 3:00), as well as section 4.2 Why Not Linear Regression [for classification]? of the (highly recommended and freely available) textbook An Introduction to Statistical Learning by Hastie, Tibshirani and coworkers
      August 17, 2021 12:59 PM IST
    0
  • The sklearn.metrics.accuracy_score(y_true, y_pred) method defines y_pred as:
    y_pred : 1d array-like, or label indicator array / sparse matrix. Predicted labels, as returned by a classifier.
    Which means y_pred has to be an array of 1's or 0's (predicated labels). They should not be probabilities.
    The predicated labels (1's and 0's) and/or predicted probabilites can be generated using the LinearRegression() model's methods predict() and predict_proba() respectively.
    1. Generate predicted labels:
    LR = linear_model.LinearRegression() y_preds=LR.predict(X_test) print(y_preds)
    output:
    [1 1 0 1]
    y_preds can now be used for the accuracy_score() method: accuracy_score(y_true, y_pred)
    2. Generate probabilities for labels:
    Some metrics such as 'precision_recall_curve(y_true, probas_pred)' require probabilities, which can be generated as follows:
    LR = linear_model.LinearRegression() y_preds=LR.predict_proba(X_test) print(y_preds)
    output:
    [0.87812372 0.77490434 0.30319547 0.84999743]
      August 17, 2021 4:22 PM IST
    0
  • Classification and Regression are two major prediction problems that are usually dealt with in Data mining and machine learning. accuracy_score is a classification metric, you cannot use it for a regression problem. You get the error because these regression model do not produce binary outcomes, but continuous (float) numbers (as all regression model do); so, when scikit-learn attempts to calculate the accuracy by comparing a binary number (true label) with a float (predicted value), it not unexpectedly gives an error. And this cause is clearly hinted at the error message itself:

    The sklearn.metrics.accuracy_score(y_true, y_pred) method defines y_pred as:

    y_pred : 1d array-like, or label indicator array / sparse matrix. Predicted labels, as returned by a classifier.

    Which means y_pred has to be an array of 1's or 0's (predicated labels). They should not be probabilities.
      December 20, 2022 10:45 AM IST
    0
  • The problem is that the true y is binary (zeros and ones), while your predictions are not. You probably generated probabilities and not predictions, hence the result :) Try instead to generate class membership, and it should work!

     
      November 18, 2021 12:10 PM IST
    0
  • Linear regression is a very poor classifier because it can’t classify two classes clearly. So It is not recommended to use for classification problems.
    You can fix this error by using the following code:

    accuracy_score(y_true, y_pred.round(), normalize=False)

    If you wish to learn more about Machine Learning, then check out this Machine Learning Tutorial for more insights.
    Hope this answer helps.
      November 19, 2021 3:00 PM IST
    0