2
$\begingroup$

I have a continuous variable called salary, age etc and output variable as loan_status

Instead of me choosing the cut off points (which I did) for salary and age bins , I would like to compute the bins based on loan_status.

Is there any automated solution or python package to do this (and return the actual bin values which we can interpret (instead of returning some probabilities or something)? If probabilities, I may not know which probability value corresponds to which bin/salary value.

I ask this because I don't have business user inputs. I just wish to compare the model performance (based on my judgement of binning data) against the model performance (based on automated binning).

Please note that I want binning method which takes into account the loan_status. Meaning, if loan_status = 1 for people mostly with salary from 10000 to 50000, then I would like that to be bin 1, 2 (based on n_bins) and rest of the values should be assigned to bin 3. I am aware of quantile binning, k-means binning etc. But I guess they don't consider the outcome label distribution. I know we have similar solutions for categorical encoding based on target label (count, frequency etc). Is there anything for continuous variable encoding based on outcome label?

Again, if it is just a min and max of the bin based on the loan_status, it would just be 2 bins (and there is no intelligence here). Is there any automated package that can suggest us the no of bins for each label or if we configure n_bins=3, can it provide 3 bins for each label?

I would like to arrive at a bin value based on the outcome label distribution

Can guide me on this?

$\endgroup$
7
  • $\begingroup$ I am trying to completely understand your problem. You have X variables salary and age, both continuous and you have a Y variable (that you're trying to predict?) called loan status. You want to turn the continuous X variables into categorical variables by binning them, and you would like this binning to be in some way based on the Y, loan status? Do I understand correctly? $\endgroup$ Jan 25, 2022 at 16:14
  • $\begingroup$ @Vladimir Belik Yes correct $\endgroup$
    – The Great
    Jan 25, 2022 at 23:31
  • 1
    $\begingroup$ Okay. I don't understand why you want to do this, though. How would it make sense to bin the X variables based on the Y variable they're supposed to predict? That sounds like data leakage (because you'd be using Y to transform X to then help predict the Y that was used in the transformation). $\endgroup$ Jan 25, 2022 at 23:32
  • $\begingroup$ It's like to trying to find the pattern/bins of Salary based on labelled data..Once, the new unseen data point comes in, they can be assigned to the appropriate bin...Because all new unseem datavpoints will have salary info at that point of time (and are not going to change)..all these predictioms are only at a point of time (and not at diff tike intervals). Have you heard of supervised binning? $\endgroup$
    – The Great
    Jan 26, 2022 at 0:03
  • 1
    $\begingroup$ This is an information-losing approach, will create lack of model fit, and is double dipping. $\endgroup$ Jan 26, 2022 at 16:53

2 Answers 2

1
$\begingroup$

After a somewhat extended discussion in chat with OP, we arrived at some conclusions which I will share here.

The subject is supervised binning (a set of techniques where the predictors are binned based, in some way, on the target variable). A common way to do supervised binning is described in some detail here, and here. The idea simply to run some algorithm (let's say decision trees) in order to determine what are the most "natural" cut points for a continuous variable (to turn it into a "binned" categorical variable), based on how those cut-points correspond to values of the target variable.

Example: If you have continuous variable X and binary target variable Y, a decision tree can help identify that in most cases of X <= 5 , Y=1 . And in most cases of X > 5, Y=0 . So, it might be logical to turn X into a categorical variable, using X=5 as the cut point.

Now that we've established the logic there, I want to highlight a crucial concern for anyone using this kind of binning.

Data Leakage

With this technique, it's very easy to fall into extensive data leakage. This can occur very simply: If you have your full data set and you run supervised binning on the entire dataset to create these binned variables, then you split it into a train and test set, you have big data leakage. Why? Because your supervised binning algorithm already "saw" the test set data to try to decide the optimal cut points - therefore when you go back to test some model on the test set, the results will be overly optimistic because your supervised binning took that hold-out data into account: clear data leakage.

Once you recognize this risk, though, it's pretty easy to avoid. Create your supervised binning variable by only using training data once you've made the split. Similarly, when you are (hopefully) using k-fold cross validation, you need to create these supervised binning variables separately for each fold! Why? Because again, if you do 5-fold CV, if you use all 5 folds to create the binned variable and then you do the training on 4 to validate on the 5th fold, your binning algorithm has already seen the 5th fold so again, your results will be skewed.

I hope this clarifies why you need to be careful when applying this method, and to only apply it on the same data that your model is being trained on (with no allowance for the supervised binning algorithm to see data it shouldn't see at the current stage of training/validation/testing).

Final tip: To be clear then, you might ask "okay I've selected my model and I'm ready to train it on the entire training set so I can test it out on the test set. How do I generate the supervised binning variable for the test set?" . To avoid leakage, you want to create your supervised binning model (ex: decision tree) on the entire training set. Then, for every test set data point, you run it through that existing, trained model to give supervised binned variable for that test data point (without training the model on the test set - only on training set).

If you have any questions please feel free to ask!

$\endgroup$
0
0
$\begingroup$

If I understood correctly you want to bin 2 variables (salary, and age) according your target variable (loan_status), which I understand is binary. If my assumption is correct, I recommend to use optbinning, this python package is well documented.

Good luck.

$\endgroup$
3
  • $\begingroup$ Binning is a really bad idea in this context (and most other contexts). $\endgroup$ Nov 29, 2022 at 13:11
  • $\begingroup$ As a general thumb rule, I agree with you but it does depend on the distribution of your data, the number of outliers, variance, etc. $\endgroup$ Nov 30, 2022 at 14:16
  • $\begingroup$ It does not depend on that. Binning replacing one problem with more serious problems: information loss and lack of model fit. $\endgroup$ Dec 2, 2022 at 16:34

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.