[英]python numpy array slicing
I have an 2d array, A that is 6x6. 我有一个二维数组,A为6x6。 I would like to take the first 2 values (index 0,0 and 0,1) and take the average of the two and insert the average into a new array that is half the column size of A (6x3) at index 0,0.
我想取前两个值(索引0,0和0,1)并取两个值的平均值,然后将平均值插入到新数组中,该数组是索引0,0处A(6x3)的列大小的一半。 Then i would get the next two indexes at A, take average and put into the new array at 0,1.
然后我将在A处获得下两个索引,取平均值并以0,1放入新数组。
The only way I know how to do this is using a double for loop, but for performance purposes (I will be using arrays as big as 3000x3000) I know there is a better solution out there! 我知道如何做到这一点的唯一方法是使用double for循环,但是出于性能目的(我将使用最大3000x3000的数组),我知道那里有更好的解决方案! Thanks!
谢谢!
A very useful feature of numpy arrays is that they can be reshaped and viewed in many different ways, and by doing so, you can make certain operations very easy. numpy数组的一个非常有用的功能是,可以用许多不同的方式对它们进行整形和查看,并且这样做可以使某些操作变得非常容易。
Since you want to pair every two items, it makes sense to reshape the 6x6 array into a 18x2 array: 由于您希望每两个项目都配对,因此将6x6数组重塑为18x2数组是有意义的:
import numpy as np
arr=np.arange(36).reshape(6,6)
print(arr)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]
# [24 25 26 27 28 29]
# [30 31 32 33 34 35]]
arr2=arr.reshape(-1,2)
print(arr2)
# [[ 0 1]
# [ 2 3]
# [ 4 5]
# [ 6 7]
# [ 8 9]
# [10 11]
# [12 13]
# [14 15]
# [16 17]
# [18 19]
# [20 21]
# [22 23]
# [24 25]
# [26 27]
# [28 29]
# [30 31]
# [32 33]
# [34 35]]
Now taking the average is easy: 现在取平均值很容易:
means=arr2.mean(axis=1)
print(means)
# [ 0.5 2.5 4.5 6.5 8.5 10.5 12.5 14.5 16.5 18.5 20.5 22.5
# 24.5 26.5 28.5 30.5 32.5 34.5]
And finally, we just reshape the array to be 6x3: 最后,我们将数组重塑为6x3:
means=means.reshape(6,-1)
print(means)
# [[ 0.5 2.5 4.5]
# [ 6.5 8.5 10.5]
# [ 12.5 14.5 16.5]
# [ 18.5 20.5 22.5]
# [ 24.5 26.5 28.5]
# [ 30.5 32.5 34.5]]
or, as a 1-liner: 或者,作为1班轮:
means=arr.reshape(-1,2).mean(axis=1).reshape(6,-1)
PS: reshaping is a very quick operation, since it is returning a view, not a copy of the original array. PS:重塑是非常快速的操作,因为它返回的是视图,而不是原始数组的副本。 All that is changed is the dimensions and the strides.
所改变的只是尺寸和步幅。 All that remains is one call to the
mean
method. 剩下的只是对
mean
方法的一次调用。 Thus, this solution should be about a quick as possible using numpy. 因此,使用numpy时,此解决方案应尽可能快。
I don't think there is a better solution, unless you have some extra information about what's in those arrays. 我认为没有更好的解决方案,除非您有一些有关这些阵列中内容的额外信息。 If they're just random numbers, you have to do (n^2)/2 calculations, and your algorithm is reflecting that, running in O((n^2)/2).
如果它们只是随机数,则必须进行(n ^ 2)/ 2个计算,并且您的算法正在以O((n ^ 2)/ 2)运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.