Skip to content

minmax_scaling returns NaN silently for constant columns #1167

@sachinn854

Description

@sachinn854

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions