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

Computes the fitness of an individual in regression problems. All simple classes assume only one sub-population.

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
RegressionEvaluator(X=None, y=None, metric=<function mean_absolute_error>)
31    def __init__(self, X=None, y=None, metric=mean_absolute_error):
32        super().__init__()
33        self.X = X
34        self.y = y
35        self.metric = metric
X
y
metric
def set_context(self, context):
37    def set_context(self, context):
38        """
39        Receive X and y values and assign them to X and y fields.
40
41        Parameters
42        ----------
43        context: tuple. first element is a numpy array of size (n_samples, n_features),
44                        and the second element is a numpy array of size (n_samples, 1) or (n_samples,)
45            X matrix and y vector, either (X_train, y_train) or (X_test, None), depending on the evolution stage
46
47        Returns
48        -------
49        None.
50
51        Examples
52        -------
53        reg_eval = RegressionEvaluator()
54        X, y = make_regression()
55        X_train, X_test, y_train, y_test = train_test_split()
56        reg_eval.set_context(X_train, y_train)
57        """
58        self.X = context[0]
59        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)

def evaluate_individual(self, individual):
61    def evaluate_individual(self, individual):
62        """
63        compute fitness value by computing the MAE between program tree execution result and y result vector
64
65        Parameters
66        ----------
67        individual : Tree
68            An individual program tree in the GP population, whose fitness needs to be computed.
69            Makes use of GPTree.execute, which runs the program.
70            In Sklearn settings, calling `individual.execute` must use a numpy array.
71            For example, if self.X is X_train/X_test, the call is `individual.execute(self.X)`.
72
73        Returns
74        ----------
75        float
76            Computed fitness value - evaluated using the provided scoring function between the execution result of X and
77            the vector y.
78        """
79        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 is individual.execute(self.X).
Returns
  • float: Computed fitness value - evaluated using the provided scoring function between the execution result of X and the vector y.