10000 The behavior of numpy.apply_along_axis function is not what expected. · Issue #5248 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

The behavior of numpy.apply_along_axis function is not what expected. #5248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yorklee70 opened this issue Oct 30, 2014 · 2 comments
Closed

Comments

@yorklee70
Copy link
yorklee70 commented Oct 30, 2014

When I use python set as elelments of a numpy array, the behavior of numpy.apply_along_axis function is not what expected. For example, I want to get the union set for each column using the following codes:

import numpy
import operator
d = numpy.array([  [ set([1, 11]), set([2, 22]), set([3, 33]) ],
                              [ set([4, 44]), set([5, 55]), set([6, 66]) ]  ])
numpy.apply_along_axis( lambda a:reduce(operator.**or**, a), 0, d)

i.e. I expect to get a result like:

    array([ set(1, 11, 4, 44), set(2, 22, 5, 55), set(3, 33, 6, 66) ]

however, the result is:

    array([ [ set(1, 11, 4, 44), set(2, 22, 5, 55), set(3, 33, 6, 66) ],
               [ set(1, 11, 4, 44), set(2, 22, 5, 55), set(3, 33, 6, 66) ],
               [ set(1, 11, 4, 44), set(2, 22, 5, 55), set(3, 33, 6, 66) ],
               [ set(1, 11, 4, 44), set(2, 22, 5, 55), set(3, 33, 6, 66) ] ])

The expected result was repeated 4 times, which is determined by the first element, actually it equals to the length of the first element.

Can this be viewed as a bug? Anyway it is different with the way of the "apply" function in R language.

York

@eric-wieser
Copy link
Member
eric-wieser commented Feb 19, 2017

So, this was kind of fixed by #8441, but also kind of not:

>>> import functools
>>> import operator
>>> import numpy as np
>>> d = np.array([  [ set([1, 11]), set([2, 22]), set([3, 33]) ],  [ set([4, 44]), set([5, 55]), set([6, 66]) ]  ])
>>> np.apply_along_axis( lambda a: functools.reduce(operator.or_, a), 0, d)
array([array({1, 11, 4, 44}, dtype=object),
       array({2, 5, 22, 55}, dtype=object),
       array({33, 66, 3, 6}, dtype=object)], dtype=object)

@eric-wieser
Copy link
Member

Closing in favor of #8642

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0