Shrinkage¶
sklego.meta._shrinkage_utils.ShrinkageMixin
¶
Mixin class for shrinkage functionality (setting shrinkage, checking shrinkage function, and fitting shrinkage factors). The shrinkage factors are used to weigh the predictions of the different levels of a model.
Class inherits from this mixin should have the following attributes:
_ALLOWED_SHRINKAGE
: dict[str, callable] A dictionary mapping the name of the shrinkage function to the function itself.shrinkage
: str | callable | None The shrinkage function to use. If a callable is passed, it should take an array of group sizes and return an array of shrinkage factors.shrinkage
is parsed by_set_shrinkage_function
, which then returnsshrinkage_function_
to be used in_fit_shrinkage_factors
.shrinkage_kwargs
: dict[str, Any] Additional keyword arguments to pass to the shrinkage function.fitted_levels_
: list[str | int] List of the levels that have been fitted.estimators_
: dict[tuple[Any, ...], scikit-learn compatible estimator]
Source code in sklego/meta/_shrinkage_utils.py
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
Shrinkage Functions¶
The following functions are the available built-in shrinkage accessed in the GroupedPredictor
and HierarchicalPredictor
.
sklego.meta._shrinkage_utils.constant_shrinkage(group_sizes, alpha)
¶
The augmented prediction for each level is the weighted average between its prediction and the augmented prediction for its parent.
Let \(\hat{y}_i\) be the prediction at level \(i\), with \(i=0\) being the root, than the augmented prediction \(\hat{y}_i^* = \alpha \hat{y}_i + (1 - \alpha) \hat{y}_{i-1}^*\), with \(\hat{y}_0^* = \hat{y}_0\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group_sizes
|
array - like
|
The number of observations in each group, must implement the |
required |
alpha
|
float
|
The weight of the prediction at the current level. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The weights for each group. |
Source code in sklego/meta/_shrinkage_utils.py
sklego.meta._shrinkage_utils.equal_shrinkage(group_sizes)
¶
Each group is weighed equally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group_sizes
|
array - like
|
The number of observations in each group, must implement the |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The weights for each group. |
Source code in sklego/meta/_shrinkage_utils.py
sklego.meta._shrinkage_utils.min_n_obs_shrinkage(group_sizes, min_n_obs)
¶
Use only the smallest group with a certain amount of observations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group_sizes
|
array - like
|
The number of observations in each group. |
required |
min_n_obs
|
int
|
The minimum number of observations for a group to be considered. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The weights for each group. |
Source code in sklego/meta/_shrinkage_utils.py
sklego.meta._shrinkage_utils.relative_shrinkage(group_sizes)
¶
Weigh each group according to its size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group_sizes
|
array - like
|
The number of observations in each group. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The weights for each group. |