The scikit-learn estimators

EvoDAG.estimators

EvoDAGClassifier and EvoDAGRegressor give one EvoDAG model the interface of scikit-learn (get_params, clone, pipelines, grid search, score). Each instance is one evolution and one Model, and its predictions equal those of EvoDAG.model.EvoDAG with the same parameters and seed. EnsembleClassifier and EnsembleRegressor clone any estimator n_estimators times with consecutive seeds and combine the members as EvoDAGE does: the median of the outputs. The predict of the classifier is the argmax of that median, which can differ from the majority vote of EvoDAGE. The module needs scikit-learn, an optional dependency (pip install 'EvoDAG[sklearn]').

scikit-learn estimators: one EvoDAG model, and ensembles of any estimator.

EvoDAGClassifier and EvoDAGRegressor follow the conventions of scikit-learn (get_params/set_params, clone, pipelines, grid search, score) and pass its estimator checks. Each instance runs one evolution and keeps one Model. EnsembleClassifier and EnsembleRegressor clone any estimator several times and combine the members as EvoDAGE does (the median of the outputs), so that EnsembleClassifier(EvoDAGClassifier()) is its scikit-learn form; its predict is the argmax of that median, which can differ from the majority vote of EvoDAGE. This module needs scikit-learn, which is an optional dependency:

pip install 'EvoDAG[sklearn]'

The two classes are reachable from the top-level package without importing this module until they are used: from EvoDAG import EvoDAGClassifier. Without scikit-learn that statement raises an ImportError that says how to install it, and the rest of the package keeps working.

class EvoDAGClassifier[source]

Classifier that evolves one EvoDAG model (scikit-learn interface).

One instance is one evolution and one Model; to combine several of them see the ensembles of scikit-learn or EvoDAGE. With integer labels 0..k-1 and the same parameters and seed, predict equals that of EvoDAG.model.EvoDAG(seed=random_state, ...) (the single model of EvoDAG.model, not the search engine EvoDAG.EvoDAG). Labels of any type are accepted; they are encoded with LabelEncoder and the engine receives 0..k-1.

Parameters:
  • popsize (int, optional) – Population size. None (default) uses the value of the file of default parameters EvoDAG/conf/default_parameters.json.

  • early_stopping_rounds (int, optional) – Individuals created without improvement before the evolution stops. None uses the default parameters.

  • time_limit (float, optional) – Seconds allowed for the evolution. None uses the default parameters.

  • random_state (int, optional) – Seed of the evolution (the engine’s seed; default 0). Only an integer is accepted and None is the same as 0, so a fit is always deterministic.

  • params_fname (str or dict, optional) – JSON file (or dictionary) used instead of the default parameters, see EvoDAG.EvoDAG.init.

  • params (dict, optional) – Any other parameter of the engine, for example {"Add": 30, "tr_fraction": 0.4}. popsize, early_stopping_rounds and time_limit given as arguments take precedence; the keys seed and classifier are not allowed. A probability_calibration class given here makes predict_proba return calibrated probabilities.

classes_

The class labels, sorted.

Type:

numpy.ndarray

n_features_in_

Number of columns of X seen in fit.

Type:

int

model_

The fitted model.

Type:

EvoDAG.model.Model

Examples

>>> import numpy as np
>>> from EvoDAG import EvoDAGClassifier
>>> rng = np.random.RandomState(0)
>>> X = rng.randn(100, 3)
>>> y = np.where(X[:, 0] > 0, "up", "down")
>>> clf = EvoDAGClassifier(popsize=50, early_stopping_rounds=50,
...                        random_state=0).fit(X, y)
>>> clf.classes_.tolist()
['down', 'up']
>>> clf.predict(X).shape
(100,)
>>> bool(clf.score(X, y) > 0.8)
True
>>> clf.decision_function(X).shape
(100,)
>>> clf.predict_proba(X).shape
(100, 2)
fit(X, y)[source]

Run one evolution.

Parameters:
  • X (array_like) – Inputs, one row per example (n_samples, n_features); NaN, infinite values and sparse matrices are rejected.

  • y (array_like) – Class labels, numbers or strings.

Returns:

This instance, with model_, classes_ and n_features_in_ set.

Return type:

EvoDAGClassifier

Raises:

ValueError – If there are fewer than two examples, y has a single class or y is not a classification target.

predict(X)[source]

Predict the class of each example: the one with the largest output.

Parameters:

X (array_like) – Inputs, one row per example (n_samples, n_features).

Returns:

One element of classes_ per example.

Return type:

numpy.ndarray

decision_function(X)[source]

Outputs of the model.

Parameters:

X (array_like) – Inputs, one row per example (n_samples, n_features).

Returns:

Shape (n_samples,) with two classes, the output of the second class minus that of the first (positive means classes_[1]); (n_samples, n_classes) otherwise. Non-finite outputs count as zero, as in predict.

Return type:

numpy.ndarray

predict_proba(X)[source]

Probability of each class.

A single model has no graded probability. If the engine parameter probability_calibration was given, the calibrated probabilities are returned (they are not guaranteed to have the same argmax as predict); otherwise the result is the one-hot encoding of predict. The rows add up to 1.

Parameters:

X (array_like) – Inputs, one row per example (n_samples, n_features).

Returns:

One row per example and one column per class, in the order of classes_.

Return type:

numpy.ndarray

class EvoDAGRegressor[source]

Regressor that evolves one EvoDAG model (scikit-learn interface).

One instance is one evolution and one Model. With the same parameters and seed, predict equals that of EvoDAG.model.EvoDAG(seed=random_state, classifier=False, ...). The defaults come from EvoDAG/conf/default_parameters_r.json.

Parameters:
  • popsize (int, optional) – Population size. None (default) uses the default parameters.

  • early_stopping_rounds (int, optional) – Individuals created without improvement before the evolution stops. None uses the default parameters.

  • time_limit (float, optional) – Seconds allowed for the evolution. None uses the default parameters.

  • random_state (int, optional) – Seed of the evolution (the engine’s seed; default 0). Only an integer is accepted and None is the same as 0.

  • params_fname (str or dict, optional) – JSON file (or dictionary) used instead of the default parameters, see EvoDAG.EvoDAG.init.

  • params (dict, optional) – Any other parameter of the engine, for example {"Add": 30}; the keys seed and classifier are not allowed.

n_features_in_

Number of columns of X seen in fit.

Type:

int

model_

The fitted model.

Type:

EvoDAG.model.Model

Examples

>>> import numpy as np
>>> from EvoDAG import EvoDAGRegressor
>>> rng = np.random.RandomState(0)
>>> X = rng.randn(100, 3)
>>> y = 2 * X[:, 0] - X[:, 1]
>>> reg = EvoDAGRegressor(popsize=50, early_stopping_rounds=50,
...                       random_state=0).fit(X, y)
>>> reg.predict(X).shape
(100,)
>>> bool(reg.score(X, y) > 0.9)
True
fit(X, y)[source]

Run one evolution.

Parameters:
  • X (array_like) – Inputs, one row per example (n_samples, n_features); NaN, infinite values and sparse matrices are rejected.

  • y (array_like) – Real targets.

Returns:

This instance, with model_ and n_features_in_ set.

Return type:

EvoDAGRegressor

predict(X)[source]

Predict the target of each example.

Parameters:

X (array_like) – Inputs, one row per example (n_samples, n_features).

Returns:

One predicted value per example.

Return type:

numpy.ndarray

class EnsembleClassifier[source]

Ensemble of clones of a classifier, built as EvoDAGE is.

EnsembleClassifier(EvoDAGClassifier(...), n_estimators=n, random_state=s) is the scikit-learn form of EvoDAG.EvoDAGE(n_estimators=n, seed=s, ...): the members are the same, and decision_function is exactly the median over the members that EvoDAGE.decision_function returns. It also works with any other classifier. Member i is a clone of estimator fitted on the whole training set with random_state + i (the diversity comes from the seed). scikit-learn’s bagging and voting ensembles cannot be used instead: they average the outputs and draw random seeds.

To follow the conventions of scikit-learn, predict, decision_function and predict_proba agree with one another. When the members have a decision_function (the EvoDAG members do), predict is the class with the largest median decision (with two classes, classes_[1] when the median is positive). This can differ from EvoDAGE.predict, which is the majority vote of the members. When they have none (a decision tree, for example), predict is the majority vote, a tie going to the class with the lowest index.

Parameters:
  • estimator (object, optional) – Classifier that is cloned; None (default) is EvoDAGClassifier with its default parameters. If it has no random_state parameter the members are identical clones. Only its own random_state is set (not that of nested estimators).

  • n_estimators (int, optional) – Number of members (default 30).

  • random_state (int, optional) – Seed of the first member (default 0); only an integer is accepted and None is the same as 0.

  • n_jobs (int, optional) – Processes used by joblib to fit and evaluate the members (default 1); the results do not depend on it.

  • voting (str, optional) – Either "hard" or "soft". "hard" (default): predict is the argmax of the median decision_function (the majority vote if the members have none), and predict_proba is the softmax of that median over the classes (the logistic function of it with two classes), or the fraction of votes if the members have no decision_function. "soft": predict_proba is the mean of the predict_proba of the members, renormalised (rows that are not finite become 1 / n_classes), which is the way EvoDAGE combines calibrated probabilities, and predict is its argmax, as in scikit-learn’s VotingClassifier; it needs members with predict_proba.

estimators_

The fitted members, in order.

Type:

list

classes_

The class labels, sorted.

Type:

numpy.ndarray

n_features_in_

Number of columns of X seen in fit.

Type:

int

Examples

>>> import numpy as np
>>> from EvoDAG import EnsembleClassifier, EvoDAGClassifier
>>> rng = np.random.RandomState(0)
>>> X = rng.randn(100, 3)
>>> y = np.where(X[:, 0] > 0, "up", "down")
>>> tiny = EvoDAGClassifier(popsize=50, early_stopping_rounds=50)
>>> clf = EnsembleClassifier(tiny, n_estimators=3, random_state=0).fit(X, y)
>>> [m.random_state for m in clf.estimators_]
[0, 1, 2]
>>> clf.predict(X).shape
(100,)
>>> bool(clf.score(X, y) > 0.8)
True
>>> clf.predict_proba(X).shape
(100, 2)
__init__(estimator=None, n_estimators=30, random_state=0, n_jobs=1, voting='hard')[source]
fit(X, y)[source]

Fit the members on the whole training set.

Parameters:
  • X (array_like) – Inputs, one row per example (n_samples, n_features); NaN, infinite values and sparse matrices are rejected.

  • y (array_like) – Class labels, numbers or strings. The members receive them encoded as 0..k-1.

Returns:

This instance, with estimators_, classes_ and n_features_in_ set.

Return type:

EnsembleClassifier

Raises:

ValueError – If n_estimators is not an integer >= 1, voting is not "hard" or "soft", random_state is not an integer, or y is not a classification target.

predict(X)[source]

Predict the class of each example.

With voting="soft" it is the argmax of predict_proba. With voting="hard" it is the argmax of the median decision_function (classes_[1] when the median is positive, with two classes) if the members have a decision_function, and otherwise the class chosen by most members, a tie going to the class with the lowest index. In the first case it can differ from the majority vote of EvoDAGE.

Parameters:

X (array_like) – Inputs, one row per example (n_samples, n_features).

Returns:

One element of classes_ per example.

Return type:

numpy.ndarray

predict_proba(X)[source]

Probability of each class, see voting.

With voting="soft" it is the mean of the probabilities of the members. With voting="hard" and members that have a decision_function it is the softmax over the classes of the median decision (with two classes, [1 - p, p] with p the logistic function of it), so that its argmax is predict; if the members have none it is the fraction of members that vote for each class.

Parameters:

X (array_like) – Inputs, one row per example (n_samples, n_features).

Returns:

One row per example, one column per class in the order of classes_; the rows add up to 1.

Return type:

numpy.ndarray

Raises:

AttributeError – With voting="soft" if the members have no predict_proba.

decision_function(X)[source]

Median, over the members, of their decision_function.

Only available when the estimator has a decision_function.

Parameters:

X (array_like) – Inputs, one row per example (n_samples, n_features).

Returns:

Shape (n_samples,) with two classes and (n_samples, n_classes) otherwise, as the members return it.

Return type:

numpy.ndarray

class EnsembleRegressor[source]

Ensemble of clones of a regressor, built as EvoDAGE is.

EnsembleRegressor(EvoDAGRegressor(...), n_estimators=n, random_state=s) is the scikit-learn form of EvoDAG.EvoDAGE(n_estimators=n, seed=s, classifier=False, ...) and gives the same predictions. It also works with any other regressor. Member i is a clone of estimator fitted on the whole training set with random_state + i, and predict is the median of the members’ predictions, which are left out when they are not all finite (if none is finite all of them are used).

Parameters:
  • estimator (object, optional) – Regressor that is cloned; None (default) is EvoDAGRegressor with its default parameters. If it has no random_state parameter the members are identical clones. Only its own random_state is set (not that of nested estimators).

  • n_estimators (int, optional) – Number of members (default 30).

  • random_state (int, optional) – Seed of the first member (default 0); only an integer is accepted and None is the same as 0.

  • n_jobs (int, optional) – Processes used by joblib to fit and evaluate the members (default 1); the results do not depend on it.

estimators_

The fitted members, in order.

Type:

list

n_features_in_

Number of columns of X seen in fit.

Type:

int

Examples

>>> import numpy as np
>>> from EvoDAG import EnsembleRegressor, EvoDAGRegressor
>>> rng = np.random.RandomState(0)
>>> X = rng.randn(100, 3)
>>> y = 2 * X[:, 0] - X[:, 1]
>>> tiny = EvoDAGRegressor(popsize=50, early_stopping_rounds=50)
>>> reg = EnsembleRegressor(tiny, n_estimators=3, random_state=0).fit(X, y)
>>> reg.predict(X).shape
(100,)
>>> bool(reg.score(X, y) > 0.9)
True
fit(X, y)[source]

Fit the members on the whole training set.

Parameters:
  • X (array_like) – Inputs, one row per example (n_samples, n_features); NaN, infinite values and sparse matrices are rejected.

  • y (array_like) – Real targets.

Returns:

This instance, with estimators_ and n_features_in_ set.

Return type:

EnsembleRegressor

Raises:

ValueError – If n_estimators is not an integer >= 1 or random_state is not an integer.

predict(X)[source]

Median of the predictions of the members.

Parameters:

X (array_like) – Inputs, one row per example (n_samples, n_features).

Returns:

One predicted value per example.

Return type:

numpy.ndarray