[英]Vectorize finding center of sets of points in multidimensional array in Numpy
I've got a multidimensional array that has 1 million sets of 3 points, each point being a coordinate specified by x and y.我有一个多维数组,它有 100 万组 3 个点,每个点都是由 x 和 y 指定的坐标。 Calling this array pointVec, what I mean is
调用这个数组pointVec,我的意思是
np.shape(pointVec) = (1000000,3,2)
I want to find the center of each of the set of 3 points.我想找到每组 3 个点的中心。 One obvious way is to iterate through all 1 million sets, finding the center of each set at each iteration.
一种明显的方法是遍历所有 100 万个集合,在每次迭代中找到每个集合的中心。 However, I have heard that vectorization is a strong-suit of Numpy's, so I'm trying to adapt it to this problem.
但是,我听说矢量化是 Numpy 的强项,所以我正在尝试使其适应这个问题。 Since this problem fits so intuitively with iteration, I don't have a grasp of how one might do it with vectorization, or if using vectorization would even be useful.
由于这个问题非常适合迭代,因此我不了解如何使用矢量化来解决这个问题,或者使用矢量化是否有用。
It depends how you define a center of a three-point.这取决于您如何定义三点的中心。 However, if it is average coordinates, like @Quang mentioned in the comments, you can take the average along a specific axis in numpy:
但是,如果是平均坐标,如评论中提到的@Quang,您可以在numpy中沿特定轴取平均值:
pointVec.mean(1)
This will take the mean along axis=1 (which is second axis with 3 points) and return a (1000000,2) shaped array.这将取沿轴 = 1(它是具有 3 个点的第二个轴)的平均值并返回一个 (1000000,2) 形状的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.