55# --------------------------------------------------%
66
77import numpy as np
8- from mealpy .optimizer import Optimizer
9- from mealpy import *
8+ from mealpy import Problem
109
1110
1211class FeatureSelectionProblem (Problem ):
13- def __init__ (self , minmax = None , data = None , estimator = None , metric_class = None ,
14- obj_name = None , obj_paras = None , fit_weights = (0.9 , 0.1 ), fit_sign = None ,
15- transfer_func = "vstf_01" , ** kwargs ):
16- bounds = TransferBinaryVar (n_vars = data .X .shape [1 ], tf_func = transfer_func , lb = - 8 , ub = 8 , all_zeros = False , name = "my_var" )
12+ """
13+ A class to define a feature selection optimization problem.
14+
15+ Attributes
16+ ----------
17+ data : object
18+ An object containing training and testing datasets.
19+ estimator : object
20+ A machine learning model with `fit` and `predict` methods.
21+ metric_class : object
22+ A class used to evaluate the performance of the model.
23+ obj_name : str
24+ The name of the objective metric to optimize.
25+ obj_paras : dict
26+ Parameters for the objective metric.
27+ fit_weights : tuple
28+ Weights for combining the objective value and feature selection ratio.
29+ fit_sign : int
30+ Sign to determine the direction of optimization (e.g., 1 for maximization, -1 for minimization).
31+
32+ Methods
33+ -------
34+ obj_func(solution)
35+ Computes the fitness, objective value, and number of selected features for a given solution.
36+ """
37+ def __init__ (self , bounds = None , minmax = None , data = None , estimator = None , metric_class = None ,
38+ obj_name = None , obj_paras = None , fit_weights = (0.9 , 0.1 ), fit_sign = None , ** kwargs ):
1739 super ().__init__ (bounds , minmax , ** kwargs )
1840 self .data = data
1941 self .estimator = estimator
@@ -25,11 +47,24 @@ def __init__(self, minmax=None, data=None, estimator=None, metric_class=None,
2547 self .fit_sign = fit_sign
2648
2749 def obj_func (self , solution ):
28- x = self .decode_solution (solution )["my_var" ]
50+ """
51+ Computes the fitness, objective value, and number of selected features for a given solution.
52+
53+ Parameters
54+ ----------
55+ solution : array-like
56+ The solution representing selected features.
57+
58+ Returns
59+ -------
60+ list
61+ A list containing the fitness value, objective value, and number of selected features.
62+ """
63+ x = self .decode_solution (solution )["my_var" ] # Please don't change this line, it is used to decode the solution
2964 cols = np .flatnonzero (x )
3065 self .estimator .fit (self .data .X_train [:, cols ], self .data .y_train )
3166 y_valid_pred = self .estimator .predict (self .data .X_test [:, cols ])
3267 evaluator = self .metric_class (self .data .y_test , y_valid_pred )
3368 obj = evaluator .get_metric_by_name (self .obj_name , paras = self .obj_paras )[self .obj_name ]
3469 fitness = self .fit_weights [0 ]* obj + self .fit_weights [1 ]* self .fit_sign * (np .sum (x ) / self .n_dims )
35- return [fitness , obj , np .sum (x )]
70+ return [fitness , obj , np .sum (x )] # Return fitness, objective value, and number of selected features
0 commit comments