%% Perform Automatic Binning Using the Split Algorithm
% This example demonstrates using the 'Split' algorithm with categorical and numeric predictors. Load the CreditCardData.mat dataset and modify so that it contains four categories for the predictor 'ResStatus' to demonstrate how the split algorithm works.

load CreditCardData.mat
x = data.ResStatus;
Ind = find(x == 'Tenant');
Nx  = length(Ind);
x(Ind(1:floor(Nx/3))) = 'Subletter';
data.ResStatus = x;

% Create a creditscorecard and use bininfo to display the 'Statistics'.

sc = creditscorecard(data,'IDVar','CustID');
[bi1,cg1] = bininfo(sc,'ResStatus','Statistics',{'Odds','WOE','InfoValue'});
disp(bi1)
disp(cg1)

%% Using the Split Algorithm with a Categorical Predictor
% Apply presorting to the 'ResStatus' category using the default sorting by 'Odds' and specify the 'Split' algorithm.

sc = autobinning(sc,'ResStatus', 'Algorithm', 'split','AlgorithmOptions',...
    {'Measure','gini','SortCategories','odds','Tolerance',1e-4});
[bi2,cg2] = bininfo(sc,'ResStatus','Statistics',{'Odds','WOE','InfoValue'});
disp(bi2)
disp(cg2)

%% Using the Split Algorithm with a Numeric Predictor
% To demonstrate a split for the numeric predictor, 'TmAtAddress', first use autobinning with the default 'Monotone' algorithm.

sc = autobinning(sc,'TmAtAddress');
bi3 = bininfo(sc,'TmAtAddress','Statistics',{'Odds','WOE','InfoValue'});
disp(bi3)

% Then use autobinning with the 'Split' algorithm.

sc = autobinning(sc,'TmAtAddress','Algorithm', 'Split');
bi4 = bininfo(sc,'TmAtAddress','Statistics',{'Odds','WOE','InfoValue'});
disp(bi4)

% Copyright 2012 The MathWorks, Inc.