在Python中求和numpy数组的元素

时间:2022-01-02 21:23:58

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?

这不会给我想要的输出。有什么建议吗?

2 个解决方案

#1


2  

You can do

你可以做

clf_prob.sum(axis=1)

This will take the sum over a certain axis, in this case rows.

这将取特定轴上的总和,在这种情况下为行。

#2


0  

numpy.sum() over your intended axis (1) should do the job in your case

numpy.sum()超过你想要的轴(1)应该在你的情况下完成工作

#1


2  

You can do

你可以做

clf_prob.sum(axis=1)

This will take the sum over a certain axis, in this case rows.

这将取特定轴上的总和,在这种情况下为行。

#2


0  

numpy.sum() over your intended axis (1) should do the job in your case

numpy.sum()超过你想要的轴(1)应该在你的情况下完成工作