7

# 'percentiles' must be a sequence of 2 elements.

Package:

scikit-learn

47032

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:
```

## Links to the raise (1)

https://github.com/scikit-learn/scikit-learn/blob/c67518350f91072f9d37ed09c5ef7edf555b6cf6/sklearn/inspection/_partial_dependence.py#L72## Ways to fix

3

_partial_dependence. _grid_from_X is used to Generate a grid of points based on the percentiles of X. The

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.*percentiles*

## 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])]

### Add a possible fix

Please authorize to post fix