[英]Summing up n number matrix in python
I have a matrix of 40*2000 a vector of of dimension 1500. I used numpy.outer to compute outer product of vector with each column of matrix as: 我有一个40 * 2000尺寸为1500的向量的矩阵。我使用numpy.outer来计算向量与矩阵的每一列的外积为:
np.outer(vector, matrix)
It showed memory error so I splitted matrix as matrix[:,:10] and computed separatly. 它显示了内存错误,因此我将矩阵拆分为matrix [:,:10]并分别进行了计算。 It resulted into a matrix
M
of 1500*(40*10). 结果得到1500×(40×10)的矩阵
M
I need sum each of the matrix here i'e first 40 columns with another next 40 column and so on..... If i used loop, it is going to be slow. 我需要对每个矩阵求和,这里我是前40列,再下一个40列,依此类推.....如果我使用循环,它将变得很慢。
M[:,:40]+ M[:,40:80] + .....
Could someone help me to do this operation efficiently? 有人可以帮助我有效地执行此操作吗?
A bit of reshaping is all you need to be able to sum
over the axis: 您需要进行一些整形,才能在轴上
sum
:
import numpy
M = numpy.arange(100).reshape(5, 20)
M[:, :4]
#>>> array([[ 0, 1, 2, 3],
#>>> [20, 21, 22, 23],
#>>> [40, 41, 42, 43],
#>>> [60, 61, 62, 63],
#>>> [80, 81, 82, 83]])
M[:, 4:8]
#>>> array([[ 4, 5, 6, 7],
#>>> [24, 25, 26, 27],
#>>> [44, 45, 46, 47],
#>>> [64, 65, 66, 67],
#>>> [84, 85, 86, 87]])
...
M[:, 16:20]
#>>> array([[16, 17, 18, 19],
#>>> [36, 37, 38, 39],
#>>> [56, 57, 58, 59],
#>>> [76, 77, 78, 79],
#>>> [96, 97, 98, 99]])
M.reshape(M.shape[0], -1, 4).sum(axis=1)
#>>> array([[ 40, 45, 50, 55],
#>>> [140, 145, 150, 155],
#>>> [240, 245, 250, 255],
#>>> [340, 345, 350, 355],
#>>> [440, 445, 450, 455]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.