Numpy MaskedArray.cumsum() function | Python (original) (raw)
Last Updated : 18 Oct, 2019
numpy.MaskedArray.cumsum()
Return the cumulative sum of the masked array elements over the given axis.Masked values are set to 0 internally during the computation. However, their position is saved, and the result will be masked at the same locations.
Syntax :
numpy.ma.cumsum(axis=None, dtype=None, out=None)
Parameters:
**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, as well as of the accumulator in which the elements are multiplied.
out : [ndarray, optional] A location into which the result is stored.
-> If provided, it must have a shape that the inputs broadcast to.
-> If not provided or None, a freshly-allocated array is returned.Return : [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.
Code #1 :
import
numpy as geek
import
numpy.ma as ma
in_arr
=
geek.array([[
1
,
2
], [
3
,
-
1
], [
5
,
-
3
]])
print
(
"Input array : "
, in_arr)
mask_arr
=
ma.masked_array(in_arr, mask
=
[[
1
,
0
], [
1
,
0
], [
0
,
0
]])
print
(
"Masked array : "
, mask_arr)
out_arr
=
mask_arr.cumsum()
print
(
"cumulative sum of masked array along default axis : "
, out_arr)
Output:
Input array : [[ 1 2] [ 3 -1] [ 5 -3]] Masked array : [[-- 2] [-- -1] [5 -3]] cumulative sum of masked array along default axis : [-- 2 -- 1 6 3]
Code #2 :
import
numpy as geek
import
numpy.ma as ma
in_arr
=
geek.array([[
1
,
0
,
3
], [
4
,
1
,
6
]])
print
(
"Input array : "
, in_arr)
mask_arr
=
ma.masked_array(in_arr, mask
=
[[
0
,
0
,
0
], [
0
,
0
,
1
]])
print
(
"Masked array : "
, mask_arr)
out_arr1
=
mask_arr.cumsum(axis
=
0
)
print
(
"cumulative sum of masked array along 0 axis : "
, out_arr1)
out_arr2
=
mask_arr.cumsum(axis
=
1
)
print
(
"cumulative sum of masked array along 1 axis : "
, out_arr2)
Output:
Input array : [[1 0 3] [4 1 6]] Masked array : [[1 0 3] [4 1 --]] cumulative sum of masked array along 0 axis : [[1 0 3] [5 1 --]] cumulative sum of masked array along 1 axis : [[1 1 4]
Similar Reads
- Numpy MaskedArray.cumprod() function | Python numpy.MaskedArray.cumprod() Return the cumulative product of the masked array elements over the given axis.Masked values are set to 1 internally during the computation. However, their position is saved, and the result will be masked at the same locations. Syntax : numpy.ma.cumprod(axis=None, dtype=N 3 min read
- Numpy MaskedArray.anom() function | Python In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays.Masked arrays are arra 2 min read
- Numpy MaskedArray.compressed() function - Python numpy.MaskedArray.compressed() function return all the non-masked data as a 1-D array. Syntax : numpy.MaskedArray.compressed(self) Return : [ndarray] A new ndarray holding the non-masked data is returned. Code #1 : # Python program explaining # numpy.MaskedArray.compressed() function # importing num 1 min read
- numpy.ma.MaskedArray.count() function - Python numpy.ma.MaskedArray.count() function count the non-masked elements of the array along the given axis. Syntax : numpy.ma.MaskedArray.count(self, axis=None, keepdims = no value) Parameters : axis : [None or int or tuple of ints, optional] Axis along which the count is performed. The default axis is N 2 min read
- Numpy MaskedArray.all() function | Python In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays. Masked arrays are arr 3 min read
- Numpy MaskedArray.dot() function | Python numpy.MaskedArray.dot() function is used to calculate the dot product of two mask arrays. Syntax : numpy.ma.dot(arr1, arr2, strict=False) Parameters: arr1, arr2:[ ndarray] Inputs arrays. strict : [bool, optional] Whether masked data are propagated (True) or set to 0 (False) for the computation. Defa 3 min read
- Numpy MaskedArray.argmin() function | Python In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays.Masked arrays are arra 3 min read
- Numpy MaskedArray.argmax() function | Python In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays.Masked arrays are arra 3 min read
- Numpy MaskedArray.astype() function | Python In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays.Masked arrays are arra 3 min read
- Numpy MaskedArray.any() function | Python In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays.Masked arrays are arra 3 min read