Numpy MaskedArray.astype() function | Python (original) (raw)
Last Updated : 16 Jun, 2021
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 arrays that may have missing or invalid entries.
numpy.MaskedArray.astype() function returns a copy of the MaskedArray cast to given newtype.
Syntax : numpy.MaskedArray.astype(newtype)
Parameters:
newtype : Type in which we want to convert the masked array.
Return : [MaskedArray] A copy of self cast to input newtype. The returned record shape matches self.shape.
Code #1 :
Python3
import
numpy as geek
import
numpy.ma as ma
in_arr
=
geek.array([
1
,
2
,
3
,
-
1
,
5
])
print
(
"Input array : "
, in_arr)
mask_arr
=
ma.masked_array(in_arr, mask
=
[
0
,
0
,
1
,
0
,
0
])
print
(
"Masked array : "
, mask_arr)
print
(mask_arr.dtype)
out_arr
=
mask_arr.astype(
'float64'
)
print
(
"Output typecasted array : "
, out_arr)
print
(out_arr.dtype)
Output:
Input array : [ 1 2 3 -1 5] Masked array : [1 2 -- -1 5] int32 Output typecasted array : [1.0 2.0 -- -1.0 5.0] float64
Code #2 :
Python3
import
numpy as geek
import
numpy.ma as ma
in_arr
=
geek.array([
10.1
,
20.2
,
30.3
,
40.4
,
50.5
], dtype
=
'float64'
)
print
(
"Input array : "
, in_arr)
mask_arr
=
ma.masked_array(in_arr, mask
=
[
1
,
0
,
1
,
0
,
0
])
print
(
"Masked array : "
, mask_arr)
print
(mask_arr.dtype)
out_arr
=
mask_arr.astype(
'int32'
)
print
(
"Output typecasted array : "
, out_arr)
print
(out_arr.dtype)
Output:
Input array : [10.1 20.2 30.3 40.4 50.5] Masked array : [-- 20.2 -- 40.4 50.5] float64 Output typecasted array : [-- 20 -- 40 50] int32
Similar Reads
- 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.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.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.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.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.average() function | Python 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 3 min read
- Numpy MaskedArray.conjugate() function | Python numpy.MaskedArray.conjugate() function is used to return the complex conjugate, element-wise.The conjugate of a complex number is obtained by changing the sign of its imaginary part. Syntax : numpy.ma.conjugate(arr, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True)Paramet 2 min read
- Numpy MaskedArray.atleast_2d() function | Python numpy.MaskedArray.atleast_2d() function is used to convert inputs to masked arrays with at least two dimension.Scalar and 1-dimensional arrays are converted to 2-dimensional arrays, whilst higher-dimensional inputs are preserved. Syntax : numpy.ma.atleast_2d(*arys) Parameters: arys:[ array_like] One 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