The confusion about numpy applies along the axis and the list of concepts

Okay, so I apologize ahead of time if I just ask something stupid, but I really thought I understood how it works apply_along_axis. I just ran into something that could be a regional case that I just did not consider, but it puzzled me. In short, this is the code that confuses me:

class Leaf(object):

    def __init__(self, location):
        self.location = location

    def __len__(self):
        return self.location.shape[0]

def bulk_leaves(child_array, axis=0):
    test = np.array([Leaf(location) for location in child_array])  # This is what I want
    check = np.apply_along_axis(Leaf, 0, child_array)  # This returns an array of individual leafs with the same shape as child_array
    return test, check

if __name__ == "__main__":
    test, check = bulk_leaves(np.random.ran(100, 50))
    test == check  # False

I always feel stupid using list comprehension with numpy and then return to the array, but I'm just not sure about the other way around this. Did I just miss something obvious?

+1
source share
2 answers

, apply_along_axis isscalar, , , isscalar False . apply_along_axis :

outarr arr, , outarr func1d.

__len__ , , numpy "" . __len__, , numpy , , len .

, . 1 __len__, Nx1 2D, 1D N. Numpy .

numpy apply_along_axis, , isscalar False numpy. , numpy , . , numpy, , , isscalar(object()), False.

, , , , . , , .

+2

apply_along_axis - Python, . :

check = np.empty(child_array.shape,dtype=object)
for i in range(child_array.shape[1]):
    check[:,i] = Leaf(child_array[:,i])

, , . , , , , , ( , ).

, , .

for i in range(check.shape[0]):
    check[i]=Leaf(child_array[i,:])

, . apply_along_axis, , .

+4

Source: https://habr.com/ru/post/1568977/


All Articles