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.

apply_along_axis cuts strings

See original GitHub issue

I’m trying to concatenate all elements of a row into a string as follows:

np.apply_along_axis(lambda x: " ".join(map(str, x)), 1, b)

b is

[[111,111,0,0,0], [111,111,111,111,111]]

However, the result of the line is:

['111 111 0 0 0', '111 111 111 1']

It looks like np.apply_along_axis is cutting the second string to be of the same length as the first one. If I put a longer sequence first, the result is correct:

['111 111 111 111 111', '111 111 0 0 0']

So I’m guessing this is a bug?


Summary 2019-04-30 by @seberg

np.apply_along_axis infers the output dtype from the first pass. Which can be worked around for example but the function returning an array of a correct type.

Actions:

  • np.apply_along_axis could/should get a dtype kwarg (or similar, compare also np.vectorize).

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:5
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

8reactions
eric-wiesercommented, May 27, 2017

I’m assuming that that’s a deliberately contrived example, because you shouldn’t be using apply_along_axis for simple indexing like that.

np.ma.apply_along_axis will work correctly here (for now - see #8511). Another option (crashed until 1.13) is a manual cast to dtype object:

np.apply_along_axis(lambda x: np.array(x[0], object), 1, fnames)
7reactions
eric-wiesercommented, Dec 29, 2017

Perhaps we should just add a dtype= argument to apply_along_axis

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.apply_along_axis truncates strings because it infers ...
I want to: apply_along_axis to my_array. for every row, return a string. def my_function(x): ...
Read more >
numpy.apply_along_axis — NumPy v1.24 Manual
Apply a function to 1-D slices along the given axis. Execute func1d(a, *args, **kwargs) where func1d operates on 1-D arrays and a is...
Read more >
Python NumPy apply_along_axis() Function Example - Morioh
Python NumPy apply_along_axis() is an inbuilt NumPy function that is used to apply a function to 1D slices along the given axis of...
Read more >
cupyx.GeneralizedUFunc — CuPy 12.0.0b2 documentation
signatures (list of tuple of str) – Contains strings in the form of 'ii->i' with i being the ... This is a short-cut...
Read more >
Revisiting Numpy and ndarray - Towards Data Science
Let's have a closer look, U8 means string with 8 characters, ... very best of Towards Data Science: from hands-on tutorials and cutting-edge ......
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