.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\feature_selection\LassoFS.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_auto_examples_feature_selection_LassoFS.py: Selective inference for Lasso Feature Selection ================================================ This example shows how to perform selective inference for Lasso feature selection using the `pythonsi` library. The method is based on the work by Duy et al. (2021)[1]. [1] Le Duy, V. N., & Takeuchi, I. (2021, March). Parametric programming approach for more powerful and general lasso selective inference. In International conference on artificial intelligence and statistics (pp. 901-909). PMLR. .. GENERATED FROM PYTHON SOURCE LINES 7-18 .. code-block:: Python # Author: Tran Tuan Kiet from pythonsi import Pipeline from pythonsi.feature_selection import LassoFeatureSelection from pythonsi import Data from pythonsi.test_statistics import FSTestStatistic import numpy as np import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 19-21 Define the pipeline ------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-36 .. code-block:: Python def LassoFS() -> Pipeline: x = Data() y = Data() lasso = LassoFeatureSelection(lambda_=10) active_set = lasso.run(x, y) return Pipeline( inputs=(x, y), output=active_set, test_statistic=FSTestStatistic(x=x, y=y) ) my_pipeline = LassoFS() .. GENERATED FROM PYTHON SOURCE LINES 37-39 Generate data -------------- .. GENERATED FROM PYTHON SOURCE LINES 39-53 .. code-block:: Python def gen_data(n, p, true_beta): x = np.random.normal(loc=0, scale=1, size=(n, p)) true_beta = true_beta.reshape(-1, 1) mu = x.dot(true_beta) Sigma = np.identity(n) Y = mu + np.random.normal(loc=0, scale=1, size=(n, 1)) return x, Y, Sigma x, y, sigma = gen_data(150, 5, np.asarray([0, 0, 0, 0, 0])) .. GENERATED FROM PYTHON SOURCE LINES 54-56 Run the pipeline ----------------- .. GENERATED FROM PYTHON SOURCE LINES 56-61 .. code-block:: Python selected_features, p_values = my_pipeline([x, y], sigma) print("Selected features: ", selected_features) print("P-values: ", p_values) .. rst-class:: sphx-glr-script-out .. code-block:: none Selected features: [2 3 4] P-values: [0.15210666782406781, 0.7928001593659839, 0.5567564061709589] .. GENERATED FROM PYTHON SOURCE LINES 62-63 Plot the p-values .. GENERATED FROM PYTHON SOURCE LINES 63-68 .. code-block:: Python plt.figure() plt.bar(range(len(p_values)), p_values) plt.xlabel("Feature index") plt.ylabel("P-value") plt.show() .. image-sg:: /auto_examples/feature_selection/images/sphx_glr_LassoFS_001.png :alt: LassoFS :srcset: /auto_examples/feature_selection/images/sphx_glr_LassoFS_001.png :class: sphx-glr-single-img .. _sphx_glr_download_auto_examples_feature_selection_LassoFS.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: LassoFS.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: LassoFS.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: LassoFS.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_