Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 209
atlantis Avatar asked Mar 17 '12 22:03

atlantis


1 Answers

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.)
like image 86
Fred Foo Avatar answered Sep 28 '22 11:09

Fred Foo