2017/10/30

Python - numpy 的使用方法

NumPy Reference

數列生成

>>> 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])

更多的數列生成

數列變形

>>> 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)

更多的數列操作

沒有留言: