numpy基礎知識(二)

數組轉置和軸對換

In [4]: arr = np.random.randn(3,2)

In [5]: arr.T
Out[5]: 
array([[-1.61958612,  0.51404498,  1.27702971],
       [-1.49568441, -0.62306175,  0.27173435]])

In [6]: np.dot(arr.T,arr)
Out[6]: 
array([[4.5181063 , 2.44912079],
       [2.44912079, 2.69911737]])

dot說明

In [7]: arr = np.arange(16).reshape((2,2,4))

In [8]: arr
Out[8]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])

In [9]: arr.transpose((1,0,2))
Out[9]: 
array([[[ 0,  1,  2,  3],
        [ 8,  9, 10, 11]],

       [[ 4,  5,  6,  7],
        [12, 13, 14, 15]]])

transpose須要獲得有一個有軸編號組成的元組才能對這些軸進行轉置數組

In [10]: arr.swapaxes(1,2)
Out[10]: 
array([[[ 0,  4],
        [ 1,  5],
        [ 2,  6],
        [ 3,  7]],

       [[ 8, 12],
        [ 9, 13],
        [10, 14],
        [11, 15]]])

通用函數

In [11]: arr = np.arange(10)

In [12]: arr
Out[12]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [13]: np.sqrt(arr)
Out[13]: 
array([0.        , 1.        , 1.41421356, 1.73205081, 2.        ,
       2.23606798, 2.44948974, 2.64575131, 2.82842712, 3.        ])

In [14]: np.exp(arr)
Out[14]: 
array([1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01,
       5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03,
       2.98095799e+03, 8.10308393e+03])

還有許多函數,你們能夠去官網查看dom

利用數組進行數據處理

In [16]: point
Out[16]: array([1, 2, 3, 4])

In [17]: xs,ys = np.meshgrid(point,point)

In [18]: xs
Out[18]: 
array([[1, 2, 3, 4],
       [1, 2, 3, 4],
       [1, 2, 3, 4],
       [1, 2, 3, 4]])

In [19]: ys
Out[19]: 
array([[1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [4, 4, 4, 4]])
In [20]: z = np.sqrt(xs**2+ys**2)

In [21]: z
Out[21]: 
array([[1.41421356, 2.23606798, 3.16227766, 4.12310563],
       [2.23606798, 2.82842712, 3.60555128, 4.47213595],
       [3.16227766, 3.60555128, 4.24264069, 5.        ],
       [4.12310563, 4.47213595, 5.        , 5.65685425]])

條件邏輯表述數組計算

In [23]: xarr = np.array([1.1,1.2,1.3,1.4,1.5])

In [24]: yarr = np.array([2.1,2.2,2.3,2.4,2.5])

In [25]: cond = np.array([True,False,True,True,False])

In [26]: result = [(x if c else y) for x,y,c in zip (xarr,yarr,cond)]

In [27]: result
Out[27]: [1.1, 2.2, 1.3, 1.4, 2.5]
In [28]: result = np.where(cond,xarr,yarr)

In [29]: result
Out[29]: array([1.1, 2.2, 1.3, 1.4, 2.5])

numpy.where 能夠用 x if condition esle y,這種方式處理的速度不是很快;沒法用於多維數組。np.where比較簡潔;np.where的條件是與,或,非邏輯計算函數

數學和統計方法

In [1]: arr = np.random.randn(5,4)

In [2]: arr
Out[2]: 
array([[-0.96566616, -0.69396553, -1.44922124,  1.25770641],
       [-0.60834685, -0.1550649 ,  0.12900002, -0.51405016],
       [ 0.69804581, -0.5674765 , -0.49269238, -1.09732552],
       [ 2.3316344 ,  0.31030837,  0.06029882,  0.44382971],
       [ 0.29524057, -1.13612158,  0.91402251,  2.34230133]])
In [4]: arr.mean()
Out[4]: 0.055122857216706934

In [5]: np.mean(arr)
Out[5]: 0.055122857216706934

In [6]: arr.mean(axis=1)
Out[6]: array([-0.46278663, -0.28711547, -0.36486215,  0.78651783,  0.60386071])

In [7]: arr.sum(0)
Out[7]: array([ 1.75090777, -2.24232013, -0.83859227,  2.43246177])

mean和sum這類函數能夠接受喲個axix參數,最終能夠獲得一個少一維的數組測試

In [9]: arr = np.array([[0,1,2],[3,4,5],[6,7,8]])

In [10]: arr
Out[10]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

In [11]: arr.cumsum()
Out[11]: array([ 0,  1,  3,  6, 10, 15, 21, 28, 36])

In [12]: arr.cumsum(0)
Out[12]: 
array([[ 0,  1,  2],
       [ 3,  5,  7],
       [ 9, 12, 15]])

cumsum計算全部元素的累計和,不加參數生成一維數組的累計值,0表明向下累計code

In [13]: arr.cumprod(1)
Out[13]: 
array([[  0,   0,   0],
       [  3,  12,  60],
       [  6,  42, 336]])

In [14]: arr.cumprod(0)
Out[14]: 
array([[ 0,  1,  2],
       [ 0,  4, 10],
       [ 0, 28, 80]])

cumprod是計算全部元素的累計積 ##用於布爾類型數組的方法對象

In [21]: arr = np.random.randn(100)

In [22]: (arr>0).sum()
Out[22]: 52
In [23]: bools = np.array([False,False,True,False])
In [25]: bools.any()
Out[25]: True

In [26]: bools.all()
Out[26]: False

any只要有一個符合就返回ture,all必須都符合才返回true排序

數組排序

In [28]: arr = np.random.randn(8)

In [29]: arr
Out[29]: 
array([-1.08477468, -1.05278807, -2.27882676,  0.08437624,  0.50895675,
        0.50632641,  0.80177614, -0.56485938])

In [30]: arr.sort()

In [31]: arr
Out[31]: 
array([-2.27882676, -1.08477468, -1.05278807, -0.56485938,  0.08437624,
        0.50632641,  0.50895675,  0.80177614])
In [32]: arr = np.random.randn(5,3)

In [33]: arr
Out[33]: 
array([[-0.52836361, -0.82969488,  0.89826385],
       [-0.13202188, -0.38917723, -0.551128  ],
       [ 0.35644031,  0.82714182,  0.45963875],
       [-1.17120769, -0.42217203,  0.14108125],
       [ 0.95581486, -0.77846096,  1.74493614]])

In [34]: arr.sort()

In [35]: arr
Out[35]: 
array([[-0.82969488, -0.52836361,  0.89826385],
       [-0.551128  , -0.38917723, -0.13202188],
       [ 0.35644031,  0.45963875,  0.82714182],
       [-1.17120769, -0.42217203,  0.14108125],
       [-0.77846096,  0.95581486,  1.74493614]])

In [36]: arr.sort(1)

In [37]: arr
Out[37]: 
array([[-0.82969488, -0.52836361,  0.89826385],
       [-0.551128  , -0.38917723, -0.13202188],
       [ 0.35644031,  0.45963875,  0.82714182],
       [-1.17120769, -0.42217203,  0.14108125],
       [-0.77846096,  0.95581486,  1.74493614]])

多維數組能夠在任何一個軸上排序ip

In [41]: large_arr = np.random.randn(5)

In [42]: large_arr
Out[42]: array([-1.69314145, -0.73522386, -1.42118609,  0.30122406,  0.5861573 ])

In [43]: large_arr.sort()

In [44]: large_arr[int(0.05*len(large_arr))]
Out[44]: -1.693141452075213

利用排序求分位數內存

數組集合的去重

In [45]: names = np.array(['Bob','Joe','Will','Bob','Will','Joe','Joe']
    ...: )

In [46]: names
Out[46]: array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], dtype='|S4')

In [47]: np.unique(names)
Out[47]: array(['Bob', 'Joe', 'Will'], dtype='|S4')
In [48]: values = np.array([6,0,0,3,2,5,6])

In [49]: np.in1d(values,[2,3,6])
Out[49]: array([ True, False, False,  True,  True, False,  True])

測試一個數組是否在在另外一個數組裏,並返回一個布爾數組,數組集合還有差集並集等數學

將數組已二進制形式保存到內存

In [50]: arr = np.arange(10)

In [51]: np.save('test',arr)

In [52]: np.load('test')
In [53]: np.load('test.npy')
Out[53]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

保存文件的時候會自動加載文件擴展名

In [54]: np.savez('two',a=arr,b=arr)

In [56]: arch = np.load('two.npz')

In [57]: arch['a']
Out[57]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [58]: arch['b']
Out[58]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

能夠把多個數組保存成壓縮文件,壓縮文件的擴展名是npz,加載時會獲得相似字典的對象

In [1]: np.loadtxt('test.txt',delimiter=',')
Out[1]: 
array([[ 1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.],
       [ 9., 19., 19., 19.]])

In [2]: arr = np.loadtxt('test.txt',delimiter=',')

In [3]: arr
Out[3]: 
array([[ 1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.],
       [ 9., 19., 19., 19.]])

numpy也能夠加載文本文件,也能夠保存文本文件,np.savetxt()

線性代數

In [1]: x = np.array([[1,2,3],[4,5,6]])

In [2]: x
Out[2]: 
array([[1, 2, 3],
       [4, 5, 6]])

In [3]: y = np.array([[6,23],[-1,7],[8,9]])

In [4]: y
Out[4]: 
array([[ 6, 23],
       [-1,  7],
       [ 8,  9]])

In [5]: x.dot(y)
Out[5]: 
array([[ 28,  64],
       [ 67, 181]])

矩陣的乘法 x.dot(y),前面已經講過

In [6]: np.ones(3)
Out[6]: array([1., 1., 1.])

In [7]: np.dot(x,np.ones(3))
Out[7]: array([ 6., 15.])

一個二維數組跟一個一維數組的矩陣點積運算以後獲得一個一維數組

In [1]: arr = np.array([[3,-1,4],[1,0,0],[2,1,-5]])

In [2]: arr
Out[2]: 
array([[ 3, -1,  4],
       [ 1,  0,  0],
       [ 2,  1, -5]])
In [3]: arr.T.dot(arr)
Out[3]: 
array([[14, -1,  2],
       [-1,  2, -9],
       [ 2, -9, 41]])

In [4]: mat = arr.T.dot(arr)

In [5]: mat
Out[5]: 
array([[14, -1,  2],
       [-1,  2, -9],
       [ 2, -9, 41]])
In [8]: inv(mat)
Out[8]: 
array([[  1.,  23.,   5.],
       [ 23., 570., 124.],
       [  5., 124.,  27.]])

In [9]: q,r = qr(mat)

In [10]: r
Out[10]: 
array([[-1.41774469e+01,  2.39817509e+00, -8.39361283e+00],
       [ 0.00000000e+00, -8.95816701e+00,  4.11770227e+01],
       [ 0.00000000e+00,  0.00000000e+00,  7.87377166e-03]])

inv計算方陣的逆;qr計算qr分解

相關文章
相關標籤/搜索