1. 程式人生 > 實用技巧 >Numpy學習筆記

Numpy學習筆記

  1. Numpy陣列ndarray物件與python裡面列表list最大的區別:

    ndarray裡面只能儲存相同型別的元素

    list物件能儲存不同型別的元素

    1. ndarray.ndim		#維度個數,一維 二維 三維
    
    2. ndarray.shape(n,m)		#陣列的形狀n行m列
    
    3. ndarray.size		#元素的個數
    
    4. ndarray.dtype		#陣列的型別
    
    5. ndarray.itemsize		#每個元素位元組的大小
    
  2. zeros 、 ones 、 empty

    1.
    c = np.zeros((3,4))
    c
    Out[26]: 
    array([[0., 0., 0., 0.],
           [0., 0., 0., 0.],
           [0., 0., 0., 0.]])
           
    2.
    d = np.ones((5,6))    
    d
    Out[28]: 
    array([[1., 1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1., 1.]])
    
    3.
    d.dtype
    Out[31]: dtype('float64')
    
    4.
    np.empty((2,3))
    Out[29]: 
    array([[6.23042070e-307, 1.89146896e-307, 1.37961302e-306],
           [6.23053614e-307, 6.23053954e-307, 1.24611470e-306]])
    
    5.
    np.arange(1,11,2)
    Out[30]: array([1, 3, 5, 7, 9])
    
    
  3. Numpy裡面常用的資料型別

  4. 資料型別的特徵碼

b
array([[1, 2, 3],
       [4, 5, 6]])
       
b.dtype
dtype('int32')

new_b = b.astype(np.string_)
new_b.dtype
dtype('S11')

new_b
array([[b'1', b'2', b'3'],
       [b'4', b'5', b'6']], dtype='|S11')
       
new_b = b.astype(np.float32)

new_b
array([[1., 2., 3.],
       [4., 5., 6.]], dtype=float32)

  1. 陣列運算的三種方式

6.ndarray的索引和切片的使用

​ 一維陣列和python類似

​ 二維陣列:

7.布林索引的基本使用