# NumPy - 高階索引

## 整數索引

### 範例 1

``````import numpy as np

x = np.array([[1,  2],  [3,  4],  [5,  6]])
y = x[[0,1,2],  [0,1,0]]
print y
``````

``````[1  4  5]
``````

### 範例 2

``````import numpy as np
x = np.array([[  0,  1,  2],[  3,  4,  5],[  6,  7,  8],[  9,  10,  11]])
print  '我們的陣列是：'
print x
print  '\n'
rows = np.array([[0,0],[3,3]])
cols = np.array([[0,2],[0,2]])
y = x[rows,cols]
print  '這個陣列的每個角處的元素是：'
print y
``````

``````我們的陣列是：
[[ 0  1  2]
[ 3  4  5]
[ 6  7  8]
[ 9 10 11]]

[[ 0  2]
[ 9 11]]
``````

### 範例 3

``````import numpy as np
x = np.array([[  0,  1,  2],[  3,  4,  5],[  6,  7,  8],[  9,  10,  11]])
print  '我們的陣列是：'
print x
print  '\n'
# 切片
z = x[1:4,1:3]
print  '切片之後，我們的陣列變為：'
print z
print  '\n'
# 對列使用高階索引
y = x[1:4,[1,2]]
print  '對列使用高階索引來切片：'
print y
``````

``````我們的陣列是：
[[ 0  1  2]
[ 3  4  5]
[ 6  7  8]
[ 9 10 11]]

[[ 4  5]
[ 7  8]
[10 11]]

[[ 4  5]
[ 7  8]
[10 11]]
``````

## 布林索引

### 範例 1

``````import numpy as np
x = np.array([[  0,  1,  2],[  3,  4,  5],[  6,  7,  8],[  9,  10,  11]])
print  '我們的陣列是：'
print x
print  '\n'
# 現在我們會列印出大於 5 的元素
print  '大於 5 的元素是：'
print x[x >  5]
``````

``````我們的陣列是：
[[ 0  1  2]
[ 3  4  5]
[ 6  7  8]
[ 9 10 11]]

[ 6  7  8  9 10 11]
``````

### 範例 2

``````import numpy as np
a = np.array([np.nan,  1,2,np.nan,3,4,5])
print a[~np.isnan(a)]
``````

``````[ 1.   2.   3.   4.   5.]
``````

### 範例 3

``````import numpy as np
a = np.array([1,  2+6j,  5,  3.5+5j])
print a[np.iscomplex(a)]
``````

``````[2.0+6.j  3.5+5.j]
``````