votes up 7

'percentiles' must be a sequence of 2 elements.

Package:
Exception Class:
ValueError

Raise code

""" 

    values : list of 1d ndarrays
        The values with which the grid has been created. The size of each
        array ``values[j]`` is either ``grid_resolution``, or the number of
        unique values in ``X[:, j]``, whichever is smaller.
    """
    if not isinstance(percentiles, Iterable) or len(percentiles) != 2:
        raise ValueError("'percentiles' must be a sequence of 2 elements.")
    if not all(0 <= x <= 1 for x in percentiles):
        raise ValueError("'percentiles' values must be in [0, 1].")
    if percentiles[0] >= percentiles[1]:
        raise ValueError('percentiles[0] must be strictly less '
                         'than percentiles[1].')

    if grid_resolution <= 1:

Ways to fix

votes up 3 votes down

_partial_dependence. _grid_from_X is used to Generate a grid of points based on the percentiles of X. The percentiles parameter is used to specify the percentiles which are used to construct the extreme values of the grid should be a tuple of 2 float elements.

Reproducing the error:


pipenv install sklearn numpy

import numpy as np
from sklearn.inspection._partial_dependence import _grid_from_X
percentiles = (.05, .90,0.05)
grid_resolution = 100
X = np.asarray([[1, 2],[3, 4]])


grid, axes = _grid_from_X(X, percentiles, grid_resolution)
print(grid,axes)

The error:

Fixed version of the code:

import numpy as np
from sklearn.inspection._partial_dependence import _grid_from_X
percentiles = (.05, .95,)
grid_resolution = 100
X = np.asarray([[1, 2],[3, 4]])


grid, axes = _grid_from_X(X, percentiles, grid_resolution)
print(grid,axes)

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-37-088e218c99c1> in <module>()
      5 X = np.asarray([[1, 2],[3, 4]])
      6 
----> 7 grid, axes = _grid_from_X(X, percentiles, grid_resolution)
      8 print(grid,axes)


/usr/local/lib/python3.7/dist-packages/sklearn/inspection/_partial_dependence.py in _grid_from_X(X, percentiles, grid_resolution)
     71     """
     72     if not isinstance(percentiles, Iterable) or len(percentiles) != 2:
---> 73         raise ValueError("'percentiles' must be a sequence of 2 elements.")
     74     if not all(0 <= x <= 1 for x in percentiles):
     75         raise ValueError("'percentiles' values must be in [0, 1].")


ValueError: 'percentiles' must be a sequence of 2 elements.

Output:

[[1 2]
 [1 4]
 [3 2]
 [3 4]] [array([1, 3]), array([2, 4])]

Jul 17, 2021 kellemnegasi answer
kellemnegasi 22.6k

Add a possible fix

Please authorize to post fix