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.

vectorize fails for zero-dimensioned arrays

See original GitHub issue

The following used to work and return an empty 5 by 0 array.

np.vectorize(lambda x: x+1)(np.zeros([5,0]))

Now it stack traces:

/usr/lib/python2.7/dist-packages/numpy/lib/function_base.pyc in __call__(self, *args, **kwargs)
   1571             vargs.extend([kwargs[_n] for _n in names])
   1572 
-> 1573         return self._vectorize_call(func=func, args=vargs)
   1574 
   1575     def _get_ufunc_and_otypes(self, func, args):

/usr/lib/python2.7/dist-packages/numpy/lib/function_base.pyc in _vectorize_call(self, func, args)
   1631             _res = func()
   1632         else:
-> 1633             ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
   1634 
   1635             # Convert args to object arrays first

/usr/lib/python2.7/dist-packages/numpy/lib/function_base.pyc in _get_ufunc_and_otypes(self, func, args)
   1594             # Assumes that ufunc first evaluates the 0th elements in the input
   1595             # arrays (the input values are not checked to ensure this)
-> 1596             inputs = [asarray(_a).flat[0] for _a in args]
   1597             outputs = func(*inputs)
   1598 

IndexError: index 0 is out of bounds for axis 0 with size 0
> /usr/lib/python2.7/dist-packages/numpy/lib/function_base.py(1596)_get_ufunc_and_otypes()
   1595             # arrays (the input values are not checked to ensure this)
-> 1596             inputs = [asarray(_a).flat[0] for _a in args]
   1597             outputs = func(*inputs)

Seems to me that there should first be a check for empty arrays before attempting to see the return types of the function?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sebergcommented, May 14, 2015

Sure, this would be better to work again if possible (even if I don’t like vectorize much 😉). The code seems only for output type detection (which may indeed be tricky with empty arguments).

So the workaround is:

np.vectorize(lambda x: x+1, otypes=[np.float64])(np.zeros([5,0]))
0reactions
shoyercommented, Oct 11, 2016

I added handling for size 0 inputs (basically, just a better error message) in https://github.com/numpy/numpy/pull/8054

Read more comments on GitHub >

github_iconTop Results From Across the Web

np.vectorize fails on a 2-d numpy array as input - Stack Overflow
I have printed the argument that is passed in to the fun which prints a single value (the first element of the vector)...
Read more >
numpy.vectorize — NumPy v1.24 Manual
Define a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns a single numpy array or...
Read more >
Solved: Error with array dimensions - PTC Community
Hello everyone,. I'm trying to square the the values in a non-square matrix in a function. In order to do this, I use...
Read more >
Creating NumPy universal functions - Numba
Using the vectorize() decorator, Numba can compile a pure Python function into a ufunc that operates over NumPy arrays as fast as traditional...
Read more >
numpy - Python Vectorizing a Function Returning an Array
The problem is that np.cos(t) and np.sqrt(t) generate arrays with the length of t , whereas the second row ( [0,1] ) maintains...
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