本文简要介绍 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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。