2015-04-18 30 views
4

我讀了numpy.where(condition[, x, y])documentation,但我不明白的小例子:瞭解有關numpy.where

>>> x = np.arange(9.).reshape(3, 3) 
>>> np.where(x > 5) 
Out: (array([2, 2, 2]), array([0, 1, 2])) 

一些人能夠解釋的結果是怎麼來?

回答

4

第一陣列(array([2, 2, 2]))是行(array([0, 1, 2]))的索引,第二個是那些大於5

可以使用zip獲得值的精確指標值的列:

>>> zip(*np.where(x > 5)) 
[(2, 0), (2, 1), (2, 2)] 

或者使用np.dstack

>>> np.dstack(np.where(x > 5)) 
array([[[2, 0], 
     [2, 1], 
     [2, 2]]]) 
+0

*表示什麼? – Raghuram

+1

@Raghuram這是就地解壓操作數,解包迭代。在這種情況下,它將把'np.where'中的項目和indeples元組傳遞給'zip'函數。 – Kasramvd

+0

非常感謝。今天學了點兒新東西! :) – Raghuram

2

它打印出的座標您康迪灰

import numpy as np 

x = np.arange(9.).reshape(3, 3) 
print x 
print np.where(x > 5) 

其中打印X打印:

[[ 0. 1. 2.] 
[ 3. 4. 5.] 
[ 6. 7. 8.]] 

np.where(x > 5)打印所有元素的索引位置超過5

(array([2, 2, 2]), array([0, 1, 2])) 

其中2,0 == 6和2,1 == 7和2,2 == 8