python 函数解析 (numpy 篇)
每碰到一个不懂的就查查源码,看看参数怎么写的。注意多积累。
.cumsum
numpy.
cumsum
(a, axis=None, dtype=None, out=None)
Return the cumulative sum of the elements along a given axis.
直译翻译过来就是返回前项列的累积和。但是这个累积是对原列的基础上。不是叠加的结果。
Parameters: | a : array_like Input array. axis : int, optional Axis along which the cumulative sum is computed. The default (None) is to compute the cumsum over the flattened array. dtype : dtype, optional Type of the returned array and of the accumulator in which the elements are summed. If out : ndarray, optional Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output but the type will be cast if necessary. See |
---|---|
Returns: | cumsum_along_axis : ndarray. A new array holding the result is returned unless out is specified, in which case a reference to out is returned. The result has the same size as a, and the same shape as a if axis is not None or a is a 1-d array. |
例如:
a = np.array([[1,2,3], [4,5,6]])
# array([[1, 2, 3],[4, 5, 6]])
np.cumsum(a)
# array([ 1, 3, 6, 10, 15, 21])
1
1+2
1+2+3..
不同于斐波那契数列的相加规则。