Numpy之sum函数用法

numpy.sum用法如下：

numpy.sum(a, axis=None, dtype=None, out=None, keepdims=False)

参数如下：

aarray_like 类型

axis ：可选择Noneint类型 或 整型的tuple类型

dtypedtype 类型，可选的

outndarray 类型，可选的

Array into which the output is placed. By default, a new array is created. If out is given, it must be of the appropriate shape (the shape of a with axis removed, i.e., numpy.delete(a.shape, axis)). Its type is preserved. See doc.ufuncs (Section “Output arguments”) for more details.

keepdimsbool 类型，可选的

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.

函数返回：

sum_along_axisndarray

>>> np.sum([])
0.0

举例如下：

>>> np.sum([0.5, 1.5])
2.0
>>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)
1
>>> np.sum([[0, 1], [0, 5]])
6
>>> np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])
>>> np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])

>>> np.ones(128, dtype=np.int8).sum(dtype=np.int8)
-128