eckity.sklearn_compatible.classification_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"""
 5import numpy as np
 6from sklearn.metrics import accuracy_score
 7
 8from eckity.evaluators.simple_individual_evaluator import SimpleIndividualEvaluator
 9
10CLASSIFICATION_THRESHOLD = 0
11
12
13class ClassificationEvaluator(SimpleIndividualEvaluator):
14    """
15    Class to compute the fitness of an individual in classification problems.
16    """
17
18    def __init__(self, X=None, y=None, metric=accuracy_score):
19        super().__init__()
20        self.X = X
21        self.y = y
22        self.metric = metric
23
24    def set_context(self, context):
25        """
26        Receive X and y values and assign them to X and y fields.
27
28        Parameters
29        ----------
30        context: tuple. first element is a numpy array of size (n_samples, n_features),
31                        and the second element is a numpy array of size (n_samples, 1) or (n_samples,)
32            X matrix and y vector, either (X_train, y_train) or (X_test, y_test), depending on the evolution stage
33
34        Returns
35        -------
36        None.
37        """
38        self.X = context[0]
39        self.y = context[1]
40
41    def evaluate_individual(self, individual):
42        """
43        Compute the fitness value by comparing the program tree execution result to the result vector y
44
45        Parameters
46        ----------
47        individual: Tree
48            An individual program tree in the GP population, whose fitness needs to be computed.
49            Makes use of GPTree.execute, which runs the program.
50            Calling `GPTree.execute` must use keyword arguments that match the terminal-set variables.
51            For example, if the terminal set includes `x` and `y` then the call is `GPTree.execute(x=..., y=...)`.
52
53        Returns
54        -------
55        float:
56            computed fitness value
57        """
58        y_pred = self.classify_individual(individual)
59        return self.metric(y_true=self.y, y_pred=y_pred)
60
61    def classify_individual(self, individual):
62        return np.where(individual.execute(self.X) > CLASSIFICATION_THRESHOLD, 1, 0)
CLASSIFICATION_THRESHOLD = 0
14class ClassificationEvaluator(SimpleIndividualEvaluator):
15    """
16    Class to compute the fitness of an individual in classification problems.
17    """
18
19    def __init__(self, X=None, y=None, metric=accuracy_score):
20        super().__init__()
21        self.X = X
22        self.y = y
23        self.metric = metric
24
25    def set_context(self, context):
26        """
27        Receive X and y values and assign them to X and y fields.
28
29        Parameters
30        ----------
31        context: tuple. first element is a numpy array of size (n_samples, n_features),
32                        and the second element is a numpy array of size (n_samples, 1) or (n_samples,)
33            X matrix and y vector, either (X_train, y_train) or (X_test, y_test), depending on the evolution stage
34
35        Returns
36        -------
37        None.
38        """
39        self.X = context[0]
40        self.y = context[1]
41
42    def evaluate_individual(self, individual):
43        """
44        Compute the fitness value by comparing the program tree execution result to the result vector y
45
46        Parameters
47        ----------
48        individual: Tree
49            An individual program tree in the GP population, whose fitness needs to be computed.
50            Makes use of GPTree.execute, which runs the program.
51            Calling `GPTree.execute` must use keyword arguments that match the terminal-set variables.
52            For example, if the terminal set includes `x` and `y` then the call is `GPTree.execute(x=..., y=...)`.
53
54        Returns
55        -------
56        float:
57            computed fitness value
58        """
59        y_pred = self.classify_individual(individual)
60        return self.metric(y_true=self.y, y_pred=y_pred)
61
62    def classify_individual(self, individual):
63        return np.where(individual.execute(self.X) > CLASSIFICATION_THRESHOLD, 1, 0)

Class to compute the fitness of an individual in classification problems.

ClassificationEvaluator(X=None, y=None, metric=<function accuracy_score>)
19    def __init__(self, X=None, y=None, metric=accuracy_score):
20        super().__init__()
21        self.X = X
22        self.y = y
23        self.metric = metric
X
y
metric
def set_context(self, context):
25    def set_context(self, context):
26        """
27        Receive X and y values and assign them to X and y fields.
28
29        Parameters
30        ----------
31        context: tuple. first element is a numpy array of size (n_samples, n_features),
32                        and the second element is a numpy array of size (n_samples, 1) or (n_samples,)
33            X matrix and y vector, either (X_train, y_train) or (X_test, y_test), depending on the evolution stage
34
35        Returns
36        -------
37        None.
38        """
39        self.X = context[0]
40        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, y_test), depending on the evolution stage
Returns
  • None.
def evaluate_individual(self, individual):
42    def evaluate_individual(self, individual):
43        """
44        Compute the fitness value by comparing the program tree execution result to the result vector y
45
46        Parameters
47        ----------
48        individual: Tree
49            An individual program tree in the GP population, whose fitness needs to be computed.
50            Makes use of GPTree.execute, which runs the program.
51            Calling `GPTree.execute` must use keyword arguments that match the terminal-set variables.
52            For example, if the terminal set includes `x` and `y` then the call is `GPTree.execute(x=..., y=...)`.
53
54        Returns
55        -------
56        float:
57            computed fitness value
58        """
59        y_pred = self.classify_individual(individual)
60        return self.metric(y_true=self.y, y_pred=y_pred)

Compute the fitness value by comparing the program tree execution result to the result vector y

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. Calling GPTree.execute must use keyword arguments that match the terminal-set variables. For example, if the terminal set includes x and y then the call is GPTree.execute(x=..., y=...).
Returns
  • float:: computed fitness value
def classify_individual(self, individual):
62    def classify_individual(self, individual):
63        return np.where(individual.execute(self.X) > CLASSIFICATION_THRESHOLD, 1, 0)