mlxtend icon indicating copy to clipboard operation
mlxtend copied to clipboard

minmax_scaling returns NaN silently for constant columns

Open sachinn854 opened this issue 6 months ago • 2 comments

Describe the bug

minmax_scaling silently returns NaN for any column where all values are identical (constant column). No error is raised — only a low-level NumPy RuntimeWarning: invalid value encountered in divide is emitted, which is easy to miss. The function computes 0 / 0 internally and writes NaN into the output without informing the user.

Notably, the sibling function standardize() in the same file already handles this edge case correctly (it sets constant columns to 0.0), but minmax_scaling does not.

Steps/Code to Reproduce

import numpy as np
from mlxtend.preprocessing import minmax_scaling

# Column 0 is constant (all 5s), Column 1 is normal
ary = np.array([[5, 1],
                [5, 2],
                [5, 3]])

result = minmax_scaling(ary, columns=[0, 1])
print(result)

Expected Results

Constant columns should either:

  • be set to 0.0 (like standardize() does), or
  • raise a clear ValueError explaining that the column has zero range.
[[0.  0. ]
 [0.  0.5]
 [0.  1. ]]

Actual Results

Column 0 is silently filled with NaN:

[[nan  0. ]
 [nan  0.5]
 [nan  1. ]]
RuntimeWarning: invalid value encountered in divide

Versions

MLxtend 0.23.4
Windows-11-10.0.26200-SP0
Python 3.13.5 (tags/v3.13.5:6cb20a2, Jun 11 2025, 16:15:46) [MSC v.1943 64 bit (AMD64)]
Scikit-learn 1.7.0
NumPy 2.2.1
SciPy 1.15.2

sachinn854 avatar May 21 '26 09:05 sachinn854

Hi @rasbt

I'm Karthik, a B.Tech AI & ML student from CBIT Hyderabad, India, currently building my open source contribution skills.

I'd like to fix this issue. After looking at the code in mlxtend/preprocessing/scaling.py, I noticed that standardize() already handles constant columns correctly. I've applied a similar approach to minmax_scaling().

My approach: Raise a clear ValueError when a constant column is detected (denominator == 0), rather than silently returning NaN. This is safer for ML pipelines where silent data corruption can be hard to debug.

I've already implemented and tested the fix locally — raising a PR now.

karthikankam avatar May 24 '26 14:05 karthikankam

@rasbt, there are three duplicate PRs #1168 #1169 #1171 for the same issue.

sachinn854 avatar May 24 '26 15:05 sachinn854