Python's sum vs. NumPy's numpy.sum

前端 未结 6 981
时光说笑
时光说笑 2020-11-28 08:03

What are the differences in performance and behavior between using Python\'s native sum function and NumPy\'s numpy.sum? sum works on

6条回答
  •  悲&欢浪女
    2020-11-28 08:35

    Note that Python sum on multidimensional numpy arrays will only perform a sum along the first axis:

    sum(np.array([[[2,3,4],[4,5,6]],[[7,8,9],[10,11,12]]]))
    Out[47]: 
    array([[ 9, 11, 13],
           [14, 16, 18]])
    
    np.sum(np.array([[[2,3,4],[4,5,6]],[[7,8,9],[10,11,12]]]), axis=0)
    Out[48]: 
    array([[ 9, 11, 13],
           [14, 16, 18]])
    
    np.sum(np.array([[[2,3,4],[4,5,6]],[[7,8,9],[10,11,12]]]))
    Out[49]: 81
    

提交回复
热议问题