2017/10/30
Python - numpy 的使用方法
markdown
[NumPy Reference](https://docs.scipy.org/doc/numpy-1.13.0/reference/index.html)
數列生成
```
>>> np.zeros(5)
#array([ 0., 0., 0., 0., 0.])
>>> np.ones(5)
#array([ 1., 1., 1., 1., 1.])
>>> np.arange(5)
#array([0, 1, 2, 3, 4])
```
[更多的數列生成](https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.array-creation.html)
數列變形
```
>>> np.arange(12)
#array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> np.arange(12).reshape(2,6)
#array([[ 0, 1, 2, 3, 4, 5],
# [ 6, 7, 8, 9, 10, 11]])
>>> np.arange(12).reshape(3,4)
#array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]])
>>> np.arange(12).reshape(4,3)
#array([[ 0, 1, 2],
# [ 3, 4, 5],
# [ 6, 7, 8],
# [ 9, 10, 11]])
>>> np.arange(12).reshape(6,2)
#array([[ 0, 1],
# [ 2, 3],
# [ 4, 5],
# [ 6, 7],
# [ 8, 9],
# [10, 11]])
>>> np.arange(12).reshape(12,1).flatten()
#array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
```
查看數列形狀
```
>>> np.arange(12).reshape(1,2,3,2)
#array([[[[ 0, 1],
# [ 2, 3],
# [ 4, 5]],
#
# [[ 6, 7],
# [ 8, 9],
# [10, 11]]]])
>>> np.arange(12).reshape(1,2,3,2).shape
#(1, 2, 3, 2)
```
[更多的數列操作](https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.array-manipulation.html)
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言