flexcv.repeated
flexcv.repeated.RepeatedCV
Bases: CrossValidation
Class for repeated cross-validation. Inherits from CrossValidation.
Attributes:
Name | Type | Description |
---|---|---|
n_repeats |
int
|
Number of repeated runs. (Default value = 3) |
seeds |
list[int]
|
List of seeds for the repeated runs. (Default value = None) |
generator_seed |
int
|
Seed to control generation of a list of seeds. (Default value = 42) |
parent_run |
run
|
A run to track the repeated meta data. (Default value = None) |
children_runs |
list[run]
|
A list of runs to track the single runs. (Default value = None) |
Methods:
Name | Description |
---|---|
set_n_repeats |
method that sets the number of repeated runs |
set_seeds |
method that sets the random seeds for the repeated runs |
set_neptune |
method that sets the neptune credentials for the repeated runs and initializes them |
set_run |
in contrast to CrossValidation, the set_run method takes in a parent run and a list of children runs |
perform |
method that performs repeated cross-validation |
Example
from flexcv.synthesizer import generate_regression
from flexcv.models import LinearModel
from flexcv.model_mapping import ModelConfigDict, ModelMappingDict
from flexcv.repeated import RepeatedCV
# make sample data
X, y, group, random_slopes = generate_regression(10, 100, n_slopes=1, noise_level=9.1e-2, random_seed=42), random_seed=42
# create a model mapping
model_map = ModelMappingDict(
{
"LinearModel": ModelConfigDict(
{
"model": LinearModel,
"requires_formula": True,
}
),
}
)
credentials = {your_neptune_credentials}
rcv = (
RepeatedCV()
.set_data(X, y, group, dataset_name="ExampleData")
.set_models(model_map)
.set_n_repeats(3)
.set_neptune(credentials)
.perform()
.get_results()
)
rcv.summary.to_excel("repeated_cv.xlsx") # save dataframe to excel file
Source code in flexcv/repeated.py
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 |
|
flexcv.repeated.RepeatedCV.get_results()
Returns the results of repeated cross-validation.
Returns:
Type | Description |
---|---|
RepeatedResult
|
RepeatedResult object with summary property that returns a DataFrame with aggregated metrics. |
Source code in flexcv/repeated.py
flexcv.repeated.RepeatedCV.perform()
Wrapper method to perform repeated cross-validation. Overwrites the perform method of CrossValidation.
Returns:
Type | Description |
---|---|
RepeatedCV
|
self |
Source code in flexcv/repeated.py
flexcv.repeated.RepeatedCV.set_n_repeats(n_repeats)
Set the number of repeats for repeated cross-validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_repeats(int) |
Number of repeated runs. |
required |
Returns:
Type | Description |
---|---|
RepeatedCV
|
self |
flexcv.repeated.RepeatedCV.set_neptune(neptune_credentials)
Set your neptune credentials and initialize the repeated runs automatically. If you do not want to pass your credentials explicitly, use set_run and call init_repeated_runs from outside the class yourself.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
neptune_credentials |
dict
|
Your API token and project name. |
required |
Returns:
Type | Description |
---|---|
RepeatedCV
|
self |
Source code in flexcv/repeated.py
flexcv.repeated.RepeatedCV.set_run(parent_run=None, children_run=None)
Use this method if you want to pass your own parent run and children runs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parent_run |
run
|
A run to track the repeated meta data. (Default value = None) |
None
|
children_run |
list[run]
|
A list of runs to track the single runs. (Default value = None) |
None
|
Returns:
Type | Description |
---|---|
RepeatedCV
|
self |
Source code in flexcv/repeated.py
flexcv.repeated.RepeatedCV.set_seeds(seeds=None, generator_seed=42)
Set the seeds for the repeated runs. If no seeds are passed, a list of seeds is generated randomly. The random seed for the random seed generation can be set via the generator_seed argument. Therefore, the random seed for the repeated runs is set to the same value for each repeated run and is deterministic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seeds |
list[int]
|
The list of seeds to use in the repeats. (Default value = None) |
None
|
generator_seed |
int
|
Seed to control generation of a list of seeds. (Default value = 42) |
42
|
Returns:
Type | Description |
---|---|
RepeatedCV
|
self |
Source code in flexcv/repeated.py
flexcv.repeated.RepeatedResult
Class for results of repeated cross-validation. Implements a summary property that returns a DataFrame with aggregated metrics.
Source code in flexcv/repeated.py
flexcv.repeated.RepeatedResult.summary
property
Summary property that returns a DataFrame with aggregated metrics.
Returns:
Type | Description |
---|---|
DataFrame
|
DataFrame with aggregated metrics. |
flexcv.repeated.RepeatedResult.__init__(df)
Constructor method for RepeatedResult class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df(pd.DataFrame) |
DataFrame with aggregated metrics. |
required |
flexcv.repeated.aggregate_(repeated_runs)
Aggregate the results of repeated runs into a single DataFrame. Therefore, the nested dict structure of the results is flattened. First, the model results are averaged over folds of individual runs. Second, the repeated (individual) runs are averaged.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repeated_runs |
list: List of results of repeated runs. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
Summary statistics of repeated runs. |
Note:
The summary statistics are returned as a DataFrame with the following structure:
- index: [aggregate]_[metric_name]
- columns: [model_name]
- values: [metric_value]
Source code in flexcv/repeated.py
flexcv.repeated.init_repeated_runs(n_repeats, neptune_credentials)
Initialize a repeated run and n_repeats children runs. The children runs are linked to the parent run via the "HostRun" key. The children runs are initialized with the same credentials as the parent run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_repeats |
int
|
Number of repeated runs. |
required |
use_neptune |
bool
|
Whether to use neptune or not. |
required |
neptune_credentials |
dict
|
Credentials for neptune. |
required |
Returns:
Type | Description |
---|---|
tuple[Run, list[Run]]
|
parent_run (Run), children_runs (list of n_repeats runs) |
Source code in flexcv/repeated.py
flexcv.repeated.try_mean(x)
Mean function that handles NaN values for cases where the input contains string "NaN". If all elements in x are equal to "NaN", -99 is returned. If x contains "NaN" and other values, -999 is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
(array-like): Input array. |
required |
Returns:
Type | Description |
---|---|
float
|
Mean of the input array. |