Closed
Description
Describe the bug
When using MinMaxScaler.fit_transform() from scikit-learn, if all elements in a column of data are the same, the scaler transforms these elements to zeros. This behavior might not be intuitive or desired in some cases, as users might expect a different treatment for constant columns (e.g., transforming to ones or maintaining the constant value). If this behavior is expected, feel free to close this issue!
Steps/Code to Reproduce
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# Sample DataFrame with constant columns
df = pd.DataFrame({
'A': [5, 5, 5], # Constant column
'B': [1, 2, 3] # Varying column
})
# Apply MinMaxScaler
scaler = MinMaxScaler()
scaled_df = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)
print(scaled_df)
Expected Results
It might be more intuitive if constant columns are scaled to a non-zero constant value (e.g., all ones) or maintain their original value.
Actual Results
The constant column ('A') is transformed to all zeros.
A B
0 0.0 0.0
1 0.0 0.5
2 0.0 1.0
Versions
System:
python: 3.9.17 (main, Jul 5 2023, 20:41:20) [GCC 11.2.0]
executable: /home/guihuan/.conda/envs/py39/bin/python
machine: Linux-5.15.0-86-generic-x86_64-with-glibc2.35
Python dependencies:
sklearn: 1.3.0
pip: 23.2.1
setuptools: 68.0.0
numpy: 1.24.3
scipy: 1.11.2
Cython: None
pandas: 2.1.0
matplotlib: 3.7.2
joblib: 1.3.2
threadpoolctl: 3.2.0
Built with OpenMP: True
threadpoolctl info:
user_api: blas
internal_api: openblas
num_threads: 32
prefix: libopenblas
filepath: /home/guihuan/.conda/envs/py39/lib/python3.9/site-packages/numpy.libs/libopenblas64_p-r0-15028c96.3.21.so
version: 0.3.21
threading_layer: pthreads
architecture: Haswell
user_api: openmp
internal_api: openmp
num_threads: 32
prefix: libgomp
filepath: /home/guihuan/.conda/envs/py39/lib/python3.9/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
version: None
user_api: blas
internal_api: openblas
num_threads: 32
prefix: libopenblas
filepath: /home/guihuan/.conda/envs/py39/lib/python3.9/site-packages/scipy.libs/libopenblasp-r0-23e5df77.3.21.dev.so
version: 0.3.21.dev
threading_layer: pthreads
architecture: Haswell