culebra.fitness_function.cooperative.KappaNumFeatsC
class¶
- class KappaNumFeatsC(training_data: Dataset, test_data: Dataset | None = None, test_prop: float | None = None, cv_folds: int | None = None, classifier: ClassifierMixin | None = None)¶
Construct a fitness function.
If test_data are provided, the whole training_data are used to train. Otherwise, if test_prop is provided, training_data are split (stratified) into training and test data each time
evaluate()
is called and a Monte Carlo cross validation is applied. Finally, if both test_data and test_prop are omitted, a k-fold cross-validation is applied.- Parameters:
training_data (
Dataset
) – The training datasettest_data (
Dataset
, optional) – The test dataset, defaults toNone
test_prop (
float
, optional) – A real value in (0, 1) orNone
. Defaults toNone
cv_folds (
int
, optional) – The number of folds for k-fold cross-validation. If omitted,DEFAULT_CV_FOLDS
is used. Defaults toNone
classifier (
SVC
with RBF kernels, optional) – The classifier. If set toNone
, aSVC
with RBF kernels will be used. Defaults toNone
More information about this fitness function can be found in [Gonzalez2021].
Class attributes¶
- class culebra.fitness_function.cooperative.KappaNumFeatsC.Fitness¶
Handles the values returned by the
evaluate()
method within aSolution
.- Fitness.weights = (1.0, -1.0, -1.0)¶
Maximizes the Kohen’s Kappa index and minimizes the number of features that a solution has selected and also de C regularization hyperparameter.
- Fitness.names = ('Kappa', 'NF', 'C')¶
Name of the objectives.
- Fitness.thresholds = [0, 0, 0]¶
Similarity threshold for fitness comparisons.
Class methods¶
- classmethod KappaNumFeatsC.load_pickle(filename: str) Base ¶
Load a pickled object from 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
PICKLE_FILE_EXTENSION
- classmethod KappaNumFeatsC.set_fitness_thresholds(thresholds: float | Sequence[float]) None ¶
Set new fitness thresholds.
Modifies the
thresholds
of theFitness
objects generated by this fitness function.- Parameters:
thresholds (
float
orSequence
offloat
) – The new thresholds. If only a single value is provided, the same threshold will be used for all the objectives. Different thresholds can be provided in aSequence
- Raises:
TypeError – If thresholds is not a real number or a
Sequence
of real numbersValueError – If any threshold is negative
- classmethod KappaNumFeatsC.get_fitness_objective_threshold(obj_name: str) None ¶
Get the similarity threshold for the given objective.
- Parameters:
obj_name (
str
) – Objective name whose threshold is returned- Raises:
TypeError – If obj_name isn’t a string
ValueError – If value isn’t a valid objective name
- classmethod KappaNumFeatsC.set_fitness_objective_threshold(obj_name: str, value: float) None ¶
Set a similarity threshold for the given fitness objective.
- Parameters:
- Raises:
TypeError – If obj_name isn’t a string or value isn’t a real number
ValueError – If obj_name isn’t a valid objective name or value is lower than 0
Properties¶
- property KappaNumFeatsC.num_nodes: int¶
Return the problem graph’s number of nodes for ACO-based trainers.
- Returns:
The problem graph’s number of nodes
- Return type:
- property KappaNumFeatsC.test_prop: float | None¶
Get and set the proportion of data used to test.
- Getter:
Return the test data proportion
- Setter:
Set a new value for the test data porportion. A real value in (0, 1) or
None
is expected- Type:
- Raises:
TypeError – If set to a value which is not a real number
ValueError – If set to a value which is not in (0, 1)
- property KappaNumFeatsC.classifier: SVC¶
Get and set the classifier applied within this fitness function.
- Getter:
Return the classifier
- Setter:
Set a new classifier
- Type:
Any subclass of
SVC
with RBF kernels- Raises:
ValueError – If the classifier has not RBF kernels
Private properties¶
Methods¶
- KappaNumFeatsC.save_pickle(filename: str) None ¶
Pickle 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
PICKLE_FILE_EXTENSION
- KappaNumFeatsC.heuristic(species: Species) Sequence[ndarray, ...] ¶
Get the heuristic matrices for ACO-based trainers.
- Parameters:
species (
Species
) – Species constraining the problem solutions- Raises:
- Returns:
A tuple with only one heuristic matrix. Arcs between selectable features have a heuristic value of 1, while arcs involving any non-selectable feature or arcs from a feature to itself have a heuristic value of 0.
- Return type:
- KappaNumFeatsC.evaluate(sol: Solution, index: int | None = None, representatives: Sequence[Solution] | None = None) Tuple[float, ...] ¶
Evaluate a solution.
This fitness function assumes that:
representatives[0]: Codes the SVC hyperparameters (C and gamma). Thus, it is an instance of
culebra.solution.parameter_optimization.Solution
representatives[1:]: The remaining solutions code the features selected, each solution a different range of features. All of them are instances of
culebra.solution.feature_selection.Solution
- Parameters:
- Returns:
The fitness of sol
- Return type:
tuple
of py:class:float
- KappaNumFeatsC.construct_solutions(sol: Solution, index: int | None = None, representatives: Sequence[Solution] | None = None) Tuple[Solution, ...] ¶
Assemble the solution and representatives.
This fitness function assumes that:
representatives[0]: Codes the SVC hyperparameters (C and gamma). Thus, it is an instance of
culebra.solution.parameter_optimization.Solution
representatives[1:]: The remaining solutions code the features selected, each solution a different range of features. All of them are instances of
culebra.solution.feature_selection.Solution
- Parameters:
- Returns:
The solutions to the different problems solved cooperatively
- Return type:
tuple
of py:class:culebra.abc.Solution