1
$\begingroup$

I am working with the Loan problem whether Loan Status: Defaulter or Non-Defaulters.In this problem, my classes are unbalanced 90% of classes are Defaulter, and 10% of them Non-Defaulter. Then I tried oversampling method. here is my code:

from imblearn.over_sampling import RandomOverSampler
ros = RandomOverSampler(0.5)
X,y=ros.fit_resample(X, y)

Then I used a random forest classifier after I am trying to predict my test data. I not some error. Here is my code.

pred=clf.predict(test_df)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_12673/2789171072.py in <module>
----> 1 pred=clf.predict(test_df)

~/miniconda3/lib/python3.8/site-packages/sklearn/ensemble/_forest.py in predict(self, X)
    796             The predicted classes.
    797         """
--> 798         proba = self.predict_proba(X)
    799 
    800         if self.n_outputs_ == 1:

~/miniconda3/lib/python3.8/site-packages/sklearn/ensemble/_forest.py in predict_proba(self, X)
    838         check_is_fitted(self)
    839         # Check data
--> 840         X = self._validate_X_predict(X)
    841 
    842         # Assign chunk of trees to jobs

~/miniconda3/lib/python3.8/site-packages/sklearn/ensemble/_forest.py in _validate_X_predict(self, X)
    567         Validate X whenever one tries to predict, apply, predict_proba."""
    568         check_is_fitted(self)
--> 569         X = self._validate_data(X, dtype=DTYPE, accept_sparse="csr", reset=False)
    570         if issparse(X) and (X.indices.dtype != np.intc or X.indptr.dtype != np.intc):
    571             raise ValueError("No support for np.int64 index based sparse matrices")

~/miniconda3/lib/python3.8/site-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
    578 
    579         if not no_val_X and check_params.get("ensure_2d", True):
--> 580             self._check_n_features(X, reset=reset)
    581 
    582         return out

what is the issue here? and how to deal with test data when we deal with imbalanced dataset and also please share with me what are the good techniques to deal with imbalanced classes.

$\endgroup$
3
  • 1
    $\begingroup$ There is a lot of information on this site and crossvalidated on techniques to deal with imbalanced data as well as doing nothing since it is often not a problem. Please search these sites. We are happy to help if you have more questions on the techniques. For the python problem, there is not enough information for me to help. The code uses X, y and test_df. Please put some debugging in before predict to check the data sizes, look at the data, etc, to make sure it is what you are expecting. Happy to help with more info. $\endgroup$
    – Craig
    Dec 7, 2021 at 10:26
  • $\begingroup$ Most likely, the best approach is to do nothing. $\endgroup$
    – Dave
    Dec 7, 2021 at 15:53
  • $\begingroup$ well, doing nothing is not very informative either; assuming the data represents realistically our problem, yes, we should not apply under/over sampling, but the right selection of our metric (KPI in this post) is still necessary $\endgroup$
    – German C M
    Dec 7, 2021 at 16:35

1 Answer 1

1
$\begingroup$

With regard to the class imbalanced topic, I would consider these two common/possible cases:

  • is the class imbalance of your dataset caused by the way the data was gathered? If afirmative, and you do not have the chance to get more representative data, I would go over/undersampling methodologies as you are trying
  • otherwise, if you think the dataset is representative enough of the nature of your problem, I would focus on selecting the right metric to evaluate your model, and keep the dataset imbalance as is (this way the model learns to predict on the real classes distribution which will find at inference time); in this case, you can select some standard metrics (among others) which help you pay oattention on the minority class (your "ones"), like could be F1-score or precision-recall AUC (in this second one you can afterwards select the best threshold for your classifier) and so on.

About the technical problem you found, might it be test_df you are using? You should display all the code and some dataset rows example.

$\endgroup$
3
  • $\begingroup$ (in this second one you can afterwards select the best threshold for your classifier). Any articles/blogs on this that you can refer? $\endgroup$
    – spectre
    Dec 7, 2021 at 10:03
  • 1
    $\begingroup$ that is based on the same principle as when using ROC curves, which helps you identify which classifier threshold best suits your needs; following the same sklearn docu you have: scikit-learn.org/stable/auto_examples/model_selection/… and a nice post also is machinelearningmastery.com/… $\endgroup$
    – German C M
    Dec 7, 2021 at 10:30
  • 1
    $\begingroup$ Thanks a lot brotha! $\endgroup$
    – spectre
    Dec 7, 2021 at 13:58

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.