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"""
  5
  6import numpy as np
  7from sklearn.metrics import accuracy_score
  8
  9from eckity.evaluators.simple_individual_evaluator import (
 10    SimpleIndividualEvaluator,
 11)
 12from scipy.special import expit as sigmoid
 13
 14CLF_METHODS = ("sigmoid", "argmax", "softmax")
 15
 16
 17class ClassificationEvaluator(SimpleIndividualEvaluator):
 18    """
 19    Class to compute the fitness of an individual in classification problems.
 20    All simple classes assume only one sub-population.
 21    """
 22
 23    def __init__(
 24        self,
 25        X=None,
 26        y=None,
 27        metric=accuracy_score,
 28        n_classes=2,
 29        clf_method=CLF_METHODS[0],
 30    ):
 31        super().__init__()
 32        self.X = X
 33        self.y = y
 34        self.metric = metric
 35        self.n_classes = n_classes
 36
 37        if clf_method not in CLF_METHODS:
 38            raise ValueError(
 39                "classification method must be one of", CLF_METHODS
 40            )
 41        self.clf_method = clf_method
 42
 43    def set_context(self, context):
 44        """
 45        Receive X and y values and assign them to X and y fields.
 46
 47        Parameters
 48        ----------
 49        context: tuple. first element is a numpy array of size (n_samples, n_features),
 50                        and the second element is a numpy array of size (n_samples, 1) or (n_samples,)
 51            X matrix and y vector, either (X_train, y_train) or (X_test, y_test), depending on the evolution stage
 52
 53        Returns
 54        -------
 55        None.
 56        """
 57        self.X = context[0]
 58        self.y = context[1]
 59
 60    def evaluate_individual(self, individual):
 61        """
 62        Compute the fitness value by comparing the program tree execution result to the result vector y
 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            Calling `GPTree.execute` must use keyword arguments that match the terminal-set variables.
 70            For example, if the terminal set includes `x` and `y` then the call is `GPTree.execute(x=..., y=...)`.
 71
 72        Returns
 73        -------
 74        float:
 75            computed fitness value
 76        """
 77        y_pred = self.classify_individual(individual)
 78        return self.metric(y_true=self.y, y_pred=y_pred)
 79
 80    def classify_individual(self, individual):
 81        clf_method_to_function = {
 82            "sigmoid": self._clf_sigmoid,
 83            "argmax": self._clf_argmax,
 84            "softmax": self._clf_softmax,
 85        }
 86        selected_func = clf_method_to_function[self.clf_method]
 87        return selected_func(individual)
 88
 89    def _clf_sigmoid(self, individual):
 90        # normalize execute results between 0 and 1
 91        probs = sigmoid(individual.execute(self.X))
 92        # Create thresholds: 1/N, 2/N, ..., (N-1)/N
 93        thresholds = np.linspace(0, 1, self.n_classes + 1)[1:-1]
 94        return np.digitize(probs, thresholds)
 95
 96    def _clf_argmax(self, individual):
 97        return self._clf_root_func(individual, "argmax")
 98
 99    def _clf_softmax(self, individual):
100        return self._clf_root_func(individual, "softmax")
101
102    def _clf_root_func(self, individual, method):
103        # assumes individual is a GP tree with argmax function in depth 1
104        if method not in individual.root.function.__name__:
105            raise ValueError(
106                f"Individual must have {method} function in depth 0 to classify."
107            )
108        return individual.execute(self.X)
CLF_METHODS = ('sigmoid', 'argmax', 'softmax')
 18class ClassificationEvaluator(SimpleIndividualEvaluator):
 19    """
 20    Class to compute the fitness of an individual in classification problems.
 21    All simple classes assume only one sub-population.
 22    """
 23
 24    def __init__(
 25        self,
 26        X=None,
 27        y=None,
 28        metric=accuracy_score,
 29        n_classes=2,
 30        clf_method=CLF_METHODS[0],
 31    ):
 32        super().__init__()
 33        self.X = X
 34        self.y = y
 35        self.metric = metric
 36        self.n_classes = n_classes
 37
 38        if clf_method not in CLF_METHODS:
 39            raise ValueError(
 40                "classification method must be one of", CLF_METHODS
 41            )
 42        self.clf_method = clf_method
 43
 44    def set_context(self, context):
 45        """
 46        Receive X and y values and assign them to X and y fields.
 47
 48        Parameters
 49        ----------
 50        context: tuple. first element is a numpy array of size (n_samples, n_features),
 51                        and the second element is a numpy array of size (n_samples, 1) or (n_samples,)
 52            X matrix and y vector, either (X_train, y_train) or (X_test, y_test), depending on the evolution stage
 53
 54        Returns
 55        -------
 56        None.
 57        """
 58        self.X = context[0]
 59        self.y = context[1]
 60
 61    def evaluate_individual(self, individual):
 62        """
 63        Compute the fitness value by comparing the program tree execution result to the result vector y
 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            Calling `GPTree.execute` must use keyword arguments that match the terminal-set variables.
 71            For example, if the terminal set includes `x` and `y` then the call is `GPTree.execute(x=..., y=...)`.
 72
 73        Returns
 74        -------
 75        float:
 76            computed fitness value
 77        """
 78        y_pred = self.classify_individual(individual)
 79        return self.metric(y_true=self.y, y_pred=y_pred)
 80
 81    def classify_individual(self, individual):
 82        clf_method_to_function = {
 83            "sigmoid": self._clf_sigmoid,
 84            "argmax": self._clf_argmax,
 85            "softmax": self._clf_softmax,
 86        }
 87        selected_func = clf_method_to_function[self.clf_method]
 88        return selected_func(individual)
 89
 90    def _clf_sigmoid(self, individual):
 91        # normalize execute results between 0 and 1
 92        probs = sigmoid(individual.execute(self.X))
 93        # Create thresholds: 1/N, 2/N, ..., (N-1)/N
 94        thresholds = np.linspace(0, 1, self.n_classes + 1)[1:-1]
 95        return np.digitize(probs, thresholds)
 96
 97    def _clf_argmax(self, individual):
 98        return self._clf_root_func(individual, "argmax")
 99
100    def _clf_softmax(self, individual):
101        return self._clf_root_func(individual, "softmax")
102
103    def _clf_root_func(self, individual, method):
104        # assumes individual is a GP tree with argmax function in depth 1
105        if method not in individual.root.function.__name__:
106            raise ValueError(
107                f"Individual must have {method} function in depth 0 to classify."
108            )
109        return individual.execute(self.X)

Class to compute the fitness of an individual in classification problems. All simple classes assume only one sub-population.

ClassificationEvaluator( X=None, y=None, metric=<function accuracy_score>, n_classes=2, clf_method='sigmoid')
24    def __init__(
25        self,
26        X=None,
27        y=None,
28        metric=accuracy_score,
29        n_classes=2,
30        clf_method=CLF_METHODS[0],
31    ):
32        super().__init__()
33        self.X = X
34        self.y = y
35        self.metric = metric
36        self.n_classes = n_classes
37
38        if clf_method not in CLF_METHODS:
39            raise ValueError(
40                "classification method must be one of", CLF_METHODS
41            )
42        self.clf_method = clf_method
X
y
metric
n_classes
clf_method
def set_context(self, context):
44    def set_context(self, context):
45        """
46        Receive X and y values and assign them to X and y fields.
47
48        Parameters
49        ----------
50        context: tuple. first element is a numpy array of size (n_samples, n_features),
51                        and the second element is a numpy array of size (n_samples, 1) or (n_samples,)
52            X matrix and y vector, either (X_train, y_train) or (X_test, y_test), depending on the evolution stage
53
54        Returns
55        -------
56        None.
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, y_test), depending on the evolution stage
Returns
  • None.
def evaluate_individual(self, individual):
61    def evaluate_individual(self, individual):
62        """
63        Compute the fitness value by comparing the program tree execution result to the result vector y
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            Calling `GPTree.execute` must use keyword arguments that match the terminal-set variables.
71            For example, if the terminal set includes `x` and `y` then the call is `GPTree.execute(x=..., y=...)`.
72
73        Returns
74        -------
75        float:
76            computed fitness value
77        """
78        y_pred = self.classify_individual(individual)
79        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):
81    def classify_individual(self, individual):
82        clf_method_to_function = {
83            "sigmoid": self._clf_sigmoid,
84            "argmax": self._clf_argmax,
85            "softmax": self._clf_softmax,
86        }
87        selected_func = clf_method_to_function[self.clf_method]
88        return selected_func(individual)