numpy.ma.where() function Python (original) (raw)
Last Updated : 05 May, 2020
numpy.ma.where()
function return a masked array with elements from x or y, depending on condition.
Syntax : numpy.ma.where(condition, x, y)
Parameter :
condition : [array_like, bool] Where True, yield x, otherwise yield y.
x, y : [array_like, optional] Values from which to choose. x, y and condition need to be broadcastable to some shape.Return : [MaskedArray] An masked array with masked elements where the condition is masked, elements from x where condition is True, and elements from y elsewhere.
Code #1 :
import
numpy as geek
import
numpy.ma as ma
x
=
geek.ma.array(geek.arange(
4.
).reshape(
2
,
2
),
`` mask
=
[[
0
,
1
], [
1
,
0
]])
gfg
=
geek.ma.where(x >
5
, x,
-
3.1416
)
print
(gfg)
Output :
[[-3.1416 --] [-- -3.1416]]
Code #2 :
import
numpy as geek
import
numpy.ma as ma
x
=
geek.ma.array(geek.arange(
9.
).reshape(
3
,
3
),
`` mask
=
[[
0
,
1
,
0
], [
1
,
0
,
1
], [
0
,
1
,
0
]])
gfg
=
geek.ma.where(x >
5
, x,
-
3.1416
)
print
(gfg)
Output :
[[-3.1416 -- -3.1416] [-- -3.1416 --] [6.0 -- 8.0]]
Similar Reads
- numpy.who function - Python numpy.who() function print the NumPy arrays in the given dictionary. Syntax : numpy.who(vardict = None) Parameters : vardict : [dict, optional] A dictionary possibly containing ndarrays. Return : Returns ‘None’. If there is no dictionary passed in or vardict is None then returns NumPy arrays in the 1 min read
- numpy.select() function - Python The numpy.select() function is used to construct an array by selecting elements from a list of choices based on multiple conditions. It is particularly useful when dealing with conditional replacements or transformations in NumPy arrays. Example:[GFGTABS] Python import numpy as np arr = np.array([10 3 min read
- numpy.where() in Python We will explore the basics of numpy.where(), how it works, and practical use cases to illustrate its importance in data manipulation and analysis. Syntax of numpy.where()Syntax :numpy.where(condition[, x, y]) Parameters condition: A condition that tests elements of the array.x (optional): Values fro 3 min read
- Numpy MaskedArray.masked_where() 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.ma.filled() function - Python numpy.ma.filled() function return input as an array with masked data replaced by a fill value. If arr is not a MaskedArray, arr itself is returned. If arr is a MaskedArray and fill_value is None, fill_value is set to arr.fill_value. Syntax : numpy.ma.filled(arr, fill_value = None) Parameters : arr : 1 min read
- numpy.ma.is_mask() function | Python numpy.ma.is_mask() function return True if parameter m is a valid, standard mask. This function does not check the contents of the input, only that the type is MaskType. In particular, this function returns False if the mask has a flexible dtype. Syntax : numpy.ma.is_mask(m) Parameter : m : [array_l 1 min read
- numpy.ma.mask_or() function | Python numpy.ma.mask_or() function combine two masks with the logical_or operator. The result may be a view on m1 or m2 if the other is nomask (i.e. False). Syntax : numpy.ma.mask_or(m1, m2, copy = False, shrink = True) Parameters : m1, m2 : [ array_like] Input masks. copy : [bool, optional] If copy is Fal 2 min read
- numpy.ma.allclose() function - Python numpy.ma.allclose() function returns True if two arrays are element-wise equal within a tolerance. This function is equivalent to allclose except that masked values are treated as equal (default) or unequal, depending on the masked_equal argument. Syntax : numpy.ma.allclose(a, b, masked_equal = True 2 min read
- numpy.ma.masked_values() function | Python numpy.ma.masked_values() function return a MaskedArray, masked where the data in array arr are approximately equal to value, determined using isclose. The default tolerances for masked_values are the same as those for isclose. Syntax : numpy.ma.masked_values(arr, value, rtol = 1e-05, atol = 1e-08, c 2 min read
- numpy.ma.make_mask() function | Python numpy.ma.make_mask() function is used to create a boolean mask from an array. This function can accept any sequence that is convertible to integers, or nomask. It does not require that contents must be 0s and 1s, values of 0 are interpreted as False, everything else as True. Return m as a boolean ma 2 min read