.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_affinity.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 or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_affinity.py: ================= Affine invariance ================= .. currentmodule:: fastcan In this examples, we will compare the robustness of the three feature selection methods on affine transformed features. .. GENERATED FROM PYTHON SOURCE LINES 11-15 .. code-block:: Python # Authors: The fastcan developers # SPDX-License-Identifier: MIT .. GENERATED FROM PYTHON SOURCE LINES 16-21 Initialize test --------------- The three feature selection methods, i.e., OMP, OLS, and :class:`FastCan`, will select three features from the 10 features of `diabetes` dataset. It can be seen, the three methods select the same features. .. GENERATED FROM PYTHON SOURCE LINES 21-44 .. code-block:: Python import numpy as np from sklearn.datasets import load_diabetes from sklearn.linear_model import OrthogonalMatchingPursuit from fastcan import FastCan from fastcan.utils import ols X, y = load_diabetes(return_X_y=True) n_selected = 3 omp_selector = OrthogonalMatchingPursuit(n_nonzero_coefs=n_selected) fastcan_selector = FastCan(n_features_to_select=n_selected, verbose=0) (ids_omp,) = omp_selector.fit(X, y).coef_.nonzero() ids_ols, _ = ols(X, y, n_selected) ids_fastcan = fastcan_selector.fit(X, y).indices_ print("Indices of features selected by:") print("OMP: ", np.sort(ids_omp)) print("OLS: ", np.sort(ids_ols)) print("FastCan: ", np.sort(ids_fastcan)) .. rst-class:: sphx-glr-script-out .. code-block:: none Indices of features selected by: OMP: [2 3 8] OLS: [2 3 8] FastCan: [2 3 8] .. GENERATED FROM PYTHON SOURCE LINES 45-50 Affine transformation --------------------- In this test, the 10 features of ``diabetes`` dataset will be randomly polluted by the affine transformation. The three feature selection methods will select three features from the polluted features. The more stable the result, the better. .. GENERATED FROM PYTHON SOURCE LINES 50-68 .. code-block:: Python n_features = X.shape[1] rng = np.random.default_rng() ids_omp_all = [] ids_ols_all = [] ids_fastcan_all = [] for i in range(10): X_affine = X @ np.diag(rng.random(n_features)) + rng.random(n_features) (ids_omp,) = omp_selector.fit(X_affine, y).coef_.nonzero() ids_ols, _ = ols(X_affine, y, n_selected) ids_fastcan = fastcan_selector.fit(X_affine, y).indices_ ids_omp_all += ids_omp.tolist() ids_ols_all += ids_ols.tolist() ids_fastcan_all += ids_fastcan.tolist() .. GENERATED FROM PYTHON SOURCE LINES 69-73 Plot results ------------ It can be seen, only :class:`FastCan` has robust results when the feature is polluted by the affine transformation. .. GENERATED FROM PYTHON SOURCE LINES 73-105 .. code-block:: Python import matplotlib.pyplot as plt bin_lims = np.arange(n_features + 1) counts_omp, _ = np.histogram(ids_omp_all, bins=bin_lims) counts_ols, _ = np.histogram(ids_ols_all, bins=bin_lims) counts_fastcan, _ = np.histogram(ids_fastcan_all, bins=bin_lims) fig, axs = plt.subplots(1, 3, figsize=(8, 3)) axs[0].bar(bin_lims[:-1], counts_omp) axs[0].set_xticks(bin_lims[:-1]) axs[0].set_ylim((0, 11)) axs[0].set_title("OMP") axs[0].set_xlabel("Feature Index") axs[0].set_ylabel("Count of Selected Times") axs[1].bar(bin_lims[:-1], counts_ols) axs[1].set_xticks(bin_lims[:-1]) axs[1].set_ylim((0, 11)) axs[1].set_title("OLS") axs[1].set_xlabel("Feature Index") axs[2].bar(bin_lims[:-1], counts_fastcan) axs[2].set_xticks(bin_lims[:-1]) axs[2].set_ylim((0, 11)) axs[2].set_title("FastCan") axs[2].set_xlabel("Feature Index") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_affinity_001.png :alt: OMP, OLS, FastCan :srcset: /auto_examples/images/sphx_glr_plot_affinity_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.199 seconds) .. _sphx_glr_download_auto_examples_plot_affinity.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../lite/lab/index.html?path=auto_examples/plot_affinity.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_affinity.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_affinity.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_affinity.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_