Numpy的文档语法 [英] Numpy where syntax from docs
问题描述
尝试自学一些python,我对文档的where函数感到非常困惑.有人可以从下面的文档中逐步解释示例吗?
Trying to teach myself some python and I am super confused from the docs what the where function does. Can somebody explain the example from the documentation below step by step please?
>>> np.where([[True, False], [True, True]],
... [[1, 2], [3, 4]],
... [[9, 8], [7, 6]])
array([[1, 8],
[3, 4]])
推荐答案
基本语法为np.where(x, a, b)
在x
为true的地方,采用a
的元素,在其为false的地方,采用
The basic syntax is np.where(x, a, b)
Wherever x
is true, take that element of a
, and wherever it's false, take an element of b
. It's equivalent to something like this:
x =. . [[1,0],[1,1]]),而不是x = [[0,1],[0,0]]
x = . . [[1, 0], [1, 1]]), not x =[[0, 1], [0, 0 ]]
array([[1, 2 ],[3,4]])+ array([[ 7 ,8],[ 9 罢工, 10 ]])
array([[1, 2], [3, 4]]) + array([[7, 8], [9, 10]])
array([[1,0],[3,4]])+ array([[0,8],[0,0]])= array([[1,8],[3,4 ]])
array([[1, 0], [3, 4]]) + array([[0, 8], [0, 0 ]]) = array([[1, 8], [3, 4]])
这篇关于Numpy的文档语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!