culebra.fitness_func.feature_selection.abc.FSDatasetScorer class

class FSDatasetScorer(training_data: Dataset, test_data: Dataset | None = None, cv_num_folds: int | None = None, cv_fixed_folds: bool | None = None, index: int | None = None)

Bases: DatasetScorer, FSScorer

Construct 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

  • test_data (Dataset) – The test dataset, defaults to None

  • cv_num_folds (int) – The number of folds for k-fold cross-validation. If omitted, _default_cv_num_folds is used. Defaults to None

  • cv_fixed_folds (bool) – If True, the same folds are used across all evaluations, ensuring deterministic results. If False, new folds are generated randomly for each evaluation, introducing variability in the results. If omitted, _default_cv_fixed_folds is used. Defaults to None

  • index (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_num_folds is not an integer value

  • ValueError – If cv_num_folds is not positive

  • TypeError – If cv_fixed_folds is not boolean

  • TypeError – If index is not an integer number

  • ValueError – If index is not positive

Class methods

classmethod FSDatasetScorer.load(filename: str) Base

Load a serialized object from a file.

Parameters:

filename (str) – The file name.

Returns:

The loaded object

Raises:

Properties

property FSDatasetScorer.cv_fixed_folds: bool

Whether the folds are fixed or not across evaluations.

Return type:

bool

Setter:

Set a new value for the fixed folds flag. If set to None, _default_cv_fixed_folds is assumed

Raises:

TypeError – If value is not boolean

property FSDatasetScorer.cv_num_folds: int

Number of cross-validation folds.

Return type:

int

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_num_folds is assumed

Raises:
property FSDatasetScorer.cv_splitter: StratifiedKFold

Return the dataset splitter.

Return type:

StratifiedKFold

property FSDatasetScorer.fitness_cls: type[Fitness]

Fitness class.

Return type:

type[Fitness]

property FSDatasetScorer.index: int

Objective index.

Return type:

int

Setter:

Set a new index

Parameters:

value (int) – The new index. If set to None, _default_index is chosen

Raises:
property FSDatasetScorer.num_obj: int

Number of objectives.

Return type:

int

property FSDatasetScorer.obj_names: tuple[str, ...]

Objective names.

Return type:

tuple[str]

property FSDatasetScorer.obj_thresholds: tuple[float]

Objective similarity thresholds.

Return type:

tuple[float]

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 to None, all the thresholds are set to _default_similarity_threshold

Raises:
  • TypeError – If neither a real number nor a Sequence of real numbers is provided

  • ValueError – If any value is negative

  • ValueError – If the length of the thresholds sequence does not match the number of objectives

abstract property FSDatasetScorer.obj_weights: tuple[int, ...]

Objective weights.

This property must be overridden by subclasses to return a correct value.

Return type:

tuple[int]

Raises:

NotImplementedError – If has not been overridden

property FSDatasetScorer.objectives: tuple[SingleObjectiveFitnessFunction]

Objectives to be optimized.

Return type:

tuple[SingleObjectiveFitnessFunction]

property FSDatasetScorer.test_data: Dataset | None

Test dataset.

If set to None, a k-fold cross-validation is applied.

Return type:

Dataset

Setter:

Set a new test dataset

Parameters:

value (Dataset) – The new test dataset

Raises:

TypeError – If set to an invalid dataset

property FSDatasetScorer.training_data: Dataset

Training dataset.

Return type:

Dataset

Setter:

Set a new training dataset

Parameters:

value (Dataset) – The new training dataset

Raises:

TypeError – If set to an invalid dataset

Private properties

property FSDatasetScorer._default_cv_fixed_folds: bool

Default value for the fixed folds flag.

Returns:

DEFAULT_CV_FIXED_FOLDS

Return type:

bool

property FSDatasetScorer._default_cv_num_folds: int

Default number of folds for cross-validation.

Returns:

DEFAULT_CV_NUM_FOLDS

Return type:

int

property FSDatasetScorer._default_index: int

Default index.

Returns:

DEFAULT_INDEX

Return type:

int

property FSDatasetScorer._default_similarity_threshold: float

Default similarity threshold for fitnesses.

Returns:

DEFAULT_SIMILARITY_THRESHOLD

Return type:

float

abstract property FSDatasetScorer._worst_score: float

Worst achievable score.

This property must be overridden by subclasses to return a correct value.

Return type:

float

Raises:

NotImplementedError – If has not been overridden

Methods

FSDatasetScorer.dump(filename: str) None

Serialize this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:
FSDatasetScorer.evaluate(sol: Solution, index: int | None = None, cooperators: Sequence[Solution | None] | None = None) tuple[float, ...]

Evaluate a solution.

Neither the solution nor its fitness should be modified.

Parameters:
  • sol (Solution) – Solution to be evaluated.

  • index (int) – Index where sol should be inserted in the cooperators sequence to form a complete solution for the problem. Only used by cooperative problems

  • cooperators (Sequence[Solution]) – Cooperators of each species being optimized. Only used by cooperative problems

Returns:

The fitness values for sol

Return type:

tuple[float, …]

Raises:

ValueError – If sol is not evaluable

FSDatasetScorer.is_evaluable(sol: Solution) bool

Assess the evaluability of a solution.

Parameters:

sol (Solution) – Solution to be evaluated.

Returns:

True if the solution can be evaluated

Return type:

bool

Raises:

NotImplementedError – If has not been overridden

Private methods

abstract FSDatasetScorer._evaluate_kfcv(sol: Solution, training_data: Dataset) tuple[float, ...]

Evaluate a solution.

A k-fold cross-validation is applied using the training_data with cv_num_folds folds.

Neither the solution nor its fitness should be modified.

This method must be overridden by subclasses to return a correct value.

Parameters:
  • sol (Solution) – Solution to be evaluated.

  • training_data (Dataset) – The training dataset

Returns:

The fitness values for sol

Return type:

tuple[float, …]

Raises:

NotImplementedError – If has not been overridden

abstract FSDatasetScorer._evaluate_train_test(sol: Solution, training_data: Dataset, test_data: Dataset) tuple[float, ...]

Evaluate a solution.

Neither the solution nor its fitness should be modified.

This method must be overridden by subclasses to return a correct value.

Parameters:
  • sol (Solution) – Solution to be evaluated.

  • training_data (Dataset) – The training dataset

  • test_data (Dataset) – The test dataset

Returns:

The fitness values for sol

Return type:

tuple[float, …]

Raises:

NotImplementedError – If has not been overridden

FSDatasetScorer._final_training_test_data(sol: Solution) tuple[Dataset, Dataset]

Get the final training and test data.

Parameters:

sol (Solution) – Solution to be evaluated. It is used to select the features from the datasets

Returns:

The final training and test datasets

Return type:

tuple[Dataset]

FSDatasetScorer._get_repr_properties() dict[str, object]

Return the subset of properties used for __repr__.

Filters and evaluates all class-level @property attributes, returning only those intended for representation purposes. Private properties (names starting with _) are excluded.

Returns:

Mapping of property names to their corresponding values.

Return type:

dict[str, object]

abstract static FSDatasetScorer._score(outputs: Sequence[float], outputs_pred: Sequence[float], **kwargs: dict) float

Score function to be used in the evaluation.

This method must be overridden by subclasses to return a correct value.