Pregunta

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?

¿Fue útil?

Solución

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.)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top