flexcv.results_handling
This module provides a CrossValidationResults class which can be used to summarize the results of CrossValidation.perform().
flexcv.results_handling.CrossValidationResults
Bases: dict
A summary of the results of CrossValidation.perform(). Cross validate returns a dictionary of results for each model with the form:
{
"model_name_1": {
"model": [model_1_fold_1, model_1_fold_2, ...],
"parameters": [params_1_fold_1, params_1_fold_2, ...],
"metrics": [
{
"metric_1_fold_1": metric_value_1_fold_1,
"metric_2_fold_1": metric_value_2_fold_1,
...
},
{
"metric_1_fold_2": metric_value_1_fold_2,
"metric_2_fold_2": metric_value_2_fold_2,
...
},
],
"y_pred": [y_pred_1_fold_1, y_pred_1_fold_2, ...],
"y_test": [y_test_1_fold_1, y_test_1_fold_2, ...],
"y_pred_train": [y_pred_train_1_fold_1, y_pred_train_1_fold_2, ...],
"y_train": [y_train_1_fold_1, y_train_1_fold_2, ...],
},
"model_name_2": {
...
},
...
}
_make_summary
computes the mean, median and standard deviation of the metrics for each model._make_summary
is called the first time the summary property is accessed and the result is cached._get_model
returns the model instance corresponding to the given model name.
Properties
summary (pd.DataFrame): Summary of the results.
Source code in flexcv/results_handling.py
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 |
|
flexcv.results_handling.CrossValidationResults.summary
property
Property that returns a pandas dataframe with the fold values, mean, median and standard deviation of the metrics for each model.
flexcv.results_handling.CrossValidationResults.get_best_model_by_metric(model_name=None, metric_name='mse', direction='min')
Returns the model with the best metric value for the given metric. Direction can be "min" or "max" and determines whether the best model is the one with the lowest or highest metric value. E.g. for MSE, direction should be "min" and for R2, direction should be "max".
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name |
str
|
Name of the model (Default value = None) |
None
|
metric_name |
str
|
Name for the metric (Default value = "mse") |
'mse'
|
direction |
str
|
Minimize or maximize. (Default value = "min") |
'min'
|
Returns:
Type | Description |
---|---|
object
|
The model with the best metric value for the given metric. |
Source code in flexcv/results_handling.py
flexcv.results_handling.CrossValidationResults.get_params(model_name, fold_id)
Returns the parameters for the given model and fold. If the key is not found, returns None and will not raise an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name |
str
|
The name of the model. |
required |
fold_id |
int
|
The id of the fold. |
required |
Returns:
Type | Description |
---|---|
dict
|
The parameters for the given model and fold. |
Source code in flexcv/results_handling.py
flexcv.results_handling.CrossValidationResults.get_predictions(model_name, fold_id)
Returns the predictions for the given model and fold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name |
str
|
The name of the model. |
required |
fold_id |
int
|
The id of the fold. |
required |
Returns:
Type | Description |
---|---|
array - like
|
The predictions for the given model and fold. |
Source code in flexcv/results_handling.py
flexcv.results_handling.CrossValidationResults.get_training_predictions(model_name, fold_id)
Returns the predictions for the given model and fold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name |
str
|
The name of the model. |
required |
fold_id |
int
|
The id of the fold. |
required |
Returns:
Type | Description |
---|---|
array - like
|
The training predictions for the given model and fold. |
Source code in flexcv/results_handling.py
flexcv.results_handling.CrossValidationResults.get_training_true_values(model_name, fold_id)
Returns the true values for the given model and fold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name |
str
|
The name of the model. |
required |
fold_id |
int
|
The id of the fold. |
required |
Returns:
Type | Description |
---|---|
array - like
|
The training true values for the given model and fold. |
Source code in flexcv/results_handling.py
flexcv.results_handling.CrossValidationResults.get_true_values(model_name, fold_id)
Returns the true values for the given model and fold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name |
str
|
The name of the model. |
required |
fold_id |
int
|
The id of the fold. |
required |
Returns:
Type | Description |
---|---|
array - like
|
The true values for the given model and fold. |
Source code in flexcv/results_handling.py
flexcv.results_handling.MergedSummary
Bases: CrossValidationResults
Source code in flexcv/results_handling.py
flexcv.results_handling.MergedSummary.summary
property
flexcv.results_handling.add_summary_stats(df)
Add summary statistics to a pandas DataFrame. Calculates the mean, median and standard deviation on copies slices of the original data and adds them as rows to the DataFrame. This makes sure, that the summary statistics are not sequentially dependent on each other.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
pd.DataFrame: Input data. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
Data with added summary statistics |
Source code in flexcv/results_handling.py
flexcv.fold_results_handling
flexcv.fold_results_handling.SingleModelFoldResult
dataclass
This dataclass is used to store the fold data as well as the predictions of a single model in a single fold. It's make_results method is used to evaluate the model with the metrics and log the results to Neptune.
Attributes:
Name | Type | Description |
---|---|---|
k |
int
|
The fold number. |
model_name |
str
|
The name of the model. |
best_model |
object
|
The best model after inner cv or the model when skipping inner cv. |
best_params |
dict | Any
|
The best parameters. |
y_pred |
Series
|
The predictions of the model. |
y_test |
Series
|
The test data. |
X_test |
DataFrame
|
The test data. |
y_train |
Series
|
The train data. |
y_pred_train |
Series
|
The predictions of the model. |
X_train |
DataFrame
|
The train data. |
fit_result |
Any
|
The result of the fit method of the model. |
fit_kwargs |
dict
|
Additional keyword arguments to pass to the fit method. (default: None) |
Source code in flexcv/fold_results_handling.py
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 |
|
flexcv.fold_results_handling.SingleModelFoldResult.make_results(run, results_all_folds, study, metrics=METRICS)
This method is used to evaluate the model with the metrics and log the results to Neptune.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
run |
run
|
Neptune run object. |
required |
results_all_folds |
dict
|
Dictionary containing the results of all models and folds. |
required |
study |
study
|
Optuna study object. |
required |
metrics |
dict
|
Dictionary containing the metrics to be evaluated. |
METRICS
|
Returns:
Type | Description |
---|---|
dict
|
Dictionary containing the results of all models and folds. |
Source code in flexcv/fold_results_handling.py
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 |
|