culebra.fitness_function.dataset_score.Accuracy class¶
- class Accuracy(training_data: Dataset, test_data: Dataset | None = None, cv_folds: int | None = None, index: int | None = None)¶
Bases:
DatasetScorerConstruct the fitness function.
If test_data are provided, the whole training_data are used to train. Otherwise, a k-fold cross-validation is applied.
- Parameters:
training_data (Dataset) – The training dataset
cv_folds (int) – The number of folds for k-fold cross-validation. If omitted,
_default_cv_foldsis used. Defaults toNoneindex (int) – Index of this objective when it is used for multi-objective fitness functions
- Raises:
RuntimeError – If the number of objectives is not 1
TypeError – If training_data or test_data is an invalid dataset
TypeError – If cv_folds is not an integer value
ValueError – If cv_folds is not positive
TypeError – If index is not an integer number
ValueError – If index is not positive
Class methods¶
- classmethod Accuracy.load(filename: str) Base¶
Load a serialized object from a file.
- Parameters:
filename (str) – The file name.
- Returns:
The loaded object
- Raises:
TypeError – If filename is not a valid file name
ValueError – If the filename extension is not
SERIALIZED_FILE_EXTENSION
Properties¶
- property Accuracy.cv_folds: int¶
Number of cross-validation folds.
- Return type:
- Setter:
Set a new value for the number of cross-validation folds
- Parameters:
value (int) – A positive integer value. If set to
None,_default_cv_foldsis assumed- Raises:
TypeError – If value is not an integer value
ValueError – If value is not positive
- property Accuracy.index: int¶
Objective index.
- Return type:
- Setter:
Set a new index
- Parameters:
value (int) – The new index. If set to
None,_default_indexis chosen- Raises:
TypeError – If value is not an integer number
ValueError – If value is a negative number
- property Accuracy.obj_thresholds: list[float]¶
Objective similarity thresholds.
- Return type:
- Setter:
Set new thresholds.
- Parameters:
values (float | Sequence[float]) – The new values. If only a single value is provided, the same threshold will be used for all the objectives. Different thresholds can be provided in a
Sequence. If set toNone, all the thresholds are set to_default_similarity_threshold- Raises:
TypeError – If neither a real number nor a
Sequenceof real numbers is providedValueError – If any value is negative
ValueError – If the length of the thresholds sequence does not match the number of objectives
- property Accuracy.obj_weights: tuple[int, ...]¶
Objective weights.
Maximize the validation accuracy.
Private properties¶
- property Accuracy._default_cv_folds: int¶
Default number of folds for cross-validation.
- Returns:
- Return type:
Methods¶
- Accuracy.dump(filename: str) None¶
Serialize this object and save it to a file.
- Parameters:
filename (str) – The file name.
- Raises:
TypeError – If filename is not a valid file name
ValueError – If the filename extension is not
SERIALIZED_FILE_EXTENSION
- Accuracy.evaluate(sol: Solution, index: int | None = None, representatives: Sequence[Solution] | None = None) Fitness¶
Evaluate a solution.
- Parameters:
sol (Solution) – Solution to be evaluated.
index (int) – Index where sol should be inserted in the representatives sequence to form a complete solution for the problem. Only used by cooperative problems
representatives (Sequence[Solution]) – Representative solutions of each species being optimized. Only used by cooperative problems
- Returns:
The fitness for sol
- Return type:
- Raises:
ValueError – If sol is not evaluable
Private methods¶
- abstract Accuracy._evaluate_kfcv(sol: Solution, training_data: Dataset) Fitness¶
Evaluate a solution.
A k-fold cross-validation is applied using the training_data with
cv_foldsfolds.This method must be overridden by subclasses to return a correct value.
- Parameters:
- Returns:
The fitness for sol
- Return type:
- Raises:
NotImplementedError – If has not been overridden
- abstract Accuracy._evaluate_train_test(sol: Solution, training_data: Dataset, test_data: Dataset) Fitness¶
Evaluate a solution.
This method must be overridden by subclasses to return a correct value.
- Parameters:
- Returns:
The fitness for sol
- Return type:
- Raises:
NotImplementedError – If has not been overridden
- Accuracy._final_training_test_data(sol: Solution) tuple[Dataset, Dataset]¶
Get the final training and test data.
- Accuracy._score(y_pred, *, normalize=True, sample_weight=None)¶
Use
accuracy_score()to score.

