Get integer prediction for regressor models with sklearn

Question:

I’m using sklearn regressor models to forecast sales by day. However, I want the output to be an integer (because I cannot sell half product) so I’m trying to get the prediction output as an integer, but I cannot find the way.

I know I can transform the float to int, but my question is about the posibility to adjust the model to generate integer prediction (as the inputs are integer numbers too).

from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import BayesianRidge

model_trained = model.fit(x_train, y_train)
y_est = model_trained.predict(x_test)

print(x_test) --> [645 198 546 619 748]

print(y_est) --> [486.84247399 352.79511474 545.31309247 499.17395514 723.32887889]
Asked By: Andrea NR

||

Answers:

The return value for the predict methods is of the type float. You would have to override the regressor models to change the implementation of the predict methods.

Line 215 has the implementation of the method for the KNeighborsRegressor:

sklearn KNeighborsRegressor implementation

You could try and change the dtype parameter on line 240 to np.intc, instead of np.float64. Here is a list of the acceptable data types:

numpy data types

I think that it would be easier to transform the list of floats into a list of ints.

Answered By: i8abyte