社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

在Python中使用Seabn绘图时,如何修正“未来警告:使用非元组序列进行多维索引”?[副本]

Shubham Gupta • 5 年前 • 1966 次点击  

我已搜索过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 ,肖伯恩: 0.9.0 ,matplotlib: 2.2.3 ,科学: 1.1.0 ,努比: 1.15.0'

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49327
 
1966 次点击  
文章 [ 4 ]  |  最新文章 5 年前
Sasha Tsukanov Andrés Rojas
Reply   •   1 楼
Sasha Tsukanov Andrés Rojas    6 年前

我遇到了同样的警告。我更新了scipy,pandas和numpy。我还记得。我用的时候就知道了 seaborn.pairplot 使用kde,它下面使用 seaborn.kdeplot .

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

import warnings

with warnings.catch_warnings():

    your_code_block
Sarah
Reply   •   2 楼
Sarah    6 年前

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

pip install --upgrade scipy --user

如果在其他海生地块中仍然收到警告,可以事先运行以下命令。这对Jupyter笔记本很有帮助,因为警告会让报告看起来很糟糕,即使你的情节很好。

import warnings
warnings.filterwarnings("ignore")
NetworkMeister
Reply   •   3 楼
NetworkMeister    6 年前

为了 python>=3.7 你需要升级你的 scipy>=1.2 .

hpaulj
Reply   •   4 楼
hpaulj    6 年前

完整的回溯就好了。我猜是的 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: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]` instead of `arr[seq]`