.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/selection/Selectors-Pipelines.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_selection_Selectors-Pipelines.py: Using scikit-matter selectors with scikit-learn pipelines ========================================================= .. GENERATED FROM PYTHON SOURCE LINES 11-23 .. code-block:: Python import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_diabetes from sklearn.linear_model import RidgeCV from sklearn.model_selection import train_test_split from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from skmatter.feature_selection import CUR, FPS .. GENERATED FROM PYTHON SOURCE LINES 24-28 Simple integration of scikit-matter selectors --------------------------------------------- This example shows how to use FPS to subselect features before training a RidgeCV. .. GENERATED FROM PYTHON SOURCE LINES 29-50 .. code-block:: Python scaler = StandardScaler() selector = FPS(n_to_select=4) ridge = RidgeCV(cv=2, alphas=np.logspace(-8, 2, 10)) X, y = load_diabetes(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) pipe = Pipeline([("scaler", scaler), ("selector", selector), ("ridge", ridge)]) pipe.fit(X_train.copy(), y_train.copy()) plt.scatter(y_test, pipe.predict(X_test)) plt.gca().set_aspect("equal") plt.plot(plt.xlim(), plt.xlim(), "r--") plt.xlabel("True Values") plt.ylabel("Predicted Values") plt.show() .. image-sg:: /examples/selection/images/sphx_glr_Selectors-Pipelines_001.png :alt: Selectors Pipelines :srcset: /examples/selection/images/sphx_glr_Selectors-Pipelines_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 51-56 Stacking selectors one after another ------------------------------------ This example shows how to use an FPS, then CUR selector to subselect features before training a RidgeCV. .. GENERATED FROM PYTHON SOURCE LINES 57-79 .. code-block:: Python scaler = StandardScaler() fps = FPS(n_to_select=8) cur = CUR(n_to_select=4) ridge = RidgeCV(cv=2, alphas=np.logspace(-8, 2, 10)) X, y = load_diabetes(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) pipe = Pipeline( [("scaler", scaler), ("selector1", fps), ("selector2", cur), ("ridge", ridge)] ) pipe.fit(X_train.copy(), y_train.copy()) plt.scatter(y_test, pipe.predict(X_test)) plt.gca().set_aspect("equal") plt.plot(plt.xlim(), plt.xlim(), "r--") plt.xlabel("True Values") plt.ylabel("Predicted Values") plt.show() .. image-sg:: /examples/selection/images/sphx_glr_Selectors-Pipelines_002.png :alt: Selectors Pipelines :srcset: /examples/selection/images/sphx_glr_Selectors-Pipelines_002.png :class: sphx-glr-single-img .. _sphx_glr_download_examples_selection_Selectors-Pipelines.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: Selectors-Pipelines.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: Selectors-Pipelines.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: Selectors-Pipelines.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_