这是一种方法-
# Store length of each subarraylens = np.array(map(len,a))# Generate IDs based on the lengthsIDs = np.repeat(np.arange(len(lens)),lens)# Use IDs to do bin-based summing of a elems and divide by subarray lengthsout = np.bincount(IDs,np.concatenate(a))/lens
样品运行-
In [34]: a # Input arrayOut[34]: array([[1, 2], [3, 4, 5]], dtype=object)In [35]: lens = np.array(map(len,a)) ...: IDs = np.repeat(np.arange(len(lens)),lens) ...: out = np.bincount(IDs,np.concatenate(a))/lens ...:In [36]: out # Average outputOut[36]: array([ 1.5, 4. ])
使用列表理解的更简单的替代方法-
In [38]: [np.mean(i) for i in a]Out[38]: [1.5, 4.0]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)