Numpy MaskedArray.average() function | Python (original) (raw)
Last Updated : 13 Oct, 2019
numpy.MaskedArray.average()
function is used to return the weighted average of array over the given axis.
Syntax :
numpy.ma.average(arr, axis=None, weights=None, returned=False)
Parameters:
**arr :**[ array_like] Input masked array whose data to be averaged. Masked entries are not taken into account in the computation.
**axis :**[ int, optional] Axis along which to average arr. If None, averaging is done over the flattened array.
weights : [array_like, optional] The importance that each element has in the computation of the average. If weights=None, then all data in arr are assumed to have a weight equal to one. If weights is complex, the imaginary parts are ignored.
**returned :**[ bool, optional] It indicates whether a tuple (result, sum of weights) should be returned as output (True), or just the result (False). Default is False.Return : [ scalar or MaskedArray] The average along the specified axis. When returned is True, return a tuple with the average as the first element and the sum of the weights as the second element.
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
=
ma.average(mask_arr)
print
(
"normal average of masked array : "
, out_arr)
Output:
Input array : [[ 1 2] [ 3 -1] [ 5 -3]] Masked array : [[-- 2] [-- -1] [5 -3]] normal average of masked array : 0.75
Code #2 :
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
=
ma.average(mask_arr, weights
=
[[
0
,
1
], [
0
,
2
], [
3
,
1
]])
print
(
"weighted average of masked array : "
, out_arr)
Output:
Input array : [[ 1 2] [ 3 -1] [ 5 -3]] Masked array : [[-- 2] [-- -1] [5 -3]] weighted average of masked array : 1.7142857142857142
Similar Reads
- 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.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.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.argsort() 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
- 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.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.allequal() 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.flatten() function | Python numpy.MaskedArray.flatten() function is used to return a copy of the input masked array collapsed into one dimension. Syntax : numpy.ma.flatten(order='C') Parameters: order : [‘C’, ‘F’, ‘A’, ‘K’, optional] Whether to flatten in C (row-major), Fortran (column-major) order, or preserve the C/Fortran o 2 min read
- Numpy MaskedArray.atleast_1d() function | Python numpy.MaskedArray.atleast_1d() function is used to convert inputs to masked arrays with at least one dimension.Scalar inputs are converted to 1-dimensional arrays, whilst higher-dimensional inputs are preserved. Syntax : numpy.ma.atleast_1d(*arys) Parameters: arys:[ array_like] One or more input arr 2 min read