|
|
| Image after applying convex hull transform | Image Corrected by dividing by convex hull |
>>> help(cumsum)
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 `dtype` is not specified, it defaults
to the dtype of `a`, unless `a` has an integer dtype with a
precision less than that of the default platform integer. In
that case, the default platform integer is used.
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 `doc.ufuncs`
(Section "Output arguments") for more details.
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.
See Also
--------
sum : Sum array elements.
trapz : Integration of array values using the composite trapezoidal rule.
Notes
-----
Arithmetic is modular when using integer types, and no error is
raised on overflow.
Examples
--------
>>> a = np.array([[1,2,3], [4,5,6]])
>>> a
array([[1, 2, 3],
[4, 5, 6]])
>>> np.cumsum(a)
array([ 1, 3, 6, 10, 15, 21])
>>> np.cumsum(a, dtype=float) # specifies type of output value(s)
array([ 1., 3., 6., 10., 15., 21.])
>>> np.cumsum(a,axis=0) # sum over rows for each of the 3 columns
array([[1, 2, 3],
[5, 7, 9]])
>>> np.cumsum(a,axis=1) # sum over columns for each of the 2 rows
array([[ 1, 3, 6],
[ 4, 9, 15]])
You can use cumsum in places where you'd otherwise use loops
and you can use it to initialize a large array from a small
one. For instance, you have an array of counts of how many times
something occurred for each of several subjects:
>>> import numpy as np
>>> # 0-9 students per teacher for 20 teachers
>>> student_count = np.random.randint(0, 10, 20)
>>> student_count
array([3, 5, 5, 9, 9, 0, 4, 0, 3, 7, 0, 1, 3, 4, 5, 6, 5, 2, 3, 4])
You want an array that, for each student, tells you which teacher
they have:
>>> # The index of the teacher's first student
>>> first_student = np.cumsum(student_count) - student_count
>>> # initialize the teacher array to the total # of students
>>> teacher = np.zeros(student_cumsum[-1], int)
>>> # mark the first student for each teacher in the array
>>> # Teachers with zero students shouldn't have their first
>>> # student marked
>>> mask = student_count > 0
>>> teacher_idx = np.arange(20)[mask]
>>> teacher[first_student[teacher_idx[1:]]] = teacher_idx[1:] - teacher_idx[:-1]
>>> # the cumulative sum only increments for the first student
>>> teacher = np.cumsum(teacher)
>>> teacher
array([ 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6,
6, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 11, 12, 12, 12, 13, 13,
13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16,
16, 17, 17, 18, 18, 18, 19, 19, 19, 19])
>>> student_idx = np.ones(teacher.shape, int)
>>> student_idx[0] = 0
>>> student_idx[first_student[teacher_idx[1:]]] = 1 - student_count[teacher_idx[:-1]]
>>> student_idx = np.cumsum(student_idx)
>>> student_idx
array([0, 1, 2, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0,
1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 0, 1, 2, 0, 1, 2, 3, 4, 5, 6, 0,
0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4,
0, 1, 0, 1, 2, 0, 1, 2, 3])