循环错误的Numpy排序数组,但原始数组运行良好



在我对这个numpy数组进行排序并删除所有重复的(y(值和对应的(x(值后,我使用for循环在剩余坐标处绘制矩形。然而,我得到了错误:ValueError:太多的值无法解压缩(应为2(,但它与原始形状相同,只是删除了重复的值。

from graphics import *
import numpy as np
def main():
win = GraphWin("A Window", 500, 500)
# starting array
startArray = np.array([[2, 1, 2, 3, 4, 7],
[5, 4, 8, 3, 7, 8]])
# the following reshapes the from all x's in one row and y's in second row
# to x,y rows pairing the x with corresponding y value.
# then it searches for duplicate (y) values and removes both the duplicate (y) and
# its corresponding (x) value by removing the row.
# then the unique [x,y]'s array is reshaped back to a [[x,....],[y,....]] array to be used to draw rectangles.
d = startArray.reshape((-1), order='F')
# reshape to [x,y] matching the proper x&y's together
e = d.reshape((-1, 2), order='C')
# searching for duplicate (y) values and removing that row so the corresponding (x) is removed too.
f = e[np.unique(e[:, 1], return_index=True)[1]]
# converting unique array back to original shape
almostdone = f.reshape((-1), order='C')
# final reshape to return to original starting shape but is only unique values
done = almostdone.reshape((2, -1), order='F')
# print all the shapes and elements
print("this is d reshape of original/start array:", d)
print("this is e reshape of d:n", e)
print("this is f unique of e:n", f)
print("this is almost done:n", almostdone)
print("this is done:n", done)
print("this is original array:n",startArray)
# loop to draw a rectangle with each x,y value being pulled from the x and y rows
# says too many values to unpack?
for x,y in np.nditer(done,flags = ['external_loop'], order = 'F'):
print("this is x,y:", x,y)
print("this is y:", y)
rect = Rectangle(Point(x,y),Point(x+4,y+4))
rect.draw(win)
win.getMouse()
win.close()
main()

这是输出:

line 42, in main
for x,y in np.nditer(done,flags = ['external_loop'], order = 'F'):
ValueError: too many values to unpack (expected 2)
this is d reshape of original/start array: [2 5 1 4 2 8 3 3 4 7 7 8]
this is e reshape of d:
[[2 5]
[1 4]
[2 8]
[3 3]
[4 7]
[7 8]]
this is f unique of e:
[[3 3]
[1 4]
[2 5]
[4 7]
[2 8]]
this is almost done:
[3 3 1 4 2 5 4 7 2 8]
this is done:
[[3 1 2 4 2]
[3 4 5 7 8]]
this is original array:
[[2 1 2 3 4 7]
[5 4 8 3 7 8]]

为什么for循环对原始数组有效,而对排序后的数组无效?或者我可以使用哪个循环来只使用(f(,因为它是排序的,但形状是(-1,2(?

我还尝试了一个不同的循环:

for x,y in done[np.nditer(done,flags = ['external_loop'], order = 'F')]:

这似乎修复了太多的值错误,但我得到:

IndexError: index 3 is out of bounds for axis 0 with size 2

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.
for x,y in done[np.nditer(done,flags = ['external_loop'], order = 'F')]:

我在stackexchange上查找了它来修复,但无论我如何执行语法,都会不断出现错误。

任何帮助都将不胜感激!

我没有graphics包(它可能是特定于windows的东西?(,但我知道你让这个过程太复杂了。这里有一个更简单的版本,可以生成相同的done阵列:

from graphics import *
import numpy as np
# starting array
startArray = np.array([[2, 1, 2, 3, 4, 7],
[5, 4, 8, 3, 7, 8]])
# searching for duplicate (y) values and removing that row so the corresponding (x) is removed too.
done = startArray.T[np.unique(startArray[1,:], return_index=True)[1]]
for x,y in done:
print("this is x,y:", x, y)
print("this is y:", y)
rect = Rectangle(Point(x,y),Point(x+4,y+4))
rect.draw(win)

需要注意的是,在上面的版本中,done.shape==(5, 2)而不是(2, 5),但您总是可以在使用done = done.Tfor循环之后将其更改回。

以下是关于您的原始代码的一些注释,以供将来参考:

  • reshape中的order标志对于您的代码试图做的事情来说是完全多余的,只会使它更加混乱/可能更加bug。你可以做所有你想做的重塑没有它。

  • nditer的用例是一次迭代一个(或多个(数组的各个元素。它通常不能用于迭代二维数组的行或列。如果你试图以这种方式使用它,你可能会得到错误的结果,这些结果高度依赖于内存中数组的布局(正如你所看到的(。

  • 要对2D数组的行或列进行迭代,只需使用简单的迭代。如果您只是在一个数组(例如for row in arr:(上进行迭代,则会得到每一行,一次一行。如果你想要列,你可以先转置数组(就像我在上面的.T代码中所做的那样(。

关于.T的注意事项

CCD_ 13取数组的转置。例如,如果您从开始

arr = np.array([[0, 1, 2, 3],
[4, 5, 6, 7]])

则转置为:

arr.T==np.array([[0, 4],
[1, 5],
[2, 6],
[3, 7]])

最新更新