1
$\begingroup$

My goal is to predict the count (variable y) based on various features (variable x). My y is most of the time (98.4%) equal to 0, so this data is inflated by 0.

Based on this premise, I thought that using the Zero Inflated Model with SVC could be an asset, given the characteristics of my data.

So I found a code on the internet and i'm trying to apply it to my problem (i'm very new to this)

 from sklego.meta import ZeroInflatedRegressor

    zir = ZeroInflatedRegressor(
        classifier=SVC(),
        regressor=LinearRegression()
    )

    zir.fit(scaled_train, y_train.values.ravel())
    
    predictions  = zir.predict(scaled_test)

I believe my problem is exactly in this part of the code below, where I am appending only 0s and 1s. However, how do I make predictions for the other counts? This is what I'm not able to understand.

 trashold = .5
    preds =[]
    for  val in predictions:
        if val>trashold:
            preds.append(1)
        else:
            preds.append(0)

    accuracy_score(y_test,preds)
    0.9976208095270875

Even in my classification_report I have an alert precisely because I don't have samples for the other labels. How to fix such a problem?

print(classification_report(y_test,preds))

Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.

              precision    recall  f1-score   support

         0.0       1.00      1.00      1.00    916493
         1.0       0.86      0.97      0.91     11398
         2.0       0.00      0.00      0.00      1498
         3.0       0.00      0.00      0.00       231
         4.0       0.00      0.00      0.00        73
         5.0       0.00      0.00      0.00        22
         6.0       0.00      0.00      0.00         5
         7.0       0.00      0.00      0.00         5
         8.0       0.00      0.00      0.00         2
         9.0       0.00      0.00      0.00         1

    accuracy                           1.00    929728
   macro avg       0.19      0.20      0.19    929728
weighted avg       1.00      1.00      1.00    929728

To illustrate my problem, I have the following image. Which shows that 0 values ​​are reasonably well predicted, but from 1 I don't have any predictions anymore.

plt.clf()
plt.hist([y_train.values, preds], log=True)
plt.legend(('orig','pred'))
plt.show()

enter image description here

Any idea how I can improve this model and have predictions for all classes? Thank you very much in advance.

$\endgroup$

1 Answer 1

2
$\begingroup$

You set a threshold at $0.5$.

If your output is above this value, you report $1$. Otherwise, you report $0$.

Your model isn’t predicting any other values because you told it only to predict those two values.

Aside from this, I see another issue with your methodology. Your output is numerical, yet you are shoehorning your model into a classifier. Predict the numbers with some kind of regression model or ordinal regression model. Regressions need not give integer values, even for integer-only observation, and this is a feature, not a bug. Regression models predict means, and means do not have to be plausible values.

$\endgroup$
4
  • $\begingroup$ Thanks for your comment, Dave. I thought I was using a classifier and a regressor in the same model. The classifier, in this case, with the objective of classifying whether the predicted number is 0 or any other; and the regressor to say, if it is different from 0, what other value then it takes. Does this make any sense? Can you help me then to fix this methodology? $\endgroup$ Sep 8, 2022 at 18:32
  • $\begingroup$ Where do you handle the un-thresholded predictions? // Be careful with thresholds. They introduce a lot of issues. We talk about this extensively on Cross Validated Stack Exchange, often in the context of imbalanced classes, but not necessarily. $\endgroup$
    – Dave
    Sep 8, 2022 at 18:38
  • $\begingroup$ You are right, Dave. I do not handle them. In view of all these problems raised in relation to thresholds , what would be the most appropriate way for me to carry out these classifications? $\endgroup$ Sep 8, 2022 at 22:01
  • $\begingroup$ You’re not doing a classification. $\endgroup$
    – Dave
    Sep 8, 2022 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.