28

我已经搜索了 S/O,但找不到答案。

当我尝试使用 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()

这是警告:

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

有什么帮助吗?你可以运行上面的代码。你会收到警告。

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

4

5 回答 5

22

因为python>=3.7您需要升级您的scipy>=1.2.

于 2019-01-14T15:28:35.830 回答
11

更完整的回溯会很好。我的猜测是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

所以在最后一行中,列表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]])

使用切片列表是可行的,但计划是在未来贬值。涉及多个维度的索引应该是元组。在上下文中使用列表是一种正在逐步淘汰的旧样式。

所以scipy开发人员需要解决这个问题。这不是最终用户应该处理的事情。但现在,不用担心futurewarning. 它不影响计算或绘图。有一种方法可以抑制未来的警告,但我不知道。

FutureWarning:不推荐使用非元组序列进行多维索引,使用 `arr[tuple(seq)]` 而不是 `arr[seq]`

于 2018-10-01T16:45:56.510 回答
6

我正在运行 seaborn.regplot,并按照 NetworkMeister 的建议通过升级 scipy 1.2 来消除警告。

pip install --upgrade scipy --user

如果您仍然在其他 seaborn 图中收到警告,您可以事先运行以下命令。这在 Jupyter Notebook 中很有帮助,因为即使您的绘图很棒,警告也会使报告看起来很糟糕。

import warnings
warnings.filterwarnings("ignore")
于 2019-02-24T20:55:42.520 回答
2

我遇到了同样的警告。我更新了 scipy、pandas 和 numpy。我仍然明白。当我seaborn.pairplot与 kde 一起使用时,我明白了,它在下面使用seaborn.kdeplot.

如果你想摆脱警告,你可以使用警告库。前任:

import warnings

with warnings.catch_warnings():

    your_code_block
于 2018-10-31T11:43:53.677 回答
0

工作示例:

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");
于 2021-03-19T00:27:50.653 回答