question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The `argmin` method of the masked array is not working properly.

See original GitHub issue

The 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:open
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sebergcommented, Nov 4, 2018

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…

0reactions
kechancommented, Jan 3, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found