简体   繁体   中英

Apply a function along all elements in a array

I have some array:

array = np.array([[1, 0], [1, 0], [1, 0]])

I want to calculate np.kron in a such manner:

def func(array):
    res = array[0]
    for i in range(1, len(array)):
        res = np.kron(res, array[i]) 
    return res

But how to do it in a more numpy way?

I tried np.apply_along_axis :

res = np.apply_along_axis(np.kron, 1, array)

But I didn't succeed.

我不知道它是否更“ numpy”,但是绝对更Python化的方法是使用reduce内置函数:

reduce(np.kron, array)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM