eckity.sklearn_compatible.sklearn_wrapper

 1from sklearn.utils.validation import check_is_fitted, check_X_y
 2
 3
 4class SklearnWrapper:
 5    """
 6    Sklearn-compatible wrapper to support evolution using sklearn methods.
 7
 8    Parameters
 9    ----------
10    algorithm: Algorithm
11        Wrapped Evolutionary algorithm.
12        The Wrapper invokes 'evolve' and 'execute' methods of the algorithm
13        during the fitting and prediction process, respectively.
14
15    Attributes
16    ----------
17    is_fitted: bool
18        Determines if the model is fitted (evolved).
19    """
20
21    def __init__(self,
22                 algorithm):
23        self.algorithm = algorithm
24
25    def fit(self, X, y=None):
26        """
27       Run evolutionary algorithm.
28       Use `fit` in a sklearn setting.
29        Parameters
30        ----------
31        X : {array-like, sparse matrix} of shape (n_samples, n_features)
32            The training input samples.
33        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
34            The target values (real numbers).
35        Returns
36        -------
37        self : SklearnWrapper
38            Fitted (evolved) model.
39        """
40        # Check that X and y have correct shape
41        X, y = check_X_y(X, y)
42
43        for sub_pop in self.algorithm.population.sub_populations:
44            sub_pop.evaluator.set_context((X, y))
45
46        self.algorithm.evolve()
47        self.is_fitted_ = True
48        return self
49
50    def predict(self, X):
51        """
52        Compute output using best evolved individual.
53        Use `predict` in a sklearn setting.
54        Input is a numpy array.
55
56        Parameters
57        ----------
58        X : array-like or sparse matrix of (num samples, num feautres)
59
60        Returns
61        -------
62        y : array, shape (num samples,)
63            Returns predicted values.
64
65        """
66
67        # Check is fit had been called
68        check_is_fitted(self)
69
70        return self.algorithm.best_of_run_.execute(X)
71
72    # def __sklearn_is_fitted__(self):
73    #     return self.is_fitted_
74
75    def get_params(self, deep=True):
76        return self.__getstate__()
77
78    def set_params(self, **parameters):
79        self.algorithm.__setstate__(parameters)
80        return self
81
82    def partial_fit(self, X, y, classes=None):
83        raise NotImplementedError('not implemented yet')
84        pass
85
86    def __getstate__(self):
87        state = self.__dict__.copy()
88        if 'is_fitted_' in state:
89            del state['is_fitted_']
90        return state
91
92    def __setstate__(self, state):
93        self.__dict__.update(state)
class SklearnWrapper:
 5class SklearnWrapper:
 6    """
 7    Sklearn-compatible wrapper to support evolution using sklearn methods.
 8
 9    Parameters
10    ----------
11    algorithm: Algorithm
12        Wrapped Evolutionary algorithm.
13        The Wrapper invokes 'evolve' and 'execute' methods of the algorithm
14        during the fitting and prediction process, respectively.
15
16    Attributes
17    ----------
18    is_fitted: bool
19        Determines if the model is fitted (evolved).
20    """
21
22    def __init__(self,
23                 algorithm):
24        self.algorithm = algorithm
25
26    def fit(self, X, y=None):
27        """
28       Run evolutionary algorithm.
29       Use `fit` in a sklearn setting.
30        Parameters
31        ----------
32        X : {array-like, sparse matrix} of shape (n_samples, n_features)
33            The training input samples.
34        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
35            The target values (real numbers).
36        Returns
37        -------
38        self : SklearnWrapper
39            Fitted (evolved) model.
40        """
41        # Check that X and y have correct shape
42        X, y = check_X_y(X, y)
43
44        for sub_pop in self.algorithm.population.sub_populations:
45            sub_pop.evaluator.set_context((X, y))
46
47        self.algorithm.evolve()
48        self.is_fitted_ = True
49        return self
50
51    def predict(self, X):
52        """
53        Compute output using best evolved individual.
54        Use `predict` in a sklearn setting.
55        Input is a numpy array.
56
57        Parameters
58        ----------
59        X : array-like or sparse matrix of (num samples, num feautres)
60
61        Returns
62        -------
63        y : array, shape (num samples,)
64            Returns predicted values.
65
66        """
67
68        # Check is fit had been called
69        check_is_fitted(self)
70
71        return self.algorithm.best_of_run_.execute(X)
72
73    # def __sklearn_is_fitted__(self):
74    #     return self.is_fitted_
75
76    def get_params(self, deep=True):
77        return self.__getstate__()
78
79    def set_params(self, **parameters):
80        self.algorithm.__setstate__(parameters)
81        return self
82
83    def partial_fit(self, X, y, classes=None):
84        raise NotImplementedError('not implemented yet')
85        pass
86
87    def __getstate__(self):
88        state = self.__dict__.copy()
89        if 'is_fitted_' in state:
90            del state['is_fitted_']
91        return state
92
93    def __setstate__(self, state):
94        self.__dict__.update(state)

Sklearn-compatible wrapper to support evolution using sklearn methods.

Parameters
  • algorithm (Algorithm): Wrapped Evolutionary algorithm. The Wrapper invokes 'evolve' and 'execute' methods of the algorithm during the fitting and prediction process, respectively.
Attributes
  • is_fitted (bool): Determines if the model is fitted (evolved).
SklearnWrapper(algorithm)
22    def __init__(self,
23                 algorithm):
24        self.algorithm = algorithm
algorithm
def fit(self, X, y=None):
26    def fit(self, X, y=None):
27        """
28       Run evolutionary algorithm.
29       Use `fit` in a sklearn setting.
30        Parameters
31        ----------
32        X : {array-like, sparse matrix} of shape (n_samples, n_features)
33            The training input samples.
34        y : array-like of shape (n_samples,) or (n_samples, n_outputs)
35            The target values (real numbers).
36        Returns
37        -------
38        self : SklearnWrapper
39            Fitted (evolved) model.
40        """
41        # Check that X and y have correct shape
42        X, y = check_X_y(X, y)
43
44        for sub_pop in self.algorithm.population.sub_populations:
45            sub_pop.evaluator.set_context((X, y))
46
47        self.algorithm.evolve()
48        self.is_fitted_ = True
49        return self

Run evolutionary algorithm. Use fit in a sklearn setting. Parameters


X : {array-like, sparse matrix} of shape (n_samples, n_features) The training input samples. y : array-like of shape (n_samples,) or (n_samples, n_outputs) The target values (real numbers). Returns


self : SklearnWrapper Fitted (evolved) model.

def predict(self, X):
51    def predict(self, X):
52        """
53        Compute output using best evolved individual.
54        Use `predict` in a sklearn setting.
55        Input is a numpy array.
56
57        Parameters
58        ----------
59        X : array-like or sparse matrix of (num samples, num feautres)
60
61        Returns
62        -------
63        y : array, shape (num samples,)
64            Returns predicted values.
65
66        """
67
68        # Check is fit had been called
69        check_is_fitted(self)
70
71        return self.algorithm.best_of_run_.execute(X)

Compute output using best evolved individual. Use predict in a sklearn setting. Input is a numpy array.

Parameters
  • X (array-like or sparse matrix of (num samples, num feautres)):
Returns
  • y (array, shape (num samples,)): Returns predicted values.
def get_params(self, deep=True):
76    def get_params(self, deep=True):
77        return self.__getstate__()
def set_params(self, **parameters):
79    def set_params(self, **parameters):
80        self.algorithm.__setstate__(parameters)
81        return self
def partial_fit(self, X, y, classes=None):
83    def partial_fit(self, X, y, classes=None):
84        raise NotImplementedError('not implemented yet')
85        pass