本文簡要介紹 python 語言中 numpy.choose
的用法。
用法:
numpy.choose(a, choices, out=None, mode='raise')
從索引數組和可供選擇的數組列表構造一個數組。
首先,如果您感到困惑或不確定,請務必查看示例 - 總體而言,此函數不像以下代碼說明中看起來那麽簡單(在 ndi =
numpy.lib.index_tricks
下方):np.choose(a,c) == np.array([c[a[I]][I] for I in ndi.ndindex(a.shape)])
.但這忽略了一些微妙之處。這是一個完全一般的摘要:
給定一個 “index” 數組 (a) 的整數和一個序列
n
數組(選擇),a並且每個選擇數組根據需要首先廣播到一個共同形狀的數組;調用這些Ba和Bchoices[i], i = 0,…,n-1我們有,必然,Ba.shape == Bchoices[i].shape
對於每個i
.然後,一個新的形狀數組Ba.shape
創建如下:如果
mode='raise'
(默認值),那麽首先,a
(以及因此Ba
)的每個元素必須在[0, n-1]
範圍內;現在,假設i
(在那個範圍內)是Ba
中(j0, j1, ..., jm)
位置的值-那麽新數組中相同位置的值就是Bchoices[i]
中相同位置的值;如果
mode='wrap'
, 中的值a(因此Ba) 可以是任何(有符號的)整數;模運算用於映射範圍外的整數[0, n-1]回到那個範圍;然後像上麵那樣構造新數組;如果
mode='clip'
, 中的值a(因此Ba
) 可以是任何(有符號的)整數;負整數映射到 0;值大於n-1
映射到n-1
;然後像上麵那樣構造新數組。
- a: int 數組
此數組必須在
[0, n-1]
中包含整數,其中n
是選擇的數量,除非mode=wrap
或mode=clip
,在這種情況下任何整數都是允許的。- choices: 數組序列
選擇數組。a並且所有的選擇必須可以廣播到相同的形狀。如果選擇本身是一個數組(不推薦),然後是它的最外層維度(即對應於
choices.shape[0]
) 被視為定義“sequence”。- out: 數組,可選
如果提供,結果將被插入到這個數組中。它應該具有適當的形狀和數據類型。注意out總是緩衝如果
mode='raise'
;使用其他模式以獲得更好的性能。- mode: {‘raise’(默認),‘wrap’, ‘clip’},可選
指定如何處理
[0, n-1]
之外的索引:‘raise’:引發異常
‘wrap’ : value 變成 value mod
n
‘clip’:值 < 0 映射到 0,值 > n-1 映射到 n-1
- merged_array: 數組
合並的結果。
- ValueError:形狀不匹配
如果 a 和每個選擇數組都不能廣播到相同的形狀。
參數:
返回:
拋出:
注意:
為了減少誤解的機會,即使名義上支持以下 “abuse”,但選擇既不應該也不應該被認為是單個數組,即最外麵的 sequence-like 容器應該是列表或元組。
例子:
>>> choices = [[0, 1, 2, 3], [10, 11, 12, 13], ... [20, 21, 22, 23], [30, 31, 32, 33]] >>> np.choose([2, 3, 1, 0], choices ... # the first element of the result will be the first element of the ... # third (2+1) "array" in choices, namely, 20; the second element ... # will be the second element of the fourth (3+1) choice array, i.e., ... # 31, etc. ... ) array([20, 31, 12, 3]) >>> np.choose([2, 4, 1, 0], choices, mode='clip') # 4 goes to 3 (4-1) array([20, 31, 12, 3]) >>> # because there are 4 choice arrays >>> np.choose([2, 4, 1, 0], choices, mode='wrap') # 4 goes to (4 mod 4) array([20, 1, 12, 3]) >>> # i.e., 0
幾個示例說明如何選擇廣播:
>>> a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]] >>> choices = [-10, 10] >>> np.choose(a, choices) array([[ 10, -10, 10], [-10, 10, -10], [ 10, -10, 10]])
>>> # With thanks to Anne Archibald >>> a = np.array([0, 1]).reshape((2,1,1)) >>> c1 = np.array([1, 2, 3]).reshape((1,3,1)) >>> c2 = np.array([-1, -2, -3, -4, -5]).reshape((1,1,5)) >>> np.choose(a, (c1, c2)) # result is 2x3x5, res[0,:,:]=c1, res[1,:,:]=c2 array([[[ 1, 1, 1, 1, 1], [ 2, 2, 2, 2, 2], [ 3, 3, 3, 3, 3]], [[-1, -2, -3, -4, -5], [-1, -2, -3, -4, -5], [-1, -2, -3, -4, -5]]])
相關用法
- Python numpy chararray.ndim用法及代碼示例
- Python numpy chebyshev.chebsub用法及代碼示例
- Python numpy chararray.nbytes用法及代碼示例
- Python numpy chebyshev.chebdiv用法及代碼示例
- Python numpy chararray.setflags用法及代碼示例
- Python numpy chararray.flat用法及代碼示例
- Python numpy chararray.strides用法及代碼示例
- Python numpy chebyshev.cheb2poly用法及代碼示例
- Python numpy chararray.view用法及代碼示例
- Python numpy chebyshev.chebx用法及代碼示例
- Python numpy chebyshev.chebmul用法及代碼示例
- Python numpy char.strip用法及代碼示例
- Python numpy chebyshev.chebroots用法及代碼示例
- Python numpy char.upper用法及代碼示例
- Python numpy chararray.imag用法及代碼示例
- Python numpy chararray.base用法及代碼示例
- Python numpy chararray.flatten用法及代碼示例
- Python numpy chararray.copy用法及代碼示例
- Python numpy char.count用法及代碼示例
- Python numpy char.chararray用法及代碼示例
- Python numpy chararray.resize用法及代碼示例
- Python numpy char.rstrip用法及代碼示例
- Python numpy chararray.sort用法及代碼示例
- Python numpy chararray.astype用法及代碼示例
- Python numpy chebyshev.chebtrim用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.choose。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。