flexcv.model_postprocessing
This module contains functions that are specifically written to fulfill the postprocessing requirements of the models.
They are customized depending on what the model returns and what the user wants to log to Neptune.
The functions are called by the cross_validate
function in the flexcv.core
module after the model has been fitted.
flexcv.model_postprocessing.LMERModelPostProcessor
Bases: ModelPostProcessor
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.LMERModelPostProcessor.__call__(results_all_folds, fold_result, features, run, *args, **kwargs)
Postprocessing function for the linear mixed effects model. Logs the summary of the model to neptune.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results_all_folds |
A dict of results for all folds |
required | |
fold_result |
A dataclass containing the results for the current fold |
required | |
run |
neptune run object |
required | |
*args |
any additional arguments |
()
|
|
**kwargs |
any additional keyword arguments |
{}
|
Returns:
Type | Description |
---|---|
dict
|
updated results dictionary |
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.LinearModelPostProcessor
Bases: ModelPostProcessor
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.LinearModelPostProcessor.__call__(results_all_folds, fold_result, features, run, *args, **kwargs)
Postprocessing function for the linear regression model. Logs the summary of the model, the VIF and the plots to neptune.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results_all_folds |
A dict of results for all folds |
required | |
fold_result |
A dataclass containing the results for the current fold |
required | |
run |
neptune run object |
required | |
*args |
any additional arguments |
()
|
|
**kwargs |
any additional keyword arguments |
{}
|
Returns:
Type | Description |
---|---|
dict
|
updated results dictionary |
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.LinearRegDiagnostic
Implementation by https://www.statsmodels.org/stable/examples/notebooks/generated/linear_regression_diagnostics_plots.html#Simple-multiple-linear-regression
Diagnostic plots to identify potential problems in a linear regression fit.
Mainly,
- non-linearity of data
- Correlation of error terms
- non-constant variance
- outliers
- high-leverage points
- collinearity
Source code in flexcv/model_postprocessing.py
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
flexcv.model_postprocessing.LinearRegDiagnostic.__cooks_dist_line(factor)
Helper function for plotting Cook's distance curves
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.LinearRegDiagnostic.__init__(results)
For a linear regression model, generates following diagnostic plots:
a. residual b. qq c. scale location and d. leverage
and a table
e. vif
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results |
Type[RegressionResultsWrapper]
|
must be instance of statsmodels.regression.linear_model object |
required |
Raises:
Type | Description |
---|---|
TypeError
|
if instance does not belong to above object |
Example:
import numpy as np import pandas as pd import statsmodels.formula.api as smf x = np.linspace(-np.pi, np.pi, 100) y = 3*x + 8 + np.random.normal(0,1, 100) df = pd.DataFrame({'x':x, 'y':y}) res = smf.ols(formula= "y ~ x", data=df).fit() cls = Linear_Reg_Diagnostic(res) cls(plot_context="seaborn-paper")
In case you do not need all plots you can also independently make an individual plot/table in following ways
cls = Linear_Reg_Diagnostic(res) cls.residual_plot() cls.qq_plot() cls.scale_location_plot() cls.leverage_plot() cls.vif_table()
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.LinearRegDiagnostic.__qq_top_resid(quantiles, top_residual_indices)
Helper generator function yielding the index and coordinates
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.LinearRegDiagnostic.leverage_plot(ax=None, high_leverage_threshold=False, cooks_threshold='baseR')
Residual vs Leverage plot
Points falling outside Cook's distance curves are considered observation that can sway the fit aka are influential. Good to have none outside the curves.
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.LinearRegDiagnostic.qq_plot(ax=None)
Standarized Residual vs Theoretical Quantile plot
Used to visually check if residuals are normally distributed. Points spread along the diagonal line will suggest so.
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.LinearRegDiagnostic.residual_plot(ax=None)
Residual vs Fitted Plot
Graphical tool to identify non-linearity. (Roughly) Horizontal red line is an indicator that the residual has a linear pattern
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.LinearRegDiagnostic.scale_location_plot(ax=None)
Sqrt(Standarized Residual) vs Fitted values plot
Used to check homoscedasticity of the residuals. Horizontal line will suggest so.
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.LinearRegDiagnostic.vif_table()
VIF table
VIF, the variance inflation factor, is a measure of multicollinearity. VIF > 5 for a variable indicates that it is highly collinear with the other input variables.
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.MERFModelPostProcessor
Bases: ModelPostProcessor
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.MERFModelPostProcessor.__call__(results_all_folds, fold_result, y_pred_base, run, *args, **kwargs)
Postprocessing function for the expectation maximization model (MERF). Logs training and test plots to Neptune.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results_all_folds |
A dict of results for all folds |
required | |
fold_result |
A dataclass containing the results for the current fold |
required | |
run |
neptune run object |
required | |
*args |
any additional arguments |
()
|
|
**kwargs |
any additional keyword arguments |
{}
|
Returns:
Type | Description |
---|---|
dict
|
updated results dictionary |
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.ModelPostProcessor
Bases: ABC
Abstract base class for model postprocessing functions. All postprocessing functions must inherit from this class. Implement your own post processing routing by inheriting from this class and implementing the call method. The class instance is called in the cross validation loop.
Methods:
Name | Description |
---|---|
__call__ |
method to be implemented by the user. This method is called in the cross validation loop. |
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.ModelPostProcessor.__call__(results_all_folds, fold_result, features, run, *args, **kwargs)
This method is called in the cross validation loop. Implement your own post processing routing by inheriting from this class and implementing the call method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results_all_folds |
dict
|
A dict of results for all folds |
required |
fold_result |
SingleModelFoldResult
|
A dataclass containing the results for the current fold |
required |
features |
Index | list[str] | ndarray[str]
|
The features used in the model |
required |
run |
Run
|
neptune run object |
required |
*args |
any additional arguments |
()
|
|
**kwargs |
any additional keyword arguments |
{}
|
Returns:
Name | Type | Description |
---|---|---|
results_all_folds |
dict
|
updated results dictionary |
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.RandomForestModelPostProcessor
Bases: ModelPostProcessor
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.RandomForestModelPostProcessor.__call__(results_all_folds, fold_result, features, run, *args, **kwargs)
Postprocessing function for the random forest model. Logs the parameters to Neptune. Generates beeswarm plots from SHAP explainers for the training and test data and logs them to Neptune.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results_all_folds |
A dict of results for all folds |
required | |
fold_result |
A dataclass containing the results for the current fold |
required | |
run |
neptune run object |
required | |
*args |
any additional arguments |
()
|
|
**kwargs |
any additional keyword arguments |
{}
|
Returns:
Type | Description |
---|---|
dict
|
updated results dictionary |
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.SVRModelPostProcessor
Bases: ModelPostProcessor
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.SVRModelPostProcessor.__call__(results_all_folds, fold_result, features, run, *args, **kwargs)
Postprocessing function for the SVR model. Logs the parameters to Neptune. Logs permutation importance plots to Neptune.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results_all_folds |
A dict of results for all folds |
required | |
fold_result |
A dataclass containing the results for the current fold |
required | |
run |
neptune run object |
required | |
*args |
any additional arguments |
()
|
|
**kwargs |
any additional keyword arguments |
{}
|
Returns:
Type | Description |
---|---|
dict
|
updated results dictionary |
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.XGBoostModelPostProcessor
Bases: ModelPostProcessor
Source code in flexcv/model_postprocessing.py
flexcv.model_postprocessing.XGBoostModelPostProcessor.__call__(results_all_folds, fold_result, features, run, *args, **kwargs)
Postprocessing function for the xgboost model. Logs the parameters to Neptune. Generates beeswarm plots from SHAP explainers for the training and test data and logs them to Neptune.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results_all_folds |
A dict of results for all folds |
required | |
fold_result |
A dataclass containing the results for the current fold |
required | |
run |
neptune run object |
required | |
*args |
any additional arguments |
()
|
|
**kwargs |
any additional keyword arguments |
{}
|
Returns:
Type | Description |
---|---|
dict
|
updated results dictionary |