.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_pipeline.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_pipeline.py: ===================================================== Integrating FracRidge objects into sklearn pipelines ===================================================== Because ``Fracridge`` is implemented using a Scikit-Learn-compatible API, the objects in the library can be integrated into pipelines that use objects from the Scikit Learn library. For example, here we will demonstrate a dimensionality reduction followed by a grid search to find the best fraction for the fraction parameter. .. GENERATED FROM PYTHON SOURCE LINES 15-17 Imports .. GENERATED FROM PYTHON SOURCE LINES 17-27 .. code-block:: default import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA from sklearn.pipeline import Pipeline from sklearn.datasets import make_regression from fracridge import FracRidgeRegressorCV .. GENERATED FROM PYTHON SOURCE LINES 28-30 We generate synthetic data .. GENERATED FROM PYTHON SOURCE LINES 30-44 .. code-block:: default np.random.seed(1984) n_targets = 15 n_features = 80 effective_rank = 20 X, y, coef_true = make_regression( n_samples=250, n_features=n_features, effective_rank=effective_rank, n_targets=n_targets, coef=True, noise=10) .. GENERATED FROM PYTHON SOURCE LINES 45-48 Iterating over the number of features, we generate design matrices that have more and more dimensions in them. As the number of data dimensions grows, the best fraction for FracRidge decreases. .. GENERATED FROM PYTHON SOURCE LINES 48-64 .. code-block:: default best_fracs = [] for n_components in range(2, X.shape[-1], 5): pca = PCA(n_components=n_components) frcv = FracRidgeRegressorCV() pipeline = Pipeline(steps=[('pca', pca), ('fracridgecv', frcv)]) pipeline.fit(X, y) best_fracs.append(pipeline['fracridgecv'].best_frac_) fig, ax = plt.subplots() ax.plot(range(2, X.shape[-1], 5), best_fracs, 'o-') ax.set_ylim([0, 1]) ax.set_ylabel("Best fraction") ax.set_xlabel("Number of PCA components") plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_pipeline_001.png :alt: plot pipeline :srcset: /auto_examples/images/sphx_glr_plot_pipeline_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 4.858 seconds) .. _sphx_glr_download_auto_examples_plot_pipeline.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pipeline.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pipeline.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_