culebra.fitness_function.feature_selection.KappaIndex class¶
- class KappaIndex(training_data: Dataset, test_data: Dataset | None = None, cv_folds: int | None = None, classifier: ClassifierMixin | None = None, index: int | None = None)¶
Bases:
FSClassificationScorer,KappaIndexConstruct 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 toNoneclassifier (ClassifierMixin) – The classifier. If omitted,
_default_classifierwill be used. Defaults toNoneindex (int) – Index of this objective when it is used for multi-objective fitness functions, optional
- 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 classifier is not a valid classifier
TypeError – If index is not an integer number
ValueError – If index is not positive
Class methods¶
- classmethod KappaIndex.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 KappaIndex.classifier: ClassifierMixin¶
Classifier applied within this fitness function.
- Return type:
- Setter:
Set a new classifier
- Parameters:
value (ClassifierMixin) – The classifier. If set to
None,_default_classifieris used- Raises:
TypeError – If value is not a valid classifier
- property KappaIndex.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 KappaIndex.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 KappaIndex.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 KappaIndex.obj_weights: tuple[int, ...]¶
Objective weights.
Maximize the validation Kappa index.
Private properties¶
- property KappaIndex._default_classifier: ClassifierMixin¶
Default classifier.
- Returns:
A Gaussian Naive Bayes classifier
- Return type:
- property KappaIndex._default_cv_folds: int¶
Default number of folds for cross-validation.
- Returns:
- Return type:
Methods¶
- KappaIndex.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
- KappaIndex.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¶
- KappaIndex._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.
- KappaIndex._evaluate_train_test(sol: Solution, training_data: Dataset, test_data: Dataset) Fitness¶
Evaluate a solution.
- KappaIndex._final_training_test_data(sol: Solution) tuple[Dataset, Dataset]¶
Get the final training and test data.
- KappaIndex._score(y2, *, labels=None, weights=None, sample_weight=None)¶
Use
cohen_kappa_score()to score.

