-
-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Closed
Labels
Description
Describe the bug
Using SelectFromModel
with the automatic ElasticNetCV
does not work if the l1_ratio
is estimated from the data, i.e., if the user provides a list of floats.
Steps/Code to Reproduce
from sklearn.datasets import make_regression
from sklearn.feature_selection import SelectFromModel
from sklearn.linear_model import ElasticNetCV
estimator = ElasticNetCV(
l1_ratio=[0.25, 0.5, 0.75]
)
model = SelectFromModel(estimator=estimator)
X, y = make_regression(n_samples=100, n_features=5, n_informative=3)
model.fit(X, y)
model.get_feature_names_out()
This fails with:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
because _calculate_threshold
calls np.isclose(estimator.l1_ratio, 1.0)
which returns an array with as many elements as l1 ratios.
Expected Results
Calling .get_feature_names_out()
should return an ndarray of str according to the best model estimating with CV.
Actual Results
Traceback (most recent call last):
File "/.venv/lib/python3.12/site-packages/IPython/core/interactiveshell.py", line 3577, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-12-304146ab06de>", line 1, in <module>
model.get_feature_names_out()
File "/.venv/lib/python3.12/site-packages/sklearn/feature_selection/_base.py", line 190, in get_feature_names_out
return input_features[self.get_support()]
^^^^^^^^^^^^^^^^^^
File "/.venv/lib/python3.12/site-packages/sklearn/feature_selection/_base.py", line 67, in get_support
mask = self._get_support_mask()
^^^^^^^^^^^^^^^^^^^^^^^^
File "/.venv/lib/python3.12/site-packages/sklearn/feature_selection/_from_model.py", line 305, in _get_support_mask
threshold = _calculate_threshold(estimator, scores, self.threshold)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/.venv/lib/python3.12/site-packages/sklearn/feature_selection/_from_model.py", line 36, in _calculate_threshold
if is_l1_penalized or is_lasso or is_elasticnet_l1_penalized:
^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Versions
System:
python: 3.12.9 (main, Feb 4 2025, 14:38:38) [Clang 16.0.0 (clang-1600.0.26.6)]
executable: /.venv/bin/python
machine: macOS-15.1.1-arm64-arm-64bit
Python dependencies:
sklearn: 1.5.2
pip: None
setuptools: 75.6.0
numpy: 1.26.4
scipy: 1.14.1
Cython: None
pandas: 2.2.3
matplotlib: 3.9.3
joblib: 1.4.2
threadpoolctl: 3.5.0
Built with OpenMP: True
threadpoolctl info:
user_api: blas
internal_api: openblas
num_threads: 8
prefix: libopenblas
filepath: /.venv/lib/python3.12/site-packages/numpy/.dylibs/libopenblas64_.0.dylib
version: 0.3.23.dev
threading_layer: pthreads
architecture: armv8
user_api: openmp
internal_api: openmp
num_threads: 8
prefix: libomp
filepath: /.venv/lib/python3.12/site-packages/sklearn/.dylibs/libomp.dylib
version: None