The `argmin` method of the masked array is not working properly.
See original GitHub issueThe argmin
method of the masked array is not working properly.
import numpy as np
a = np.ma.array([1, 2, 1e+21, 1e+22], mask=[1, 1, 0, 0])
d = a.filled().view(np.ndarray)
print(d.argmin()) # 0
print(a.argmin()) # 2
However, the argmin
method is implemented by computing d
and then returning d.argmin()
:
# https://github.com/numpy/numpy/blob/master/numpy/ma/core.py
def argmin(self, axis=None, fill_value=None, out=None):
if fill_value is None:
fill_value = minimum_fill_value(self)
d = self.filled(fill_value).view(ndarray)
return d.argmin(axis, out=out)
So why I am getting two different results?
On the other hand, when the unmasked items are all inf’s, then the argmin
returns always 0:
a = np.ma.array([1, 2, np.inf, np.inf], mask=[1, 1, 0, 0])
d = a.filled().view(np.ndarray)
print(d.argmin()) # 0
print(a.argmin()) # 0
But in both cases fill_value=1e+20
is smaller than a[2] = 1e+21
and a[2] = np.inf
. In any case, the output I would expect is 2 for both examples, since I don’t want to compare the unmasked items with the masked items.
Numpy/Python version information:
1.15.3 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)]
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Numpy masked array argmax not returning 'masked' on fully ...
From the documentation (emphasis mine): Returns array of indices of the maximum values along the given axis.
Read more >numpy.ma.MaskedArray.argmin — NumPy v1.24 Manual
If None, the index is into the flattened array, otherwise along the specified axis.
Read more >Missing data: masked arrays
When working with real oceanographic data sets, there are often gaps. One way of handling them is to fill them with NaN, and...
Read more >Numpy MaskedArray.argmin() function | Python - GeeksforGeeks
numpy.MaskedArray.argmin() function returns array of indices of the minimum values along the given axis. Masked values are treated as if they ...
Read more >Know About Numpy argmin Function in Python
We can mask the nan values present in a given array using the MaskedArray. All the nan values will be masked and not...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Not sure, but maybe something like
np.maximum(np.argmin(mask), np.argmin(filled))
might work. If the first unfilled value is actually larger then the found minimum, it must be an inf.EDIT: The question is if that opens another bag of problems somehow…
I would love to do min and argmin on masked array as welll. I was looking for a solution in TF Tensor but landed here.