问题描述
我有一个modified
元素的NumPy数组,表示为扁平数组(X * Y
)。
给出以下值:
arr = np.array(x * y)
我需要按以下顺序访问数组的元素:
X = 832
Y = 961
在数学上,我不确定如何在循环中的每次迭代中实现arr[0:832:2]
arr[1:832:2]
arr[832:1664:2]
arr[833:1664:2]
...
arr[((Y-1) * X):(X * Y):2]
和start
。
解决方法
This should do the trick
Y = 961
X = 832
all_ = np.random.rand(832*961)
# Iterating over the values of y
for i in range(1,Y):
# getting the indicies from the array we need
# i - 1 = Start
# X*i = END
# 2 is the step
indicies = list(range(i-1,X*i,2))
# np.take slice values from the array or get values corresponding to the list of indicies we prepared above
required_array = np.take(indices=indices)
,
对对此解决方案感兴趣的任何人(每次迭代,每次迭代都不增加移位):
for i in range(Y):
shift = X * (i // 2)
begin = (i % 2) + shift
end = X + shift
print(f'{begin}:{end}:2')
,
比方说,您有一个形状为try:
all_info = response.xpath("//div[@class = 'title']/div[@class='tooltipContainer']/div/text()").extract_first()
except Exception as ex:
yield {"Ex`enter code here`": ex}
yield {"All_Information":all_info}
的数组,您希望以(x * y,)
的块进行处理。您可以简单地将数组整形为x
并处理行:
(y,x)
现在重塑,并批量处理。在下面的示例中,我取每一行的平均值。您可以通过以下方式将所需的任何功能应用于整个数组:
>>> x = 832
>>> y = 961
>>> arr = np.arange(x * y)
整形操作不会更改数组中的数据。它指向的缓冲区与原始缓冲区相同。您可以通过在数组上调用>>> arr = arr.reshape(y,x)
>>> np.mean(arr[:,::2],axis=1)
>>> np.mean(arr[:,1::2],axis=1)
来反转整形。