gpt4 book ai didi

python - 循环错误的 Numpy 排序数组,但原始工作正常

转载 作者:行者123 更新时间:2023-11-28 19:00:30 24 4
gpt4 key购买 nike

在我对这个 numpy 数组进行排序并删除所有重复 (y) 值和重复 (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),但您可以随时将其改回for 循环与 done = done.T

以下是您的原始代码的一些注释以供将来引用:

  • reshape 中的 order 标志对于您的代码试图做的事情来说是完全多余的,只会让它更困惑/可能有更多错误。没有它,您可以进行所有您想进行的 reshape 。

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

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

关于.T的注意事项

.T 进行数组的转置。例如,如果您开始于

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

那么转置就是:

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

关于python - 循环错误的 Numpy 排序数组,但原始工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53244018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com