numpy.mean在不同的行大小上

numpy.mean在不同的行大小上,第1张

numpy.mean在不同的行大小

这是一种方法-

# 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]


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5675086.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存