Repository: mlflow/mlflow

Issue:
[BUG] import error _backported_all_estimators  from sklearn.utils.tests import ignore_warnings deprecated
### Issues Policy acknowledgement

- [X] I have read and agree to submit bug reports in accordance with the [issues policy](https://www.github.com/mlflow/mlflow/blob/master/ISSUE_POLICY.md)

### Where did you encounter this bug?

Local machine

### Willingness to contribute

Yes. I can contribute a fix for this bug independently.

### MLflow version

- Client: 2.9.2
- Tracking server: 1.x.y


### System information

- **OS Platform and Distribution (e.g., Linux Ubuntu 16.04)**: 
- **Python version**:
- **yarn version, if running the dev UI**:


### Describe the problem

The most recent version of sklearn, has removed the sklearn.utils.testing module.  Instead, use sklearn.utils._testing instead of it
mlflow.sklearn.utils._all_estimators() make cause error








### Tracking information

<!-- PLEASE KEEP BACKTICKS AND CHECK PREVIEW -->
```shell
REPLACE_ME
```


### Code to reproduce issue

<!-- PLEASE KEEP BACKTICKS AND CHECK PREVIEW -->
```
from sklearn import datasets
import mlflow
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

dataset = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(dataset.data, dataset.target, test_size=0.4)

clf = LogisticRegression()
clf.fit(X_train, y_train)

mlflow.sklearn.autolog()
```


### Stack trace

<!-- PLEASE KEEP BACKTICKS AND CHECK PREVIEW -->
```
/home/zhouyou/miniconda3/bin/python /home/zhouyou/PycharmProjects/mlflow_test/Chapter1/run1.py 
Traceback (most recent call last):
  File "/home/zhouyou/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/utils.py", line 858, in _all_estimators
    return all_estimators()
           ^^^^^^^^^^^^^^^^
  File "/home/zhouyou/miniconda3/lib/python3.11/site-packages/sklearn/utils/discovery.py", line 70, in all_estimators
    module = import_module(module_name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zhouyou/miniconda3/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/zhouyou/miniconda3/lib/python3.11/site-packages/sklearn/utils/metadata_routing.py", line 12, in <module>
    from ._metadata_requests import WARN, UNUSED, UNCHANGED  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zhouyou/miniconda3/lib/python3.11/site-packages/sklearn/utils/_metadata_requests.py", line 87, in <module>
    from ..exceptions import UnsetMetadataPassedError
ImportError: cannot import name 'UnsetMetadataPassedError' from 'sklearn.exceptions' (/home/zhouyou/miniconda3/lib/python3.11/site-packages/sklearn/exceptions.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/zhouyou/PycharmProjects/mlflow_test/Chapter1/run1.py", line 12, in <module>
    mlflow.sklearn.autolog()
  File "/home/zhouyou/miniconda3/lib/python3.11/site-packages/mlflow/utils/autologging_utils/__init__.py", line 424, in autolog
    return _autolog(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zhouyou/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/__init__.py", line 1261, in autolog
    _autolog(
  File "/home/zhouyou/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/__init__.py", line 1822, in _autolog
    estimators_to_patch = _gen_estimators_to_patch()
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zhouyou/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/__init__.py", line 98, in _gen_estimators_to_patch
    _, estimators_to_patch = zip(*_all_estimators())
                                  ^^^^^^^^^^^^^^^^^
  File "/home/zhouyou/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/utils.py", line 860, in _all_estimators
    return _backported_all_estimators()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zhouyou/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/utils.py", line 905, in _backported_all_estimators
    from sklearn.utils.testing import ignore_warnings
ModuleNotFoundError: No module named 'sklearn.utils.testing'

Process finished with exit code 1
```


### Other info / logs

<!-- PLEASE KEEP BACKTICKS AND CHECK PREVIEW -->
```
REPLACE_ME
```


### What component(s) does this bug affect?

- [ ] `area/artifacts`: Artifact stores and artifact logging
- [ ] `area/build`: Build and test infrastructure for MLflow
- [ ] `area/deployments`: MLflow Deployments client APIs, server, and third-party Deployments integrations
- [ ] `area/docs`: MLflow documentation pages
- [ ] `area/examples`: Example code
- [ ] `area/model-registry`: Model Registry service, APIs, and the fluent client calls for Model Registry
- [ ] `area/models`: MLmodel format, model serialization/deserialization, flavors
- [ ] `area/recipes`: Recipes, Recipe APIs, Recipe configs, Recipe Templates
- [ ] `area/projects`: MLproject format, project running backends
- [ ] `area/scoring`: MLflow Model server, model deployment tools, Spark UDFs
- [ ] `area/server-infra`: MLflow Tracking server backend
- [X] `area/tracking`: Tracking Service, tracking client APIs, autologging

### What interface(s) does this bug affect?

- [ ] `area/uiux`: Front-end, user experience, plotting, JavaScript, JavaScript dev server
- [ ] `area/docker`: Docker use across MLflow's components, such as MLflow Projects and MLflow Models
- [ ] `area/sqlalchemy`: Use of SQLAlchemy in the Tracking Service or Model Registry
- [ ] `area/windows`: Windows support

### What language(s) does this bug affect?

- [ ] `language/r`: R APIs and clients
- [ ] `language/java`: Java APIs and clients
- [ ] `language/new`: Proposals for new client languages

### What integration(s) does this bug affect?

- [ ] `integrations/azure`: Azure and Azure ML integrations
- [ ] `integrations/sagemaker`: SageMaker integrations
- [ ] `integrations/databricks`: Databricks integrations


Discussion:
@zhouyou9505 which version of sklearn are you using?
> ImportError: cannot import name 'UnsetMetadataPassedError' from 'sklearn.exceptions' (/home/zhouyou/miniconda3/lib/python3.11/site-packages/sklearn/exceptions.py)

Can you run the following command to see what this file looks like?

```
cat /home/zhouyou/miniconda3/lib/python3.11/site-packages/sklearn/exceptions.py
```

I was able to reproduce the exact the same error by **manually removing `UnsetMetadataPassedError` in `exceptions.py`:

```
>>> from sklearn.utils import all_estimators
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/sklearn/__init__.py", line 87, in <module>
    from .base import clone
  File "/usr/local/lib/python3.9/site-packages/sklearn/base.py", line 19, in <module>
    from .utils import _IS_32BIT
  File "/usr/local/lib/python3.9/site-packages/sklearn/utils/__init__.py", line 20, in <module>
    from . import _joblib, metadata_routing
  File "/usr/local/lib/python3.9/site-packages/sklearn/utils/metadata_routing.py", line 12, in <module>
    from ._metadata_requests import WARN, UNUSED, UNCHANGED  # noqa
  File "/usr/local/lib/python3.9/site-packages/sklearn/utils/_metadata_requests.py", line 87, in <module>
    from ..exceptions import UnsetMetadataPassedError
ImportError: cannot import name 'UnsetMetadataPassedError' from 'sklearn.exceptions' (/usr/local/lib/python3.9/site-packages/sklearn/exceptions.py)
```

`UnsetMetadataPassedError` should exist in `exceptions.py`. It looks like your sklearn package is collpased? Can you reinstall sklearn?

```
pip uninstall -y scikit-learn
pip install scikit-learn
```
Can you run the following code (plz make sure to only run this code)? If `ImportError: cannot import name 'UnsetMetadataPassedError' from 'sklearn.exceptions' (/home/zhouyou/miniconda3/lib/python3.11/site-packages/sklearn/exceptions.py)` occurs, this is not an mlflow issue but an sklearn issue or your sklearn is collapsed for some reason:

```python
from sklearn.utils import all_estimators

all_estimators()
```
> learn are you using?

scikit-learn              1.4.1.post1              pypi_0    pypi

> Can you run the following code (plz make sure to only run this code)? If `ImportError: cannot import name 'UnsetMetadataPassedError' from 'sklearn.exceptions' (/home/zhouyou/miniconda3/lib/python3.11/site-packages/sklearn/exceptions.py)` occurs, this is not an mlflow issue but an sklearn issue or your sklearn is collapsed for some reason:
> 
> ```python
> from sklearn.utils import all_estimators
> 
> all_estimators()
> ```

```
# Name                    Version                   Build  Channel
scikit-learn              1.3.0           py311ha02d727_1    defaults

```
I change the version of scikit-learn    from  1.4.1.post1  ->   1.3.0  , then my program works well.
but I find it it true that sklearn.utils.testing is deprecated

```
from mlflow.sklearn.utils import _backported_all_estimators

_backported_all_estimators()
```

```
Traceback (most recent call last):
  File "/home/zhouyou/PycharmProjects/mlflow_test/Chapter1/expcetion.py", line 4, in <module>
    _backported_all_estimators()
  File "/home/zhouyou/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/utils.py", line 905, in _backported_all_estimators
    from sklearn.utils.testing import ignore_warnings
ModuleNotFoundError: No module named 'sklearn.utils.testing'
```

Resolving patch:
diff --git a/mlflow/sklearn/utils.py b/mlflow/sklearn/utils.py
--- a/mlflow/sklearn/utils.py
+++ b/mlflow/sklearn/utils.py
@@ -918,7 +918,7 @@ def _backported_all_estimators(type_filter=None):
         RegressorMixin,
         TransformerMixin,
     )
-    from sklearn.utils.testing import ignore_warnings
+    from sklearn.utils._testing import ignore_warnings
 
     IS_PYPY = platform.python_implementation() == "PyPy"
 


Test patch: