FutureWarning:不推荐使用非元组序列进行多维索引,使用 `arr[tuple(seq)]` 而不是 `arr[seq]` [英] FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]` instead of `arr[seq]`

查看:22
本文介绍了FutureWarning:不推荐使用非元组序列进行多维索引,使用 `arr[tuple(seq)]` 而不是 `arr[seq]`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like not to use the non-tuple sequence for multidimensional indexing so that the script will support future release of Python when this changes.

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

Below is the code that i am using for plotting the graph:

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())

推荐答案

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

I can reproduce the warning with:

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

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

By replacing the : with a slice(None) we can write this indexing as:

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.])

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

It really should be a tuple, rather than a list:

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.])

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

The warning tells us that the list format used to be ok, but will in the future produce an error.

我没有看到您的代码在列表索引中执行此类切片.

I don't see anything your code that does this sort of slice in a list indexing.

data from genfromtxt 是结构化数组,所以按字段名索引是正常的:data["column_1"].所以很可能警告是在 plot 代码中生成的.但我们不知道在哪里.警告没有提供任何类型的错误堆栈跟踪,是吗?

data from genfromtxt is a structured array, so indexing by field name is normal: data["column_1"]. So it's likely that the warning is generated within the plot code. But we don't have any clue as to where. The warning doesn't give any sort of error stack trace, do it?

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

So without a sample array like data, or a csv file like Example.csv, we can't reproduce the warning, and dig further.

首先,我会在您的每个代码行之间添加某种print.目标是确定哪个 matplotlib 调用产生了警告.

For a start I'd put some sort of print between each of your code lines. The goal is to pin down which matplotlib call is producing the warning.

例如,如果它是在

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

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

I might try changing that call to

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

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

这有点疯狂的猜测...

That's a bit of wild guess...

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

这个最新的 SO,帮助我们识别 scipy.stats 包中的问题函数.它构造了一个切片列表,并在不进一步转换为元组的情况下使用它.

This latest SO, helps us identify a problem function in the scipy.stats package. It constructs a list of slices, and uses it without further conversion to tuple.

这篇关于FutureWarning:不推荐使用非元组序列进行多维索引,使用 `arr[tuple(seq)]` 而不是 `arr[seq]`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆