I am implementing a regression, however my regressor has not been able to predict the least frequent counts. I've tried adjusting the hyperparameters (as you can see below), but I haven't had much success.
As you can see below, my dataset is zero-inflated and the count frequency drops dramatically as the count value increases.
df_count = df.groupby(['count']).size().to_frame(name = 'size').reset_index()
df_count
count size
0 2291939
1 23796
2 3513
3 595
4 209
5 52
6 24
7 7
8 2
10 1
15 1
As the frequency of some values is very small, I have had difficulty in predicting them, as you can see in my attempts below. Any suggestions on how to handle this situation?
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
zir_rf = ZeroInflatedRegressor(
classifier=RandomForestClassifier(random_state=0),
regressor=RandomForestRegressor(random_state=0)
)
zir_rf.fit(scaled_train, y_train.values.ravel())
predictions_zir_rf = zir_rf.predict(scaled_test)
mean_squared_error(y_test['count'].values,predictions_zir_rf)
0.004358037315598189
plt.clf()
plt.hist([y_test['count'].values, predictions_zir_rf], log=True)
plt.legend(('real','predction'))
plt.show()
After some RandomizedSearchCV I tried the following model and still got the same problem of not being able to predict the less frequent counts.
zir_rf_tunned = ZeroInflatedRegressor(
classifier=RandomForestClassifier(random_state=0),
regressor=RandomForestRegressor(n_estimators=200, min_samples_split = 10, min_samples_leaf = 2, max_features = 'sqrt', max_depth =50, bootstrap = True)
)
predictions_zir_rf_tunned = zir_rf_tunned.predict(scaled_test)
mean_squared_error(y_test['count'].values,predictions_zir_rf_tunned)
0.004107122405590996
plt.clf()
plt.hist([y_test['count'].values, predictions_zir_rf_tunned], log=True)
plt.legend(('real','predction'))
plt.show()