Skip to content

Shape may mismatch in IterativeImputer if set min_value with array-like input and some features contain all missing values #29355

Closed

Description

@xuefeng-xu

Describe the bug

In all Imputers, features with all missing values will be droped if keep_empty_features is set to False.

However, in IterativeImputer, if we set min_value and max_value with array-like input, it might raise an error indicate that the shape mismatch.

We can manually reduce the elements in min_value and max_value after we check if there is any features with all missing values, I wonder if this can be done automatically.

Steps/Code to Reproduce

import numpy as np
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer

X = [[7, 2, np.nan], [4, np.nan, np.nan], [10, 5, np.nan]]
imp = IterativeImputer(min_value=[1,2,3])
imp.fit_transform(X)

Expected Results

No error raised.

Actual Results

/Users/xxf/code/scikit-learn/sklearn/impute/_base.py:597: UserWarning: Skipping features without any observed values: [2]. At least one non-missing value is needed for imputation with strategy='mean'.
  warnings.warn(
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/xxf/code/scikit-learn/sklearn/utils/_set_output.py", line 313, in wrapped
    data_to_wrap = f(self, X, *args, **kwargs)
  File "/Users/xxf/code/scikit-learn/sklearn/base.py", line 1514, in wrapper
    return fit_method(estimator, *args, **kwargs)
  File "/Users/xxf/code/scikit-learn/sklearn/impute/_iterative.py", line 759, in fit_transform
    self._min_value = self._validate_limit(self.min_value, "min", X.shape[1])
  File "/Users/xxf/code/scikit-learn/sklearn/impute/_iterative.py", line 682, in _validate_limit
    raise ValueError(
ValueError: 'min_value' should be of shape (2,) when an array-like is provided. Got (3,), instead.

Versions

System:
    python: 3.9.16 | packaged by conda-forge | (main, Feb  1 2023, 21:38:11)  [Clang 14.0.6 ]
executable: /Users/xxf/miniconda3/envs/sklearn-env/bin/python
   machine: macOS-14.5-arm64-arm-64bit

Python dependencies:
      sklearn: 1.6.dev0
          pip: 23.2.1
   setuptools: 68.0.0
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: 3.0.8
       pandas: 2.1.0
   matplotlib: 3.7.2
       joblib: 1.3.0
threadpoolctl: 3.5.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 8
         prefix: libopenblas
       filepath: /Users/xxf/miniconda3/envs/sklearn-env/lib/libopenblas.0.dylib
        version: 0.3.23
threading_layer: openmp
   architecture: VORTEX

       user_api: openmp
   internal_api: openmp
    num_threads: 8
         prefix: libomp
       filepath: /Users/xxf/miniconda3/envs/sklearn-env/lib/libomp.dylib
        version: None

Activity

  1. added
    Needs Triage
    on Jun 27, 2024
  2. lesteve commented on Jul 5, 2024

    @lesteve
    Member

    Thanks for your issue and the reproducer.

    I guess it seems feasible, but you would need to have a closer look at the code and try to do it! Setting the label "help wanted" on this. Also "Easy" although this is a bit of a guess. Withouth having a closer look it may well be more complicated than it looks ...

    Maybe the dropping the features happens first and min_features happens afterwards and it should be the other way around, or you could track the number of features after and before dropping. Maybe it is more complicated than this 😉.

  3. added
    Easy
    and removed
    Needs Triage
    on Jul 5, 2024
  4. gunsodo commented on Jul 6, 2024

    @gunsodo
    Contributor

    @lesteve Would love to take this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugEasyhelp wantedmodule:impute

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions