程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

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

发布于2019-09-01 20:07     阅读(878)     评论(0)     点赞(13)     收藏(4)


我不想将非元组序列用于多维索引,以便脚本在此更改时支持Python的未来版本。

下面是我用于绘制图形的代码:

data = np.genfromtxt(Example.csv,delimiter=',', dtype=None, names=True, 
    converters={0: str2date})

p1, = host.plot(data["column_1"], data["column_2"], "b-", label="column_2")
p2, = par1.plot(data["column_1"], data['column_3'], "r-", label="column_3")
p3, = par2.plot(data["column_1"], data["column_4"], "g-", label="column_4")

host.set_xlim([data["column_1"][0], data["column_1"][-1]])
host.set_ylim(data["column_2"].min(), data["column_2"].max())
par1.set_ylim(data["column_3"].min(), data["column_3"].max())
par2.set_ylim(data["column_4"].min(), data["column_4"].max())

解决方案


我可以通过以下方式重现警告:

In [313]: x = np.zeros((4,2))
In [315]: x[:,1]
Out[315]: array([0., 0., 0., 0.])

通过替换:a,slice(None)我们可以将此索引编写为:

In [316]: x[[slice(None),1]]
/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[316]: array([0., 0., 0., 0.])

它应该是一个元组,而不是一个列表:

In [317]: x[(slice(None),1)]
Out[317]: array([0., 0., 0., 0.])
In [318]: x[tuple([slice(None),1])]
Out[318]: array([0., 0., 0., 0.])

警告告诉我们列表格式过去没问题,但将来会产生错误。

我没有看到任何代码在列表索引中执行此类切片。

datafrom genfromtxt是一个结构化数组,因此按字段名称索引是正常的:data["column_1"]所以警告很可能是在plot代码中生成的但我们对于哪里没有任何线索。警告不会给出任何类型的错误堆栈跟踪,这样做吗?

因此,如果没有类似的示例数组data或csv文件Example.csv,我们无法重现警告,并进一步挖掘。


首先,我会print在每个代码行之间添加一些内容。目标是确定哪个matplotlib呼叫产生警告。

例如,如果它是生产的

host.set_xlim([data["column_1"][0], data["column_1"][-1]])

我可能会尝试将该调用更改为

host.set_xlim((data["column_1"][0], data["column_1"][-1]))

要么

host.set_xlim(data["column_1"][0], data["column_1"][-1])

这有点疯狂猜测......

编辑

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

这个最新的SO,帮助我们识别scipy.stats包中的问题功能它构造了一个切片列表,并使用它而无需进一步转换为元组。



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/76153/e72817cfefd7834f10ef/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

13 0
收藏该文
已收藏

评论内容:(最多支持255个字符)