Find the min/max excluding zeros in a numpy array (or a tuple) in python

PythonNumpy

Python Problem Overview


I have an array. The valid values are not zero (either positive or negetive). I want to find the minimum and maximum within the array which should not take zeros into account. For example if the numbers are only negative. Zeros will be problematic.

Python Solutions


Solution 1 - Python

How about:

import numpy as np
minval = np.min(a[np.nonzero(a)])
maxval = np.max(a[np.nonzero(a)])

where a is your array.

Solution 2 - Python

If you can choose the "invalid" value in your array, it is better to use nan instead of 0:

>>> a = numpy.array([1.0, numpy.nan, 2.0])
>>> numpy.nanmax(a)
2.0
>>> numpy.nanmin(a)
1.0

If this is not possible, you can use an array mask:

>>> a = numpy.array([1.0, 0.0, 2.0])
>>> masked_a = numpy.ma.masked_equal(a, 0.0, copy=False)
>>> masked_a.max()
2.0
>>> masked_a.min()
1.0

Compared to Josh's answer using advanced indexing, this has the advantage of avoiding to create a copy of the array.

Solution 3 - Python

Here's another way of masking which I think is easier to remember (although it does copy the array). For the case in point, it goes like this:

>>> import numpy
>>> a = numpy.array([1.0, 0.0, 2.0])
>>> ma = a[a != 0]
>>> ma.max()
2.0
>>> ma.min()
1.0
>>> 

It generalizes to other expressions such as a > 0, numpy.isnan(a), ... And you can combine masks with standard operators (+ means OR, * means AND, - means NOT) e.g:

# Identify elements that are outside interpolation domain or NaN
outside = (xi < x[0]) + (eta < y[0]) + (xi > x[-1]) + (eta > y[-1])
outside += numpy.isnan(xi) + numpy.isnan(eta)
inside = -outside
xi = xi[inside]
eta = eta[inside]

Solution 4 - Python

You could use a generator expression to filter out the zeros:

array = [-2, 0, -4, 0, -3, -2]
max(x for x in array if x != 0)

Solution 5 - Python

A simple way would be to use a list comprehension to exclude zeros.

>>> tup = (0, 1, 2, 5, 2)
>>> min([x for x in tup if x !=0])
1

Solution 6 - Python

Masked arrays in general are designed exactly for these kind of purposes. You can leverage masking zeros from an array (or ANY other kind of mask you desire, even masks that are more complicated than a simple equality) and do pretty much most of the stuff you do on regular arrays on your masked array. You can also specify an axis for which you wish to find the min along:

import numpy.ma as ma
mx = ma.masked_array(x, mask=x==0)
mx.min()

Example input:

x = np.array([1.0, 0.0, 2.0])

output:

1.0

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShanView Question on Stackoverflow
Solution 1 - PythonJoshAdelView Answer on Stackoverflow
Solution 2 - PythonSven MarnachView Answer on Stackoverflow
Solution 3 - PythonuniomniView Answer on Stackoverflow
Solution 4 - PythonChris PickettView Answer on Stackoverflow
Solution 5 - PythonWilduckView Answer on Stackoverflow
Solution 6 - PythonEhsanView Answer on Stackoverflow