[英]Summing elements of numpy array in Python
I have an numpy array 我有一个numpy数组
>>> clf_prob.dtype()
array([[ 0.05811791, 0.06526527, 0.06024136, ..., 0.06972481],
[ 0.06093686, 0.06357167, 0.06462331, ..., 0.06999094],
[ 0.08188396, 0.08504034, 0.0820972 , ..., 0.08487802],
[ 0.05197106, 0.0786195 , 0.15669477, ..., 0.0893244]])
I'm trying to add elements of these arrays such that my output would be: 我试图添加这些数组的元素,这样我的输出将是:
[[0.05811791 + 0.06526527 + 0.06024136 +...+ 0.06972481],
[0.06093686 + 0.06357167 + 0.06462331 +...+0.06999094],
[0.08188396 + 0.08504034 + 0.0820972 + ...+ 0.08487802],
[0.05197106 + 0.0786195 + 0.15669477+ ...+ 0.0893244]]
I tried doing 我试着做
sum(map(sum, clf_prob))
This doesn't gives me desired output. 这没有给我想要的输出。 Any suggestion?
有什么建议吗?
You can do 你可以做
clf_prob.sum(axis=1)
This will take the sum over a certain axis, in this case rows. 这将取某个轴上的总和,在本例中为行。
预期轴(1)上的numpy.sum()应该适合您的情况
Another probability is to use ufunc of numpy: 另一种可能性是使用numpy的ufunc:
np.sum.reduce(clf_prob)
which will give the sum over the first axis. 这将给出第一个轴上的总和。 You can also use the
axis
parameter to sum over another axis. 您还可以使用
axis
参数对另一个轴求和。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.