eckity.sklearn_compatible.regression_evaluator
This module implements the fitness evaluation class, which delivers the fitness function. You will need to implement such a class to work with your own problem and fitness function.
1""" 2This module implements the fitness evaluation class, which delivers the fitness function. 3You will need to implement such a class to work with your own problem and fitness function. 4""" 5 6from sklearn.metrics import mean_absolute_error 7 8from eckity.evaluators.simple_individual_evaluator import SimpleIndividualEvaluator 9 10 11class RegressionEvaluator(SimpleIndividualEvaluator): 12 """ 13 Computes the fitness of an individual in regression problems. 14 15 Parameters 16 ---------- 17 X: array-like of shape (n_samples, n_features), default=None 18 Training/Test data. 19 20 y: array-like of shape (n_samples,) or (n_samples, 1), default=None 21 Target vector. used during the training phase. 22 23 metric: callable (optional, default=mean_absolute_error) 24 A function which receives two array-like of shapes (n_samples,) or (n_samples, 1) and returns a float or 25 ndarray of floats 26 """ 27 28 def __init__(self, X=None, y=None, metric=mean_absolute_error): 29 super().__init__() 30 self.X = X 31 self.y = y 32 self.metric = metric 33 34 def set_context(self, context): 35 """ 36 Receive X and y values and assign them to X and y fields. 37 38 Parameters 39 ---------- 40 context: tuple. first element is a numpy array of size (n_samples, n_features), 41 and the second element is a numpy array of size (n_samples, 1) or (n_samples,) 42 X matrix and y vector, either (X_train, y_train) or (X_test, None), depending on the evolution stage 43 44 Returns 45 ------- 46 None. 47 48 Examples 49 ------- 50 reg_eval = RegressionEvaluator() 51 X, y = make_regression() 52 X_train, X_test, y_train, y_test = train_test_split() 53 reg_eval.set_context(X_train, y_train) 54 """ 55 self.X = context[0] 56 self.y = context[1] 57 58 def evaluate_individual(self, individual): 59 """ 60 compute fitness value by computing the MAE between program tree execution result and y result vector 61 62 Parameters 63 ---------- 64 individual : Tree 65 An individual program tree in the GP population, whose fitness needs to be computed. 66 Makes use of GPTree.execute, which runs the program. 67 In Sklearn settings, calling `individual.execute` must use a numpy array. 68 For example, if self.X is X_train/X_test, the call is `individual.execute(self.X)`. 69 70 Returns 71 ---------- 72 float 73 Computed fitness value - evaluated using the provided scoring function between the execution result of X and 74 the vector y. 75 """ 76 return self.metric(self.y, individual.execute(self.X))
12class RegressionEvaluator(SimpleIndividualEvaluator): 13 """ 14 Computes the fitness of an individual in regression problems. 15 16 Parameters 17 ---------- 18 X: array-like of shape (n_samples, n_features), default=None 19 Training/Test data. 20 21 y: array-like of shape (n_samples,) or (n_samples, 1), default=None 22 Target vector. used during the training phase. 23 24 metric: callable (optional, default=mean_absolute_error) 25 A function which receives two array-like of shapes (n_samples,) or (n_samples, 1) and returns a float or 26 ndarray of floats 27 """ 28 29 def __init__(self, X=None, y=None, metric=mean_absolute_error): 30 super().__init__() 31 self.X = X 32 self.y = y 33 self.metric = metric 34 35 def set_context(self, context): 36 """ 37 Receive X and y values and assign them to X and y fields. 38 39 Parameters 40 ---------- 41 context: tuple. first element is a numpy array of size (n_samples, n_features), 42 and the second element is a numpy array of size (n_samples, 1) or (n_samples,) 43 X matrix and y vector, either (X_train, y_train) or (X_test, None), depending on the evolution stage 44 45 Returns 46 ------- 47 None. 48 49 Examples 50 ------- 51 reg_eval = RegressionEvaluator() 52 X, y = make_regression() 53 X_train, X_test, y_train, y_test = train_test_split() 54 reg_eval.set_context(X_train, y_train) 55 """ 56 self.X = context[0] 57 self.y = context[1] 58 59 def evaluate_individual(self, individual): 60 """ 61 compute fitness value by computing the MAE between program tree execution result and y result vector 62 63 Parameters 64 ---------- 65 individual : Tree 66 An individual program tree in the GP population, whose fitness needs to be computed. 67 Makes use of GPTree.execute, which runs the program. 68 In Sklearn settings, calling `individual.execute` must use a numpy array. 69 For example, if self.X is X_train/X_test, the call is `individual.execute(self.X)`. 70 71 Returns 72 ---------- 73 float 74 Computed fitness value - evaluated using the provided scoring function between the execution result of X and 75 the vector y. 76 """ 77 return self.metric(self.y, individual.execute(self.X))
Computes the fitness of an individual in regression problems.
Parameters
X (array-like of shape (n_samples, n_features), default=None):
Training/Test data.
y (array-like of shape (n_samples,) or (n_samples, 1), default=None):
Target vector. used during the training phase.
metric (callable (optional, default=mean_absolute_error)):
A function which receives two array-like of shapes (n_samples,) or (n_samples, 1) and returns a float or
- ndarray of floats
35 def set_context(self, context): 36 """ 37 Receive X and y values and assign them to X and y fields. 38 39 Parameters 40 ---------- 41 context: tuple. first element is a numpy array of size (n_samples, n_features), 42 and the second element is a numpy array of size (n_samples, 1) or (n_samples,) 43 X matrix and y vector, either (X_train, y_train) or (X_test, None), depending on the evolution stage 44 45 Returns 46 ------- 47 None. 48 49 Examples 50 ------- 51 reg_eval = RegressionEvaluator() 52 X, y = make_regression() 53 X_train, X_test, y_train, y_test = train_test_split() 54 reg_eval.set_context(X_train, y_train) 55 """ 56 self.X = context[0] 57 self.y = context[1]
Receive X and y values and assign them to X and y fields.
Parameters
- context (tuple. first element is a numpy array of size (n_samples, n_features),): and the second element is a numpy array of size (n_samples, 1) or (n_samples,) X matrix and y vector, either (X_train, y_train) or (X_test, None), depending on the evolution stage
Returns
- None.
Examples
reg_eval = RegressionEvaluator() X, y = make_regression() X_train, X_test, y_train, y_test = train_test_split() reg_eval.set_context(X_train, y_train)
59 def evaluate_individual(self, individual): 60 """ 61 compute fitness value by computing the MAE between program tree execution result and y result vector 62 63 Parameters 64 ---------- 65 individual : Tree 66 An individual program tree in the GP population, whose fitness needs to be computed. 67 Makes use of GPTree.execute, which runs the program. 68 In Sklearn settings, calling `individual.execute` must use a numpy array. 69 For example, if self.X is X_train/X_test, the call is `individual.execute(self.X)`. 70 71 Returns 72 ---------- 73 float 74 Computed fitness value - evaluated using the provided scoring function between the execution result of X and 75 the vector y. 76 """ 77 return self.metric(self.y, individual.execute(self.X))
compute fitness value by computing the MAE between program tree execution result and y result vector
Parameters
- individual (Tree):
An individual program tree in the GP population, whose fitness needs to be computed.
Makes use of GPTree.execute, which runs the program.
In Sklearn settings, calling
individual.execute
must use a numpy array. For example, if self.X is X_train/X_test, the call isindividual.execute(self.X)
.
Returns
- float: Computed fitness value - evaluated using the provided scoring function between the execution result of X and the vector y.