Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

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

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())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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 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?

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


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.

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...