Numpy
將字符型數據轉為datetime
import numpy as np f = np.array(['2018','2019-01-01','2019-01-02 01:01:01']) # 把f數組的元素類型改為日期類型 g = f.astype('M8[D]') # M8[Y] M8[M] M8[D] print(g) # 時間戳(將日期轉為數) 上面g的單位不同,這邊的數值也不同 # g中的值距離1970年總共有多少天 h = g.astype('int32') print(h) print(h[2] - h[1])
生成ndarray數組
- np.random.random((2,2))
- np.ones((3,4))
- np.zeros((2,2), dtype='int32')
- np.arange(1,10)
- np.linspace(0,2,10)
- np.eye(3)
- np.full((3,3),7)
np.random.random((2,2)) Out[2]: array([[ 0.61705652, 0.48264423], [ 0.69303143, 0.35004567]]) np.ones((3,4)) Out[3]: array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]]) np.zeros((2,2), dtype='int32') Out[4]: array([[0, 0], [0, 0]]) np.arange(1,10) Out[5]: array([1, 2, 3, 4, 5, 6, 7, 8, 9]) np.linspace(0,2,10) Out[6]: array([ 0. , 0.22222222, 0.44444444, 0.66666667, 0.88888889, 1.11111111, 1.33333333, 1.55555556, 1.77777778, 2. ]) np.eye(3) Out[7]: array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]) np.full((3,3),7) Out[8]: array([[7, 7, 7], [7, 7, 7], [7, 7, 7]])
Numpy 的random模塊
# 使用numpy.random的normal函數生成符合二項分布的隨機數 n = 100 # 172:期望值 # 20:標准差 # n:數字生成數量 x= np.random.normal(172, 20, n) y= np.random.normal(60, 10, n)
ndarray數組對象的維度操作
視圖變維:array.reshape() array.ravel()
- ravel() 是扁平化但是不復制,公用一個對象
- flatten() 是扁平化同時復制,會生成一個新對象並且返回
import numpy as np a = np.arange(1,9) # 視圖變維使用的還是原始數組中的數據,如果修改了原始數組中的數據,那么新數組讀到的數據也會發生變化。 b = a.reshape((2,4)) print(a,b) a[0] =999 print(b) c = b.ravel() print(c)
復制變維(數據獨立):flatten()
# 測試flatten d=b.flatten().reshape((2,4)) d[0] = 110 print(b) print(d)
就地變維:直接修改數組維度,不返回新數組 resize() shape
d.resize(2,2,2) d.shape=(2,4) print(d)
ndarray數組的切片操作
# 數組的切片與列表的切片參數類似 # 步長為正:默認從前往后切 # 步長為負:默認從后往前切 array[起始位置:終止位置:步長] a = np.arange(1,10) # array([1, 2, 3, 4, 5, 6, 7, 8, 9]) a.resize(3,3) # array([[1, 2, 3], # [4, 5, 6], # [7, 8, 9]]) a[1:, :] # 第2行到最后一行,所有列
ndarray數組的掩碼操作
a = np.array([1,2,3,4,5,6,7,8]) f = np.array([True, False, True, False,False, True, False, True]) a[f] Out[35]: array([1, 3, 6, 8]) # 現在有數組的1-100,我們現在要拿到數組中3的倍數或7的倍數 flag_a = a%3==0 flag_b = a%7==0 flag_a Out[43]: array([False, False, True, False, False, True, False, False, True, False, False, False, False, False, ... False, False, False, False, False, True, False, False, True, False, False, True, False], dtype=bool) flag = np.any([flag_a, flag_b], axis=0) a[flag] Out[45]: array([ 3, 6, 7, 9, 12, 14, 15, 18, 21, 24, 27, 28, 30, 33, 35, 36, 39, 42, 45, 48, 49, 51, 54, 56, 57, 60, 63, 66, 69, 70, 72, 75, 77, 78, 81, 84, 87, 90, 91, 93, 96, 98, 99])
多維數組的組合和拆分
垂直方向的操作:vstack() vsplit()
a = np.arange(1,7).reshape(2,3) b = np.arange(7,13).reshape(2,3) a Out[54]: array([[1, 2, 3], [4, 5, 6]]) b Out[55]: array([[ 7, 8, 9], [10, 11, 12]]) c = np.vstack((a,b)) c Out[57]: array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]]) a,b = np.vsplit(c, 2)
水平方向的操作:hstack() hsplit()
d = np.hstack((a,b)) a,b = np.hsplit(d, 2)
深度方向的操作:dstack() dsplit() 二維數組深度操作會變為三維數組,最后拆分也是三維數組
a Out[59]: array([[1, 2, 3], [4, 5, 6]]) b Out[60]: array([[ 7, 8, 9], [10, 11, 12]]) e = np.dstack((a,b)) e Out[62]: array([[[ 1, 7], [ 2, 8], [ 3, 9]], [[ 4, 10], [ 5, 11], [ 6, 12]]]) a,b = np.dsplit(e,2) a Out[64]: array([[[1], [2], [3]], [[4], [5], [6]]])
多維數組組合與拆分的相關函數
# 根據axis所指定的軸向(0,1,2)進行多維數組的組合 # 如果待組合的兩個數組都是二維數組 # axis=0:垂直方向 # axis=1:水平方向 # 如果待組合的兩個數組都是三維數組 # axis=0:垂直方向 # axis=1:水平方向 # axis=2:深度方向 np.concatenate((a,b), axis=0) # 通過axis給定的軸向和拆分的份數對c進行拆分 np.split(c,2,axis=0)
長度不等的兩個數組的組合操作
np.pad(ary, # 原始數組 pad_width=(0,1), # 補全方式(頭部補0個,尾部補1個) mode='constant', # 設置補全模式 constant_values=-1) # 設置補全的默認值為-1 a = np.arange(1,5) a Out[66]: array([1, 2, 3, 4]) # 返回一個新數組 np.pad(a, pad_width=(0,3),mode='constant',constant_values=-1) Out[67]: array([ 1, 2, 3, 4, -1, -1, -1])
簡單的一維數組的組合方案
a = np.arange(1,10) b = np.arange(11,20) # 垂直方向疊加 np.row_stack((a,b)) # 水平方向疊加 np.column_stack((a,b))
Numpy數組的其他屬性
1.shape 維度
2.dtype 元素類型
3.size 元素的個數
4.ndim 維度
5.itemsize 元素字節數
6.nbytes 數組的總字節數
7.real 復數數組的實部
8.imag 復數數組的虛部
9.T 數組對象的轉置視圖
10.flat 返回數組的扁平迭代器
a = np.arange(1,28) a.resize(3,3,3) a.size Out[75]: 27 len(a) Out[76]: 3 a.ndim Out[77]: 3 a.shape Out[78]: (3, 3, 3) a.dtype Out[79]: dtype('int32') a.dtype.name Out[81]: 'int32' # ndarray數組的扁平迭代器 for i in a.flat: print(i) [e for e in a.flat]
