BaseTrainer
Bases: BaseValidator
, ABC
Abstract base class for training machine learning models.
This class provides foundational methods for training and evaluating machine learning models, including MLP models with early stopping, and optimizing decision thresholds. It supports binary and multiclass classification and allows for various evaluation metrics, threshold tuning, and cross-validation procedures.
Inherits
BaseValidator
: Validates instance level variables.ABC
: Specifies abstract methods for subclasses to implement.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
classification
|
str
|
Specifies the type of classification ('binary' or 'multiclass'). |
required |
criterion
|
str
|
Defines the performance criterion to optimize (e.g., 'f1' or 'brier_score'). |
required |
tuning
|
Optional[str]
|
Specifies the tuning method ('holdout' or 'cv') or None. |
required |
hpo
|
Optional[str]
|
Specifies the hyperparameter optimization method. |
required |
mlp_training
|
Optional[bool]
|
Flag to indicate if a separate MLP training procedure with early stopping is to be used. |
required |
threshold_tuning
|
Optional[bool]
|
Determines if threshold tuning is performed for binary classification when the criterion is "f1". |
required |
Attributes:
Name | Type | Description |
---|---|---|
classification |
str
|
Type of classification ('binary' or 'multiclass'). |
criterion |
str
|
Performance criterion to optimize ('f1', 'brier_score' or 'macro_f1'). |
tuning |
Optional[str]
|
Tuning method ('holdout' or 'cv') or None. |
hpo |
Optional[str]
|
Hyperparameter optimization method if specified. |
mlp_training |
Optional[bool]
|
Indicates if MLP training with early stopping is applied. |
threshold_tuning |
Optional[bool]
|
Specifies if threshold tuning is performed for binary classification when the criterion is 'f1'. |
Methods:
Name | Description |
---|---|
evaluate |
Determines model performance based on the specified classification criterion. |
optimize_threshold |
Utilizes cross-validation to optimize the decision threshold by aggregating probability predictions. |
evaluate_cv |
Evaluates a model on a training-validation fold based on the specified criterion, supporting cross-validation. |
Abstract Methods
train
: Trains the model with standard or custom logic depending on the specified learner type.train_mlp
: Trains an MLP model with early stopping and additional evaluation logic if required.train_final_model
: Trains the final model on the entire dataset, applying resampling, parallel processing, and specified sampling methods.
Source code in periomod/training/_basetrainer.py
17 18 19 20 21 22 23 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 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 |
|
__init__(classification, criterion, tuning, hpo, mlp_training, threshold_tuning)
¶
Initializes the Trainer with classification type and criterion.
Source code in periomod/training/_basetrainer.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
evaluate(y, probs, threshold=None)
¶
Evaluates model performance based on the classification criterion.
For binary or multiclass classification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y
|
ndarray
|
True labels for the validation data. |
required |
probs
|
ndarray
|
Probability predictions for each class. For binary classification, the probability for the positive class. For multiclass, a 2D array with probabilities. |
required |
threshold
|
bool
|
Flag for threshold tuning when tuning with F1. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Tuple |
Tuple[float, Optional[float]]
|
Score and optimal threshold (if for binary). For multiclass, only the score is returned. |
Source code in periomod/training/_basetrainer.py
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 |
|
evaluate_cv(model, fold, return_probs=False)
¶
Evaluates a model on a specific training-validation fold.
Based on a chosen performance criterion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Any
|
The machine learning model used for evaluation. |
required |
fold
|
tuple
|
A tuple containing two tuples: - The first tuple contains the training data (features and labels). - The second tuple contains the validation data (features and labels). Specifically, it is structured as ((X_train, y_train), (X_val, y_val)), where X_train and X_val are the feature matrices, and y_train and y_val are the target vectors. |
required |
return_probs
|
bool
|
Return predicted probabilities with score if True. |
False
|
Returns:
Name | Type | Description |
---|---|---|
Union |
Union[float, Tuple[float, ndarray, ndarray]]
|
The calculated score of the model on the validation data, and optionally the true labels and predicted probabilities. |
Source code in periomod/training/_basetrainer.py
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 |
|
optimize_threshold(model, outer_splits, n_jobs)
¶
Optimize the decision threshold using cross-validation.
Aggregates probability predictions across cross-validation folds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Any
|
The trained machine learning model. |
required |
outer_splits
|
List[Tuple]
|
List of ((X_train, y_train), (X_val, y_val)). |
required |
n_jobs
|
int
|
Number of parallel jobs to use for cross-validation. |
required |
Returns:
Name | Type | Description |
---|---|---|
Union |
Union[float, None]
|
The optimal threshold for 'f1', or None if the criterion is 'brier_score'. |
Source code in periomod/training/_basetrainer.py
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 |
|
train(model, X_train, y_train, X_val, y_val)
abstractmethod
¶
Trains either an MLP model with custom logic or a standard model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Any
|
The machine learning model to be trained. |
required |
X_train
|
DataFrame
|
Training features. |
required |
y_train
|
Series
|
Training labels. |
required |
X_val
|
DataFrame
|
Validation features. |
required |
y_val
|
Series
|
Validation labels. |
required |
Source code in periomod/training/_basetrainer.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
train_final_model(df, resampler, model, sampling, factor, n_jobs, seed, test_size, verbose)
abstractmethod
¶
Trains the final model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
The dataset used for model evaluation. |
required |
resampler
|
Resampler
|
Resampling class. |
required |
model
|
sklearn estimator
|
The machine learning model used for evaluation. |
required |
sampling
|
str
|
The type of sampling to apply. |
required |
factor
|
float
|
The factor by which to upsample or downsample. |
required |
n_jobs
|
int
|
The number of parallel jobs to run for evaluation. |
required |
seed
|
int
|
Seed for splitting. |
required |
test_size
|
float
|
Size of train test split. |
required |
verbose
|
bool
|
verbose during model evaluation process if set to True. |
required |
Source code in periomod/training/_basetrainer.py
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 |
|
train_mlp(mlp_model, X_train, y_train, X_val, y_val, final=False)
abstractmethod
¶
Trains MLPClassifier with early stopping and evaluates performance.
Applies evaluation for both binary and multiclass classification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mlp_model
|
MLPClassifier
|
The MLPClassifier to be trained. |
required |
X_train
|
DataFrame
|
Training features. |
required |
y_train
|
Series
|
Training labels. |
required |
X_val
|
DataFrame
|
Validation features. |
required |
y_val
|
Series
|
Validation labels. |
required |
final
|
bool
|
Flag for final model training. |
False
|
Source code in periomod/training/_basetrainer.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|