OptimalBucketer
Bases: BaseBucketer
The OptimalBucketer
transformer uses the optbinning package to find optimal buckets.
Support:
This bucketer basically wraps optbinning.OptimalBinning to be consistent with skorecard. Requires a feature to be pre-bucketed to max 100 buckets. Optbinning uses a constrained programming solver to merge buckets, taking into account the following constraints 1) monotonicity in bad rate, 2) at least 5% of records per bin.
Example:
from skorecard import datasets
from skorecard.bucketers import OptimalBucketer
X, y = datasets.load_uci_credit_card(return_X_y=True)
bucketer = OptimalBucketer(variables = ['LIMIT_BAL'])
bucketer.fit_transform(X, y)
Source code in skorecard/bucketers/bucketers.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
__init__(variables=[], specials={}, variables_type='numerical', max_n_bins=10, missing_treatment='separate', min_bin_size=0.05, cat_cutoff=None, time_limit=25, remainder='passthrough', get_statistics=True, solver='cp', monotonic_trend='auto_asc_desc', gamma=0, ob_kwargs={})
¶
Initialize Optimal Bucketer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variables |
List of variables to bucket. |
[]
|
|
specials |
(nested) dictionary of special values that require their own binning.
The dictionary has the following format:
{" |
{}
|
|
variables_type |
Passed to optbinning.OptimalBinning: Type of the variables. Must be either 'categorical' or 'numerical'. |
'numerical'
|
|
missing_treatment |
Defines how we treat the missing values present in the data.
If a string, it must be one of the following options:
separate: Missing values get put in a separate 'Other' bucket: |
'separate'
|
|
min_bin_size |
Passed to optbinning.OptimalBinning: Minimum fraction of observations in a bucket. |
0.05
|
|
max_n_bins |
Passed to optbinning.OptimalBinning: Maximum numbers of bins to return. |
10
|
|
cat_cutoff |
Passed to optbinning.OptimalBinning: Threshold ratio (None, or >0 and <=1) below which categories are grouped together in a bucket 'other'. |
None
|
|
time_limit |
float
|
Passed to optbinning.OptimalBinning: Time limit in seconds to find an optimal solution. |
25
|
remainder |
How we want the non-specified columns to be transformed. It must be in ["passthrough", "drop"]. passthrough (Default): all columns that were not specified in "variables" will be passed through. drop: all remaining columns that were not specified in "variables" will be dropped. |
'passthrough'
|
|
solver |
str
|
Passed to optbinning.OptimalBinning: The optimizer to solve the optimal binning problem. Supported solvers are “mip” to choose a mixed-integer programming solver, “cp” (default) to choose a constrained programming solver or “ls” to choose LocalSolver. |
'cp'
|
monotonic_trend |
str
|
Passed to optbinning.OptimalBinning: The event rate monotonic trend. Supported trends are “auto”, “auto_heuristic” and “auto_asc_desc” to automatically determine the trend maximizing IV using a machine learning classifier, “ascending”, “descending”, “concave”, “convex”, “peak” and “peak_heuristic” to allow a peak change point, and “valley” and “valley_heuristic” to allow a valley change point. Trends “auto_heuristic”, “peak_heuristic” and “valley_heuristic” use a heuristic to determine the change point, and are significantly faster for large size instances (max_n_prebins > 20). Trend “auto_asc_desc” is used to automatically select the best monotonic trend between “ascending” and “descending”. If None, then the monotonic constraint is disabled. |
'auto_asc_desc'
|
gamma |
float
|
Passed to optbinning.OptimalBinning: Regularization strength to reduce the number of dominating bins. Larger values specify stronger regularization. Default is 0. Option supported by solvers “cp” and “mip”. |
0
|
ob_kwargs |
dict
|
Other parameters passed to optbinning.OptimalBinning. |
{}
|
Source code in skorecard/bucketers/bucketers.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|