Search code examples
pythonlinear-regressionscikits

Capping linear regression prediction values using scikit


I am training linear regression model using a data-set which has real valued labels in the interval [0,10]. My predicted values on the test set have some predictions exceeding 10. Is there a way to cap the predictions to 10.

I am thinking of doing a conditional check such that if a prediction exceeds 10, I explicitly set it to 10.

Is there a better way?


Solution

  • If y is the output of the regression object's predict method, then you can Numpy's minimum to cap it to 10:

    y = np.minimum(y, 10.)
    

    To also cap it below at zero, do

    y = np.maximum(np.minimum(y, 10.), 0.)
    

    or, shorter:

    y = np.clip(y, 0., 10.)