pandas FutureWarning:不推荐使用非元组序列进行多维索引,使用 `arr[tuple(seq)]`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52594235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]`
提问by user_6396
I have searched S/O but I couldn't find a answer for this.
我已经搜索过 S/O,但我找不到答案。
When I try to plot a distribution plot using seaborn I am getting a futurewarning. I was wondering what could be the issue here.
当我尝试使用 seaborn 绘制分布图时,我收到了未来警告。我想知道这里可能是什么问题。
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'
Pandas : 0.23.4
, seaborn : 0.9.0
, matplotlib : 2.2.3
, scipy : 1.1.0
, numpy:1.15.0'
采纳答案by hpaulj
A fuller traceback would be nice. My guess is that seaborn.distplot
is using scipy.stats
to calculate something. The error occurs in
更完整的追溯会很好。我的猜测是,seaborn.distplot
使用scipy.stats
计算的东西。错误发生在
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
.
所以在最后一行中,列表indexer
用于切片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.
所以scipy
开发人员需要解决这个问题。这不是最终用户应该处理的事情。但是现在,不要担心futurewarning
. 它不影响计算或绘图。有一种方法可以抑制未来的警告,但我不知道。
FutureWarning:不推荐使用非元组序列进行多维索引,使用 `arr[tuple(seq)]` 而不是 `arr[seq]`
回答by NetworkMeister
For python>=3.7
you need to upgrade your scipy>=1.2
.
因为python>=3.7
您需要升级您的scipy>=1.2
.
回答by Sarah
I was running seaborn.regplot, and got rid of the warning by upgrading scipy 1.2 as NetworkMeister suggested.
我正在运行 seaborn.regplot,并按照 NetworkMeister 的建议通过升级 scipy 1.2 来消除警告。
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.
如果您在其他 seaborn 图中仍然收到警告,您可以预先运行以下命令。这在 Jupyter Notebook 中很有用,因为即使您的绘图很棒,警告也会使报告看起来很糟糕。
import warnings
warnings.filterwarnings("ignore")
回答by Andrés Rojas
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
.
我遇到了同样的警告。我更新了 scipy、pandas 和 numpy。我仍然得到它。当我使用seaborn.pairplot
kde时我得到它,它在下面使用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