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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52110869/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:02:19  来源:igfitidea点击:

FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]` instead of `arr[seq]`

pythonarrayspython-3.xnumpymatplotlib

提问by yajant b

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.

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

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

回答by hpaulj

I can reproduce the warning with:

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

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

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

通过用: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.])

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.

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

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

datagenfromtxt是一个结构阵列,使由索引字段名称为正常:data["column_1"]。所以很可能警告是在plot代码中生成的。但我们不知道在哪里。警告没有给出任何类型的错误堆栈跟踪,是吗?

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

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



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

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

If for example it is produced in

例如,如果它是在

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

or

或者

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

That's a bit of wild guess...

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

edit

编辑

FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]`

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

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

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

回答by RiseofRice

Upadating Scipy fixed this problem in my case. Cause the Scipy.stats class was deprecated.

在我的情况下,更新 Scipy 解决了这个问题。因为 Scipy.stats 类已被弃用。

回答by Thom Ives

I would have tested this before posting (well, I did test it for areas where I was having the same problem), but I suspect that this will help you. Using your first line where you call a plot above, use tuple type casting as I've shown and do the same to your other lines calling plot.

我会在发布之前对此进行测试(好吧,我确实针对遇到相同问题的区域进行了测试),但我怀疑这会对您有所帮助。使用您在上面调用 plot 的第一行,使用我展示的元组类型转换,并对调用 plot 的其他行执行相同的操作。

p1, = host.plot(tuple(data["column_1"]), 
                tuple(data["column_2"]), 
                "b-", label="column_2")

When I studied numpy methods of indexing, the warning made a little more sense. However, I don't really understand why things need to go this way.

当我研究 numpy 索引方法时,警告更有意义。但是,我真的不明白为什么事情需要这样。