簡體   English   中英

Numpy: 如何 map f: (shape (3) ndarray) --> (float) over an ndarray of shape (...,3) 以獲得 ndarray of shape (...)?

[英]Numpy: How to map f: (shape (3) ndarray) --> (float) over an ndarray of shape (…,3) to get ndarray of shape (…)?

我有一個 function 將形狀 (3) 的 ndarray 映射到浮點數,並且我有一個形狀 (...,3) 的 ndarray。 map function 在該陣列上獲得形狀陣列(...)的最佳方法是什么?

謝謝。

你想要numpy.apply_along_axis

def f(a):
     return a[0] + a[1] + a[2]
mm = numpy.random.randn(5, 3)
numpy.apply_along_axis(f, 1, mm)

output: array([-1.75875289, -0.34689792, 0.66092486, -0.21626001, -0.14125476])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM