[英]How to calculate the sums of squares along one dimension of on ndarray?
One miltidimensional matrix with shape (2, 50, 25, 3): 一个具有形状的二维矩阵(2,50,25,3):
xx = np.random.randn(2, 50, 25, 3)
I want to calculate the sum of squares of the last dimension. 我想计算最后一个维度的平方和。 The result should be a matrix with a shape (2, 50, 25, 1).
结果应该是具有形状(2,50,25,1)的矩阵。
[np.sum(x) for x in np.square(features_displacement[0][0][:])[:]]
This code can successfully calculate the one dimension, output a list with shape (25,1), but how can I calculate all the dimensions as described above? 此代码可以成功计算一维,输出一个形状为(25,1)的列表,但如何计算如上所述的所有维度?
You can apply the numpy functions along the axis you want, for example: 您可以沿所需的轴应用numpy函数,例如:
np.sum(np.square(xx), axis=3)
Will produce an array of shape (2, 50, 25)
. 将产生一个形状的阵列
(2, 50, 25)
。 Not exactly sure this is what you want, if not please be more specific :-) 不完全确定这是你想要的,如果没有请更具体:-)
用这个:
sum_of_squares = np.sum(np.square(features_displacement), axis=-1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.