Skip to content

Commit

Permalink
DOC highlights: stacking estimators (#15414)
Browse files Browse the repository at this point in the history
* stacking estimators

* cleanup a line

* user guide

* apply thomas's suggestions

* apply suggestions

* use --upgrade instead
  • Loading branch information
adrinjalali committed Oct 31, 2019
1 parent 6c52a24 commit be027e0
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion examples/release_highlights/plot_release_highlights_0_22_0.py
Expand Up @@ -12,7 +12,7 @@
To install the latest version (with pip)::
pip install -U scikit-learn --upgrade
pip install --upgrade scikit-learn
or with conda::
Expand Down Expand Up @@ -172,3 +172,43 @@
# recomputed.
estimator.set_params(isomap__n_neighbors=5)
estimator.fit(X)

############################################################################
# Stacking Classifier and Regressor
# ---------------------------------
# :class:`~ensemble.StackingClassifier` and
# :class:`~ensemble.StackingRegressor`
# allow you to have a stack of estimators with a final classifier or
# a regressor.
# Stacked generalization consists in stacking the output of individual
# estimators and use a classifier to compute the final prediction. Stacking
# allows to use the strength of each individual estimator by using their output
# as input of a final estimator.
# Base estimators are fitted on the full ``X`` while
# the final estimator is trained using cross-validated predictions of the
# base estimators using ``cross_val_predict``.
#
# Read more in the :ref:`User Guide <stacking>`.

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import StackingClassifier
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
estimators = [
('rf', RandomForestClassifier(n_estimators=10, random_state=42)),
('svr', make_pipeline(StandardScaler(),
LinearSVC(random_state=42)))
]
clf = StackingClassifier(
estimators=estimators, final_estimator=LogisticRegression()
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=y, random_state=42
)
clf.fit(X_train, y_train).score(X_test, y_test)

0 comments on commit be027e0

Please sign in to comment.