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.

min/max of empty arrays

See original GitHub issue

A similar issue was raised in #2670, however the edge case of (0, ) shape is still not covered. I can go around it with checking for non-zero length in an if statement, but it would be more practical if np.min could handle this, too.

In [24]: [np.min(param[x][1]) for x in fields]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-076aa74e3684> in <module>()
----> 1 [np.min(param[x][1]) for x in fields]

/data/wts/pipeline64/python/lib/python2.6/site-packages/numpy/core/fromnumeric.pyc in amin(a, axis, out)
   1893     except AttributeError:
   1894         return _wrapit(a, 'min', axis, out)
-> 1895     return amin(axis, out)
   1896 
   1897 

ValueError: zero-size array to minimum.reduce without identity

In [25]: param[x]
Out[25]: 
{1: array([], dtype=float64),
 2: array([], dtype=float64),
 3: array([], dtype=float64),
 4: array([], dtype=float64)}

In [26]: param[x][1].shape
Out[26]: (0,)

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:20 (19 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, Feb 20, 2018
  • reduceat doesn’t even use it’s identity - don’t open that can of worms. There’s another issue about that elsewhere
  • accumulate perhaps doesn’t need it? np.minimum.accumulate([]) is already well-defined
  • accumulateat doesn’t exist
1reaction
bsipoczcommented, Sep 3, 2014

OK, I see your point, however can’t see why it behaves differently for different empty arrays (e.g. getting the minimum for axis=1 and axis=0 in #2670’s example). Both are empty, still the minimum will be either an empty array or a ValueError.
To be honest, I would be most happy with a nan as a result to min([]), at least it seems more reasonable than an error.

In [12]: a = numpy.zeros((0,2))

In [13]: a
Out[13]: array([], shape=(0, 2), dtype=float64)

In [14]: np.min(a, axis=1)
Out[14]: array([], dtype=float64)

In [15]: np.min(a, axis=0)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-c8666bcd2a35> in <module>()
----> 1 np.min(a, axis=0)

/data/wts/pipeline64/python/lib/python2.6/site-packages/numpy/core/fromnumeric.pyc in amin(a, axis, out)
   1893     except AttributeError:
   1894         return _wrapit(a, 'min', axis, out)
-> 1895     return amin(axis, out)
   1896 
   1897 

ValueError: zero-size array to minimum.reduce without identity
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Getting empty array when calculating min and max
I'm trying to write a function that used reduce() method to find the min and max value in the array, and then returns...
Read more >
CodingBat minMax
Return both the smallest and the largest elements ({min, max}) in a non-empty array of integers. We'll use the convention of considering only...
Read more >
JavaScript find min/max from array of objects - Daily Dev Tips
How to find the min or max value from an array of objects. ... the following error: TypeError: Reduce of empty array with...
Read more >
Steps to make array empty by removing maximum and its right ...
Select the max number in the array and delete that number including all the numbers to its right side in the array.
Read more >
ReArrange Min/Max - Xavier Carty - Medium
Today we are going to talk about rearranging values in array min and max form. In this solution, we first create an empty...
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