例如,尝试理解这些结果:
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> (x == np.array([[1],[2]])).astype(np.float32)
array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)
>>> (x == np.array([1,2]))
False
>>> (x == np.array([[1]])).astype(np.float32)
array([[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)
>>> (x == np.array([1])).astype(np.float32)
array([ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
>>> (x == …Run Code Online (Sandbox Code Playgroud) 我有两个numpy数组:a和b.我想选择所有的索引a == 1和b == 0.
也就是说,如果我有以下数组:
a = [0, 1, 3, 5, 1, 1, 2]
b = [1, 0, 2, 5, 3, 0, 6]
Run Code Online (Sandbox Code Playgroud)
我想得到以下索引:
[1, 5]
Run Code Online (Sandbox Code Playgroud)
我怎么能在numpy中做到这一点?我尝试过使用以下内容(快速参考指南建议显示numpy,matlab和IDL之间的差异):
(a == 1 and b == 0).nonzero()
Run Code Online (Sandbox Code Playgroud)
但这给出了关于真值的含糊不清的错误.
有任何想法吗?