FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]`
Asked Answered
D

6

30

I have searched S/O but I couldn't find a answer for this.

When I try to plot a distribution plot using seaborn I am getting a futurewarning. I was wondering what could be the issue here.

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
% matplotlib inline
from sklearn import datasets

iris = datasets.load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['class'] = iris.target
df['species'] = df['class'].map({idx:s for idx, s in enumerate(iris.target_names)})


fig, ((ax1,ax2),(ax3,ax4))= plt.subplots(2,2, figsize =(13,9))
sns.distplot(a = df.iloc[:,0], ax=ax1)
sns.distplot(a = df.iloc[:,1], ax=ax2)
sns.distplot(a = df.iloc[:,2], ax=ax3)
sns.distplot(a = df.iloc[:,3], ax=ax4)
plt.show()

This is the warning:

C:\ProgramData\Anaconda3\lib\site-packages\scipy\stats\stats.py:1713:
FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; 
use `arr[tuple(seq)]` instead of `arr[seq]`. 
In the future this will be interpreted as an array index, `arr[np.array(seq)]`,
which will result either in an error or a different result.
return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval

Any help? You can run the above code. You'll get the warning.

Pandas : 0.23.4, seaborn : 0.9.0, matplotlib : 2.2.3, scipy : 1.1.0, numpy: 1.15.0'

Dg answered 1/10, 2018 at 15:22 Comment(7)
I don't get that warning when running the code. Maybe you want to share which versions of pandas, matplotlib, scipy, numpy and seaborn you are using? I suppose updating them all would prevent this from happening.Disagree
@Disagree Pandas : 0.23.4 and seaborn : 0.9.0, matplotlib : 2.2.3, scipy : 1.1.0, numpy : 1.15.0'Dg
Ok this is because of numpy 1.15 in use (I'd not occur with numpy 1.14.6). I might look a bit deeper which package brings up this problem later.Disagree
So a minimal reproducible example is sns.kdeplot(data = [1,3,4]). This is hence a problem somewhere in seaborn I suppose.Disagree
@Disagree Do I have to install older version of seaborn so this warning get fixed? or can I keep using these versions?Dg
No, an older seaborn version will not fix this. To get rid of the warning you can install an older numpy version (e.g. 1.14.6); but the warning is not harmful as of now. One would hope that scipy releases a new version before numpy removes the list support for indexing. I would be pretty confident that this will happen.Disagree
@Disagree Thank you for all the help :)Dg
O
13

A fuller traceback would be nice. My guess is that seaborn.distplot is using scipy.stats to calculate something. The error occurs in

def _compute_qth_percentile(sorted, per, interpolation_method, axis):
    ....
    indexer = [slice(None)] * sorted.ndim
    ...
    indexer[axis] = slice(i, i + 2)
    ...
    return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval

So in this last line, the list indexer is used to slice sorted.

In [81]: x = np.arange(12).reshape(3,4)
In [83]: indexer = [slice(None), slice(None,2)]
In [84]: x[indexer]
/usr/local/bin/ipython3:1: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  #!/usr/bin/python3
Out[84]: 
array([[0, 1],
       [4, 5],
       [8, 9]])
In [85]: x[tuple(indexer)]
Out[85]: 
array([[0, 1],
       [4, 5],
       [8, 9]])

Using a list of slices works, but the plan is to depreciate in the future. Indexes that involve several dimensions are supposed to be tuples. The use of lists in the context is an older style that is being phased out.

So the scipy developers need to fix this. This isn't something end users should have to deal with. But for now, don't worry about the futurewarning. It doesn't affect the calculations or plotting. There is a way of suppressing future warnings, but I don't know it off hand.

FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]` instead of `arr[seq]`

Organic answered 1/10, 2018 at 16:45 Comment(4)
Thanks for the info :) I was thinking it was something that I did was causing the error. Hence I wanted to know what caused the error and how can I fix it.Dg
This is a good explanation, although no solution was provided. See @Colb solution - simply upgrade scipy. The bug was fixed in previous releases.Pyrogallol
I found this rather confusing. Did they put up some explanation on in what cases the results are different? Because it is said which will result either in an error or a different result.. An error is probably fine because it alerts the user about the issue, but a silent different result concerns me more.Bingo
@Jason: Notice that np.array([[1,2]]) is a two dimension array. While tuple([[1,2]]) is a tuple where the first component is a one dimension list. Currently, when a >1d-list is passed into the index of a np.array, the latter approach is adopted. But in the future the first approach will be adopted: i.e. wrapping with np.array(...).Azeria
C
22

For python>=3.7 you need to upgrade your scipy>=1.2.

Colb answered 14/1, 2019 at 15:28 Comment(2)
I agree with this solution. In SciPy 1.2 this works as intended. The warning in previous version was probably due to an incomplete code.Muzz
In addition to SciPy, this also applies to skimage.Marmolada
O
13

A fuller traceback would be nice. My guess is that seaborn.distplot is using scipy.stats to calculate something. The error occurs in

def _compute_qth_percentile(sorted, per, interpolation_method, axis):
    ....
    indexer = [slice(None)] * sorted.ndim
    ...
    indexer[axis] = slice(i, i + 2)
    ...
    return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval

So in this last line, the list indexer is used to slice sorted.

In [81]: x = np.arange(12).reshape(3,4)
In [83]: indexer = [slice(None), slice(None,2)]
In [84]: x[indexer]
/usr/local/bin/ipython3:1: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  #!/usr/bin/python3
Out[84]: 
array([[0, 1],
       [4, 5],
       [8, 9]])
In [85]: x[tuple(indexer)]
Out[85]: 
array([[0, 1],
       [4, 5],
       [8, 9]])

Using a list of slices works, but the plan is to depreciate in the future. Indexes that involve several dimensions are supposed to be tuples. The use of lists in the context is an older style that is being phased out.

So the scipy developers need to fix this. This isn't something end users should have to deal with. But for now, don't worry about the futurewarning. It doesn't affect the calculations or plotting. There is a way of suppressing future warnings, but I don't know it off hand.

FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]` instead of `arr[seq]`

Organic answered 1/10, 2018 at 16:45 Comment(4)
Thanks for the info :) I was thinking it was something that I did was causing the error. Hence I wanted to know what caused the error and how can I fix it.Dg
This is a good explanation, although no solution was provided. See @Colb solution - simply upgrade scipy. The bug was fixed in previous releases.Pyrogallol
I found this rather confusing. Did they put up some explanation on in what cases the results are different? Because it is said which will result either in an error or a different result.. An error is probably fine because it alerts the user about the issue, but a silent different result concerns me more.Bingo
@Jason: Notice that np.array([[1,2]]) is a two dimension array. While tuple([[1,2]]) is a tuple where the first component is a one dimension list. Currently, when a >1d-list is passed into the index of a np.array, the latter approach is adopted. But in the future the first approach will be adopted: i.e. wrapping with np.array(...).Azeria
D
6

I was running seaborn.regplot, and got rid of the warning by upgrading scipy 1.2 as NetworkMeister suggested.

pip install --upgrade scipy --user

If you still get warnings in other seaborn plots, you can run the following beforehand. This is helpful in Jupyter Notebook because the warnings kind of make the report look bad even if your plots are great.

import warnings
warnings.filterwarnings("ignore")
Drava answered 24/2, 2019 at 20:55 Comment(0)
C
2

I came across the same warning. I updated scipy,pandas and numpy. I still get it.I get it when i use seaborn.pairplot with kde, which underneath uses seaborn.kdeplot.

If you want to get rid off the warning you can use warnings library. Ex:

import warnings

with warnings.catch_warnings():

    your_code_block
Corrasion answered 31/10, 2018 at 11:43 Comment(1)
Still getting the warning.Endosperm
E
0

Working example :

import numpy as np
import warnings

x  = np.random.normal(size=100)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    
    sns.distplot(x, hist=False, rug=True, color="r");
Endosperm answered 19/3, 2021 at 0:27 Comment(0)
S
0

Updating Scipy, pip, and Numpy resolved this issue for me.

Swett answered 16/12, 2023 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.