# NumPy - 陣列建立例程

## `numpy.empty`

``````numpy.empty(shape, dtype = float, order = 'C')
``````

1. `Shape` 空陣列的形狀，整數或整數元組
2. `Dtype` 所需的輸出陣列型別，可選
3. `Order` `'C'`為按行的 C 風格陣列，`'F'`為按列的 Fortran 風格陣列

### 範例

``````import numpy as np
x = np.empty([3,2], dtype =  int)
print x
``````

``````[[22649312    1701344351]
[1818321759  1885959276]
[16779776    156368896]]
``````

## `numpy.zeros`

``````numpy.zeros(shape, dtype = float, order = 'C')
``````

1. `Shape` 空陣列的形狀，整數或整數元組
2. `Dtype` 所需的輸出陣列型別，可選
3. `Order` `'C'`為按行的 C 風格陣列，`'F'`為按列的 Fortran 風格陣列

### 範例 1

``````# 含有 5 個 0 的陣列，預設型別為 float
import numpy as np
x = np.zeros(5)
print x
``````

``````[ 0.  0.  0.  0.  0.]
``````

### 範例 2

``````import numpy as np
x = np.zeros((5,), dtype = np.int)
print x
``````

``````[0  0  0  0  0]
``````

### 範例 3

``````# 自定義型別
import numpy as np
x = np.zeros((2,2), dtype =  [('x',  'i4'),  ('y',  'i4')])
print x
``````

``````[[(0,0)(0,0)]
[(0,0)(0,0)]]
``````

## `numpy.ones`

``````numpy.ones(shape, dtype = None, order = 'C')
``````

1. `Shape` 空陣列的形狀，整數或整數元組
2. `Dtype` 所需的輸出陣列型別，可選
3. `Order` `'C'`為按行的 C 風格陣列，`'F'`為按列的 Fortran 風格陣列

### 範例 1

``````# 含有 5 個 1 的陣列，預設型別為 float
import numpy as np
x = np.ones(5)  print x
``````

``````[ 1.  1.  1.  1.  1.]
``````

### 範例 2

``````import numpy as np
x = np.ones([2,2], dtype =  int)
print x
``````

``````[[1  1]
[1  1]]
``````