Better error message when using invalid `MinMaxScaler.fit(...)` inputs
- Dominant language
- Python
- Stars
- 951
- Forks
- 262
- PR merge metrics
- No merged PRs in 30d
Description
Here's a small example that shows `MinMaxScaler.fit(...)` not working with a Dask Series
```python
import pandas as pd
import dask.dataframe as dd
from dask_ml.preprocessing import MinMaxScaler
# Create data
df = pd.DataFrame({"A": range(10), "B": range(10, 20)})
ddf = dd.from_pandas(df, npartitions=2)
# Use MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(ddf["A"])
```
We get the following traceback:
```python
Traceback (most recent call last):
File "/Users/james/projects/dask/dask/test-scaler.py", line 11, in
scaler.fit(ddf["A"])
File "/Users/james/projects/dask/dask-ml/dask_ml/preprocessing/data.py", line 144, in fit
scale = (feature_range[1] - feature_range[0]) / handle_zeros_in_scale(
File "/Users/james/projects/dask/dask-ml/dask_ml/utils.py", line 71, in handle_zeros_in_scale
scale = scale.copy()
AttributeError: 'Scalar' object has no attribute 'copy'
```
I don't necessarily expect this to work as when I run the same example but using `sklearn` / `pandas` instead of `dask-ml` / `dask.dataframe`, I still get an error, but with a more informative message
```python
Traceback (most recent call last):
File "/Users/james/projects/dask/dask/test-scaler.py", line 12, in
scaler.fit(df["A"])
File "/Users/james/mambaforge/envs/dask/lib/python3.10/site-packages/sklearn/preprocessing/_data.py", line 420, in fit
return self.partial_fit(X, y)
File "/Users/james/mambaforge/envs/dask/lib/python3.10/site-packages/sklearn/preprocessing/_data.py", line 457, in partial_fit
X = self._validate_data(
File "/Users/james/mambaforge/envs/dask/lib/python3.10/site-packages/sklearn/base.py", line 577, in _validate_data
X = check_array(X, input_name="X", **check_params)
File "/Users/james/mambaforge/envs/dask/lib/python3.10/site-packages/sklearn/utils/validation.py", line 879, in check_array
raise ValueError(
ValueError: Expected 2D array, got 1D array instead:
array=[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
```
It'd be great if we could have a similar error message for `dask-ml`s `MinMaxScalar`.
Contributor guide
Assessment
This issue has not been assessed yet.