| {"organization": "scikit-learn", "repo_name": "scikit-learn", "base_commit": "559609fe98ec2145788133687e64a6e87766bc77", "is_iss": 0, "iss_html_url": "https://github.com/scikit-learn/scikit-learn/issues/25525", "iss_label": "Bug\nmodule:feature_extraction", "title": "Extend SequentialFeatureSelector example to demonstrate how to use negative tol", "body": "### Describe the bug\r\n\r\nI utilized the **SequentialFeatureSelector** for feature selection in my code, with the direction set to \"backward.\" The tolerance value is negative and the selection process stops when the decrease in the metric, AUC in this case, is less than the specified tolerance. Generally, increasing the number of features results in a higher AUC, but sacrificing some features, especially correlated ones that offer little contribution, can produce a pessimistic model with a lower AUC. The code worked as expected in **sklearn 1.1.1**, but when I updated to **sklearn 1.2.1**, I encountered the following error.\r\n\r\n### Steps/Code to Reproduce\r\n\r\n```python\r\nfrom sklearn.datasets import load_breast_cancer\r\nfrom sklearn.linear_model import LogisticRegression\r\nfrom sklearn.feature_selection import SequentialFeatureSelector\r\nfrom sklearn.preprocessing import StandardScaler\r\nfrom sklearn.pipeline import Pipeline\r\n\r\nX, y = load_breast_cancer(return_X_y=True)\r\n\r\nTOL = -0.001\r\nfeature_selector = SequentialFeatureSelector(\r\n LogisticRegression(max_iter=1000),\r\n n_features_to_select=\"auto\",\r\n direction=\"backward\",\r\n scoring=\"roc_auc\",\r\n tol=TOL\r\n )\r\n\r\n\r\npipe = Pipeline(\r\n [('scaler', StandardScaler()), \r\n ('feature_selector', feature_selector), \r\n ('log_reg', LogisticRegression(max_iter=1000))]\r\n )\r\n\r\n\r\n\r\nif __name__ == \"__main__\":\r\n pipe.fit(X, y)\r\n print(pipe['log_reg'].coef_[0])\r\n\r\n```\r\n\r\n### Expected Results\r\n\r\n```\r\n$ python sfs_tol.py \r\n[-2.0429818 0.5364346 -1.35765488 -2.85009904 -2.84603016]\r\n```\r\n\r\n### Actual Results\r\n\r\n```python-traceback\r\n$ python sfs_tol.py \r\nTraceback (most recent call last):\r\n File \"/home/modelling/users-workspace/nsofinij/lab/open-source/sfs_tol.py\", line 28, in <module>\r\n pipe.fit(X, y)\r\n File \"/home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/sklearn/pipeline.py\", line 401, in fit\r\n Xt = self._fit(X, y, **fit_params_steps)\r\n File \"/home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/sklearn/pipeline.py\", line 359, in _fit\r\n X, fitted_transformer = fit_transform_one_cached(\r\n File \"/home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/joblib/memory.py\", line 349, in __call__\r\n return self.func(*args, **kwargs)\r\n File \"/home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/sklearn/pipeline.py\", line 893, in _fit_transform_one\r\n res = transformer.fit_transform(X, y, **fit_params)\r\n File \"/home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/sklearn/utils/_set_output.py\", line 142, in wrapped\r\n data_to_wrap = f(self, X, *args, **kwargs)\r\n File \"/home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/sklearn/base.py\", line 862, in fit_transform\r\n return self.fit(X, y, **fit_params).transform(X)\r\n File \"/home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/sklearn/feature_selection/_sequential.py\", line 201, in fit\r\n self._validate_params()\r\n File \"/home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/sklearn/base.py\", line 581, in _validate_params\r\n validate_parameter_constraints(\r\n File \"/home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/sklearn/utils/_param_validation.py\", line 97, in validate_parameter_constraints\r\n raise InvalidParameterError(\r\nsklearn.utils._param_validation.InvalidParameterError: The 'tol' parameter of SequentialFeatureSelector must be None or a float in the range (0, inf). Got -0.001 instead.\r\n\r\n```\r\n\r\n### Versions\r\n\r\n```shell\r\nSystem:\r\n python: 3.10.8 | packaged by conda-forge | (main, Nov 22 2022, 08:26:04) [GCC 10.4.0]\r\nexecutable: /home/modelling/opt/anaconda3/envs/py310/bin/python\r\n machine: Linux-4.14.301-224.520.amzn2.x86_64-x86_64-with-glibc2.26\r\n\r\nPython dependencies:\r\n sklearn: 1.2.1\r\n pip: 23.0\r\n setuptools: 66.1.1\r\n numpy: 1.24.1\r\n scipy: 1.10.0\r\n Cython: None\r\n pandas: 1.5.3\r\n matplotlib: 3.6.3\r\n joblib: 1.2.0\r\nthreadpoolctl: 3.1.0\r\n\r\nBuilt with OpenMP: True\r\n\r\nthreadpoolctl info:\r\n user_api: openmp\r\n internal_api: openmp\r\n prefix: libgomp\r\n filepath: /home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0\r\n version: None\r\n num_threads: 64\r\n\r\n user_api: blas\r\n internal_api: openblas\r\n prefix: libopenblas\r\n filepath: /home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-15028c96.3.21.so\r\n version: 0.3.21\r\nthreading_layer: pthreads\r\n architecture: SkylakeX\r\n num_threads: 64\r\n\r\n user_api: blas\r\n internal_api: openblas\r\n prefix: libopenblas\r\n filepath: /home/modelling/opt/anaconda3/envs/py310/lib/python3.10/site-packages/scipy.libs/libopenblasp-r0-41284840.3.18.so\r\n version: 0.3.18\r\nthreading_layer: pthreads\r\n architecture: SkylakeX\r\n num_threads: 64\r\n```\r\n", "code": null, "pr_html_url": "https://github.com/scikit-learn/scikit-learn/pull/26205", "commit_html_url": null, "file_loc": {"base_commit": "559609fe98ec2145788133687e64a6e87766bc77", "files": [{"path": "examples/feature_selection/plot_select_from_model_diabetes.py", "status": "modified", "Loc": {"(None, None, None)": {"add": [145], "mod": [123, 124, 125]}}}]}, "own_code_loc": [], "ass_file_loc": [], "other_rep_loc": [], "analysis": {"iss_type": "1", "iss_reason": "1", "loc_way": "pr", "loc_scope": "", "info_type": ""}, "max_topk": 1, "file_topk": 1, "loctype": {"code": ["examples/feature_selection/plot_select_from_model_diabetes.py"], "doc": [], "test": [], "config": [], "asset": []}} | |