%% Bin creditscorecard Data as Bin Numbers, Categories, or WOE Values
% This example shows how to use the bindata function to simply bin or discretize data.
% Suppose bin ranges of
% '0 to 30'
% '31 to 50'
% '51 and up'
% are determined for the age variable (via manual or automatic binning). If a data point with age 41 is given, binning this data point means placing it in the bin for 41 years old, which is the second bin, or the '31 to 50' bin. Binning is then the mapping from the original data, into discrete groups or bins. In this example, you can say that a 41-year old is mapped into bin number 2, or that it is binned into the '31 to 50' category. If you know the Weight of Evidence (WOE) value for each of the three bins, you could also replace the data point 41 with the WOE value corresponding to the second bin. bindata supports the three binning formats just mentioned:
% Bin number (where the 'OutputType' name-value pair argument is set to 'BinNumber'); this is the default option, and in this case, 41 is mapped to bin 2.
% Categorical (where the 'OutputType' name-value pair argument is set to 'Categorical'); in this case, 41 is mapped to the '31 to 50' bin.
% WOE value (where the 'OutputType' name-value pair argument is set to 'WOE'); in this case, 41 is mapped to the WOE value of bin number 2.
% Create a creditscorecard object using the CreditCardData.mat file to load the data (using a dataset from Refaat 2011). Use the 'IDVar' argument to indicate that 'CustID' contains ID information and should not be included as a predictor variable.

load CreditCardData 
sc = creditscorecard(data,'IDVar','CustID')

%
% Perform automatic binning.

sc = autobinning(sc);

%
% Show the bin information for 'CustAge'.

bininfo(sc,'CustAge')

%
% These are the first 10 age values in the original data, used to create the creditscorecard object.

data(1:10,'CustAge')

%
% Bin scorecard data into bin numbers (default behavior).

bdata = bindata(sc);

%
% According to the bin information, the first age should be mapped into the fourth bin, the second age into the fifth bin, etc. These are the first 10 binned ages, in bin-number format.

bdata(1:10,'CustAge')

%
% Bin the scorecard data and show their bin labels. To do this, set the bindata name-value pair argument for 'OutputType' to 'Categorical'.

bdata = bindata(sc,'OutputType','Categorical');

%
% These are the first 10 binned ages, in categorical format.

bdata(1:10,'CustAge')

%
% Convert the scorecard data to WOE values. To do this, set the bindata name-value pair argument for 'OutputType' to 'WOE'.

bdata = bindata(sc,'OutputType','WOE');

%
% These are the first 10 binned ages, in WOE format. The ages are mapped to the WOE values that are internally displayed using the bininfo function.

bdata(1:10,'CustAge')

% Copyright 2015 The MathWorks, Inc.