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 orEvoDAGE. With integer labels0..k-1and the same parameters and seed,predictequals that ofEvoDAG.model.EvoDAG(seed=random_state, ...)(the single model ofEvoDAG.model, not the search engineEvoDAG.EvoDAG). Labels of any type are accepted; they are encoded withLabelEncoderand the engine receives0..k-1.- Parameters:
popsize (int, optional) – Population size.
None(default) uses the value of the file of default parametersEvoDAG/conf/default_parameters.json.early_stopping_rounds (int, optional) – Individuals created without improvement before the evolution stops.
Noneuses the default parameters.time_limit (float, optional) – Seconds allowed for the evolution.
Noneuses the default parameters.random_state (int, optional) – Seed of the evolution (the engine’s
seed; default 0). Only an integer is accepted andNoneis 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_roundsandtime_limitgiven as arguments take precedence; the keysseedandclassifierare not allowed. Aprobability_calibrationclass given here makespredict_probareturn calibrated probabilities.
- classes_¶
The class labels, sorted.
- Type:
- model_¶
The fitted model.
- Type:
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_andn_features_in_set.- Return type:
- Raises:
ValueError – If there are fewer than two examples,
yhas a single class oryis 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:
- 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 meansclasses_[1]);(n_samples, n_classes)otherwise. Non-finite outputs count as zero, as inpredict.- Return type:
- predict_proba(X)[source]¶
Probability of each class.
A single model has no graded probability. If the engine parameter
probability_calibrationwas given, the calibrated probabilities are returned (they are not guaranteed to have the same argmax aspredict); otherwise the result is the one-hot encoding ofpredict. 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:
- 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,predictequals that ofEvoDAG.model.EvoDAG(seed=random_state, classifier=False, ...). The defaults come fromEvoDAG/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.
Noneuses the default parameters.time_limit (float, optional) – Seconds allowed for the evolution.
Noneuses the default parameters.random_state (int, optional) – Seed of the evolution (the engine’s
seed; default 0). Only an integer is accepted andNoneis 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 keysseedandclassifierare not allowed.
- model_¶
The fitted model.
- Type:
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_andn_features_in_set.- Return type:
- class EnsembleClassifier[source]¶
Ensemble of clones of a classifier, built as
EvoDAGEis.EnsembleClassifier(EvoDAGClassifier(...), n_estimators=n, random_state=s)is the scikit-learn form ofEvoDAG.EvoDAGE(n_estimators=n, seed=s, ...): the members are the same, anddecision_functionis exactly the median over the members thatEvoDAGE.decision_functionreturns. It also works with any other classifier. Memberiis a clone ofestimatorfitted on the whole training set withrandom_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_functionandpredict_probaagree with one another. When the members have adecision_function(the EvoDAG members do),predictis the class with the largest median decision (with two classes,classes_[1]when the median is positive). This can differ fromEvoDAGE.predict, which is the majority vote of the members. When they have none (a decision tree, for example),predictis the majority vote, a tie going to the class with the lowest index.- Parameters:
estimator (object, optional) – Classifier that is cloned;
None(default) isEvoDAGClassifierwith its default parameters. If it has norandom_stateparameter the members are identical clones. Only its ownrandom_stateis 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
Noneis the same as 0.n_jobs (int, optional) – Processes used by
joblibto fit and evaluate the members (default 1); the results do not depend on it.voting (str, optional) – Either
"hard"or"soft"."hard"(default):predictis the argmax of the mediandecision_function(the majority vote if the members have none), andpredict_probais 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 nodecision_function."soft":predict_probais the mean of thepredict_probaof the members, renormalised (rows that are not finite become1 / n_classes), which is the wayEvoDAGEcombines calibrated probabilities, andpredictis its argmax, as in scikit-learn’sVotingClassifier; it needs members withpredict_proba.
- classes_¶
The class labels, sorted.
- Type:
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)
- 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_andn_features_in_set.- Return type:
- Raises:
ValueError – If
n_estimatorsis not an integer >= 1,votingis not"hard"or"soft",random_stateis not an integer, oryis not a classification target.
- predict(X)[source]¶
Predict the class of each example.
With
voting="soft"it is the argmax ofpredict_proba. Withvoting="hard"it is the argmax of the mediandecision_function(classes_[1]when the median is positive, with two classes) if the members have adecision_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 ofEvoDAGE.- Parameters:
X (array_like) – Inputs, one row per example (
n_samples, n_features).- Returns:
One element of
classes_per example.- Return type:
- predict_proba(X)[source]¶
Probability of each class, see
voting.With
voting="soft"it is the mean of the probabilities of the members. Withvoting="hard"and members that have adecision_functionit is the softmax over the classes of the median decision (with two classes,[1 - p, p]withpthe logistic function of it), so that its argmax ispredict; 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:
- Raises:
AttributeError – With
voting="soft"if the members have nopredict_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:
- class EnsembleRegressor[source]¶
Ensemble of clones of a regressor, built as
EvoDAGEis.EnsembleRegressor(EvoDAGRegressor(...), n_estimators=n, random_state=s)is the scikit-learn form ofEvoDAG.EvoDAGE(n_estimators=n, seed=s, classifier=False, ...)and gives the same predictions. It also works with any other regressor. Memberiis a clone ofestimatorfitted on the whole training set withrandom_state + i, andpredictis 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) isEvoDAGRegressorwith its default parameters. If it has norandom_stateparameter the members are identical clones. Only its ownrandom_stateis 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
Noneis the same as 0.n_jobs (int, optional) – Processes used by
joblibto fit and evaluate the members (default 1); the results do not depend on it.
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_andn_features_in_set.- Return type:
- Raises:
ValueError – If
n_estimatorsis not an integer >= 1 orrandom_stateis not an integer.