numpy.vectorize#

class numpy.vectorize(pyfunc=np._NoValue, otypes=None, doc=None, excluded=None, cache=False, signature=None)[原始碼]#

回傳一個行為類似 pyfunc,但接受陣列作為輸入的物件。

定義一個向量化函數,它接受物件或 NumPy 陣列的巢狀序列作為輸入,並回傳單個 NumPy 陣列或 NumPy 陣列的元組。向量化函數會像 Python 的 map 函數一樣,對輸入陣列的連續元組評估 pyfunc,但它使用 NumPy 的廣播規則。

vectorized 輸出的資料型別,是透過使用輸入的第一個元素呼叫函數來決定的。可以透過指定 otypes 參數來避免這種情況。

參數:
pyfunc可呼叫的,選填

一個 Python 函數或方法。可以省略以產生帶有關鍵字參數的裝飾器。

otypes字串或 dtype 列表,選填

輸出資料型別。它必須指定為型別代碼字串或資料型別指定符的列表。每個輸出應該有一個資料型別指定符。

doc字串,選填

函數的文件字串。如果為 None,文件字串將會是 pyfunc.__doc__

excluded集合,選填

字串或整數的集合,表示函數不會向量化的位置或關鍵字參數。這些將直接傳遞給 pyfunc,不作修改。

cache布林值,選填

如果為 True,則在未提供 otypes 的情況下,快取決定輸出數量的第一次函數呼叫。

signature字串,選填

廣義通用函數簽名,例如 (m,n),(n)->(m) 用於向量化矩陣-向量乘法。如果提供,pyfunc 將會被呼叫(並預期回傳)形狀由相應核心維度大小給定的陣列。預設情況下,pyfunc 假設接受純量作為輸入和輸出。

回傳值:
out可呼叫的

如果提供了 pyfunc,則為向量化函數;否則為裝飾器。

另請參閱

frompyfunc

接受任意 Python 函數並回傳 ufunc

註解

vectorize 函數主要為了方便而提供,而不是為了效能。實作本質上是一個 for 迴圈。

如果未指定 otypes,則會使用使用第一個引數呼叫函數來決定輸出數量。如果 cacheTrue,則會快取此呼叫的結果,以防止呼叫函數兩次。然而,為了實作快取,原始函數必須被包裝,這會減慢後續呼叫的速度,因此只有在您的函數成本很高時才這樣做。

新的關鍵字參數介面和 excluded 參數支援進一步降低效能。

參考文獻

範例

>>> import numpy as np
>>> def myfunc(a, b):
...     "Return a-b if a>b, otherwise return a+b"
...     if a > b:
...         return a - b
...     else:
...         return a + b
>>> vfunc = np.vectorize(myfunc)
>>> vfunc([1, 2, 3, 4], 2)
array([3, 4, 1, 2])

文件字串取自 vectorize 的輸入函數,除非有指定。

>>> vfunc.__doc__
'Return a-b if a>b, otherwise return a+b'
>>> vfunc = np.vectorize(myfunc, doc='Vectorized `myfunc`')
>>> vfunc.__doc__
'Vectorized `myfunc`'

輸出型別是透過評估輸入的第一個元素決定的,除非有指定。

>>> out = vfunc([1, 2, 3, 4], 2)
>>> type(out[0])
<class 'numpy.int64'>
>>> vfunc = np.vectorize(myfunc, otypes=[float])
>>> out = vfunc([1, 2, 3, 4], 2)
>>> type(out[0])
<class 'numpy.float64'>

excluded 參數可用於防止對某些引數進行向量化。這對於固定長度的類陣列引數很有用,例如多項式的係數,如 polyval 中所示

>>> def mypolyval(p, x):
...     _p = list(p)
...     res = _p.pop(0)
...     while _p:
...         res = res*x + _p.pop(0)
...     return res

在這裡,我們將第零個引數排除在向量化之外,無論它是按位置或關鍵字傳遞的。

>>> vpolyval = np.vectorize(mypolyval, excluded={0, 'p'})
>>> vpolyval([1, 2, 3], x=[0, 1])
array([3, 6])
>>> vpolyval(p=[1, 2, 3], x=[0, 1])
array([3, 6])

signature 參數允許對作用於固定長度的非純量陣列的函數進行向量化。例如,您可以將其用於 Pearson 相關係數及其 p 值的向量化計算

>>> import scipy.stats
>>> pearsonr = np.vectorize(scipy.stats.pearsonr,
...                 signature='(n),(n)->(),()')
>>> pearsonr([[0, 1, 2, 3]], [[1, 2, 3, 4], [4, 3, 2, 1]])
(array([ 1., -1.]), array([ 0.,  0.]))

或用於向量化卷積

>>> convolve = np.vectorize(np.convolve, signature='(n),(m)->(k)')
>>> convolve(np.eye(4), [1, 2, 1])
array([[1., 2., 1., 0., 0., 0.],
       [0., 1., 2., 1., 0., 0.],
       [0., 0., 1., 2., 1., 0.],
       [0., 0., 0., 1., 2., 1.]])

支援裝飾器語法。裝飾器可以作為函數呼叫,以提供關鍵字引數

>>> @np.vectorize
... def identity(x):
...     return x
...
>>> identity([0, 1, 2])
array([0, 1, 2])
>>> @np.vectorize(otypes=[float])
... def as_float(x):
...     return x
...
>>> as_float([0, 1, 2])
array([0., 1., 2.])

方法

__call__(*args, **kwargs)

將自身作為函數呼叫。