Numpy练习

Numpy的“增”操作

import numpy as np
### 新建数组 ndarray()
arr1 = np.array([1,2,3,4])
arr1
array([1, 2, 3, 4])
arr2=np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]])
arr2
array([[ 1,  2,  3,  4],
       [ 4,  5,  6,  7],
       [ 7,  8,  9, 10]])
np.linspace(0,1,12)
array([0.        , 0.09090909, 0.18181818, 0.27272727, 0.36363636,
       0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,
       0.90909091, 1.        ])
np.logspace(0,2,20)
array([  1.        ,   1.27427499,   1.62377674,   2.06913808,
         2.6366509 ,   3.35981829,   4.2813324 ,   5.45559478,
         6.95192796,   8.8586679 ,  11.28837892,  14.38449888,
        18.32980711,  23.35721469,  29.76351442,  37.92690191,
        48.32930239,  61.58482111,  78.47599704, 100.        ])
np.zeros((2,3))
array([[0., 0., 0.],
       [0., 0., 0.]])
np.eye(3)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
np.diag([1,2,3,4])
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]])
numpy的“查”操作
### 数组属性的查看
arr2.shape
(3, 4)
arr2.ndim
2
print(arr2.size)
print(arr2.dtype)
print(arr2.itemsize)
12
int32
4
np.float64(56)
56.0
np.int8(42.00)
42
np.bool(0)
False
np.float(True)
1.0
np.float(False)
0.0
数组的索引查找
arr2[0][3]
4
arr2[1][::2]
array([4, 6])
arr2[2][::-1]
array([10,  9,  8,  7])
arr2[0,0:3]
array([1, 2, 3])

数组的“改”操作

arr2.shape
(3, 4)
arr2
array([[ 1,  2,  3,  4],
       [ 4,  5,  6,  7],
       [ 7,  8,  9, 10]])
arr2.shape=4,3
arr2
array([[ 1,  2,  3],
       [ 4,  4,  5],
       [ 6,  7,  7],
       [ 8,  9, 10]])
arr1.reshape(2,2)
array([[1, 2],
       [3, 4]])
arr=np.arange(24).reshape(6,4)
arr
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])
arr.ravel()
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
arr.flatten()
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
arr.flatten('F')
array([ 0,  4,  8, 12, 16, 20,  1,  5,  9, 13, 17, 21,  2,  6, 10, 14, 18,
       22,  3,  7, 11, 15, 19, 23])

组合数组

arr3=np.array([5,6,7,8])
np.hstack((arr1,arr3))
array([1, 2, 3, 4, 5, 6, 7, 8])
np.vstack((arr1,arr3))
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
np.concatenate((arr1,arr3),axis=0)
array([1, 2, 3, 4, 5, 6, 7, 8])

切割数组

np.hsplit(arr3,2)
[array([5, 6]), array([7, 8])]
np.vsplit(arr2,2)
[array([[1, 2, 3],
        [4, 4, 5]]), array([[ 6,  7,  7],
        [ 8,  9, 10]])]
np.split(arr2,2,axis=0)
[array([[1, 2, 3],
        [4, 4, 5]]), array([[ 6,  7,  7],
        [ 8,  9, 10]])]
numpy生成数组
np.random.random(100)
array([0.71053606, 0.95203536, 0.76331541, 0.55493895, 0.39102462,
       0.13562036, 0.54606128, 0.39459205, 0.70617919, 0.17562621,
       0.28501892, 0.39278865, 0.11813006, 0.20269566, 0.63378109,
       0.59720777, 0.61643689, 0.97485196, 0.4834031 , 0.96336799,
       0.26706305, 0.00403679, 0.99827773, 0.07561853, 0.95340374,
       0.5953736 , 0.51921219, 0.12503096, 0.26468936, 0.40264103,
       0.22725614, 0.59584873, 0.49822961, 0.30558757, 0.97078809,
       0.82387846, 0.00598697, 0.2579057 , 0.33768335, 0.20705999,
       0.38711076, 0.88568059, 0.34330894, 0.00459766, 0.66069836,
       0.01466494, 0.72991822, 0.46446075, 0.15236435, 0.03403856,
       0.50002317, 0.14583122, 0.67819492, 0.46156311, 0.58614041,
       0.22683131, 0.78510308, 0.49240927, 0.79566184, 0.49761592,
       0.07211559, 0.02137084, 0.1495692 , 0.75535465, 0.69036627,
       0.49758816, 0.92190045, 0.97432414, 0.06186802, 0.83891532,
       0.03821025, 0.51286209, 0.55923737, 0.16736189, 0.18940051,
       0.03945974, 0.48420069, 0.16900747, 0.67088112, 0.6460478 ,
       0.55075362, 0.58272657, 0.78415799, 0.90095223, 0.13459997,
       0.06584495, 0.01064911, 0.28361183, 0.22340501, 0.53096492,
       0.6925489 , 0.38313092, 0.91432115, 0.12192859, 0.95725144,
       0.70806747, 0.29479204, 0.43968849, 0.82318264, 0.23264036])
np.random.randn(10,5)
array([[ 0.48933406,  1.34939347, -1.71554657, -0.42422225, -0.8157484 ],
       [ 0.0328522 ,  0.93455817,  0.48219509,  0.01502152, -1.42161913],
       [ 1.55477901,  1.02162058,  0.60510017, -0.41498954, -1.1197109 ],
       [-2.09555644,  0.39974874,  0.91302354, -0.91962849, -1.94280772],
       [-1.27056391,  0.2657837 ,  1.09529382,  0.28615002, -1.49412104],
       [-0.19117315,  0.44268055, -1.2308667 ,  0.90402837, -1.24903965],
       [ 0.09506805,  1.74429733,  1.22459314, -0.27716015, -0.54156692],
       [-0.2214944 , -0.10732188, -0.40986407, -1.17789644,  1.24715783],
       [ 0.27407159,  0.58844992, -0.08965965,  0.28551202,  0.66932852],
       [ 0.16601714, -2.00387632,  0.95165132,  0.36905729, -0.35935348]])
np.random.randint(2,10,size=[2,5])
array([[8, 7, 2, 6, 4],
       [6, 4, 9, 2, 6]])

Numpy矩阵操作

matr1 = np.mat("1 2 3;4 5 6;7 8 9")
matr1
matrix([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
matr2=np.matrix([[1,2,3],[4,5,6],[7,8,9]])
matr1*3
matrix([[ 3,  6,  9],
        [12, 15, 18],
        [21, 24, 27]])
matr1+matr2
matrix([[ 2,  4,  6],
        [ 8, 10, 12],
        [14, 16, 18]])
matr1*matr2
matrix([[ 30,  36,  42],
        [ 66,  81,  96],
        [102, 126, 150]])
np.dot(matr1,matr2)
matrix([[ 30,  36,  42],
        [ 66,  81,  96],
        [102, 126, 150]])
matr1.I
matrix([[ 3.15251974e+15, -6.30503948e+15,  3.15251974e+15],
        [-6.30503948e+15,  1.26100790e+16, -6.30503948e+15],
        [ 3.15251974e+15, -6.30503948e+15,  3.15251974e+15]])
matr1.H
matrix([[1, 4, 7],
        [2, 5, 8],
        [3, 6, 9]])
import numpy as np
x1=np.random.rand(100,3)
x1
array([[0.97158058, 0.71546246, 0.35065274],
       [0.99461343, 0.65317385, 0.22432578],
       [0.13769643, 0.82740808, 0.58087198],
       [0.94598307, 0.96531307, 0.89692282],
       [0.70763597, 0.51129917, 0.84040091],
       [0.10204592, 0.80324219, 0.12584154],
       [0.21747618, 0.36548934, 0.89816395],
       [0.30690007, 0.1868626 , 0.1997995 ],
       [0.6771123 , 0.32704448, 0.91191128],
       [0.70178207, 0.0087312 , 0.87587776],
       [0.96589778, 0.4333469 , 0.27195302],
       [0.44486127, 0.32201163, 0.94192211],
       [0.88343357, 0.07597127, 0.90386532],
       [0.1467375 , 0.08479162, 0.25153134],
       [0.95201269, 0.55359863, 0.16972768],
       [0.76117587, 0.44538496, 0.27036177],
       [0.49250433, 0.5134123 , 0.6816104 ],
       [0.53659134, 0.02347996, 0.28195867],
       [0.33097538, 0.64287776, 0.11290429],
       [0.90780981, 0.39064046, 0.41910695],
       [0.94959557, 0.60655436, 0.53540838],
       [0.95428854, 0.82035735, 0.50421943],
       [0.84721257, 0.04983021, 0.85477232],
       [0.65419275, 0.51037702, 0.87459355],
       [0.37506153, 0.05997114, 0.08890829],
       [0.21843131, 0.98888774, 0.32296471],
       [0.66053877, 0.96479309, 0.20314874],
       [0.41476688, 0.31176532, 0.13226863],
       [0.71560543, 0.45162106, 0.57990222],
       [0.24344961, 0.67942329, 0.87964684],
       [0.84871632, 0.76069265, 0.12729351],
       [0.97408102, 0.17375491, 0.95077516],
       [0.4111124 , 0.99828734, 0.45460105],
       [0.32461037, 0.61830415, 0.51635167],
       [0.5050527 , 0.90352111, 0.50796347],
       [0.93215827, 0.41523027, 0.8263368 ],
       [0.96985874, 0.66233668, 0.51694334],
       [0.06758698, 0.38447855, 0.15083614],
       [0.38513143, 0.85517932, 0.36960819],
       [0.68328847, 0.79438194, 0.44769912],
       [0.669942  , 0.96925943, 0.61071944],
       [0.41286287, 0.36833979, 0.54206367],
       [0.16096248, 0.02904524, 0.55845268],
       [0.41351926, 0.2407517 , 0.26832979],
       [0.67428215, 0.11360686, 0.74858401],
       [0.24887591, 0.16531982, 0.16326466],
       [0.33278797, 0.82350564, 0.14531241],
       [0.02283463, 0.61280686, 0.25496113],
       [0.22302049, 0.10181989, 0.77576486],
       [0.16070374, 0.51282751, 0.91831646],
       [0.41391583, 0.58406441, 0.54980505],
       [0.50663413, 0.06323077, 0.05663689],
       [0.86477219, 0.83563757, 0.34915442],
       [0.09715681, 0.59949765, 0.6704383 ],
       [0.86031254, 0.55049062, 0.96787983],
       [0.70113952, 0.03336649, 0.38849933],
       [0.42431209, 0.27534732, 0.97467164],
       [0.93172815, 0.34676018, 0.24674162],
       [0.08274272, 0.97651887, 0.2746486 ],
       [0.41576864, 0.54514886, 0.81627334],
       [0.55992736, 0.43106022, 0.36313656],
       [0.55366076, 0.25345039, 0.10012318],
       [0.14976176, 0.54903588, 0.47997538],
       [0.16317977, 0.22750322, 0.0917339 ],
       [0.03170418, 0.89231934, 0.43024014],
       [0.34608548, 0.02697052, 0.60390517],
       [0.67864169, 0.80551139, 0.59295622],
       [0.95434585, 0.02702092, 0.82012447],
       [0.94984259, 0.94039867, 0.26133184],
       [0.54054787, 0.89403357, 0.65542275],
       [0.15858546, 0.5598103 , 0.18917386],
       [0.11428847, 0.05249719, 0.35719608],
       [0.34194511, 0.12083727, 0.5031263 ],
       [0.2290274 , 0.52568162, 0.4777548 ],
       [0.77522057, 0.25894931, 0.62537943],
       [0.68969705, 0.31017669, 0.61101983],
       [0.86987636, 0.00938243, 0.30486191],
       [0.00131263, 0.99794696, 0.95355472],
       [0.90380482, 0.87058469, 0.73862517],
       [0.29297134, 0.83085578, 0.50207158],
       [0.47359425, 0.25749568, 0.01112421],
       [0.66338508, 0.98478975, 0.5516798 ],
       [0.58126997, 0.934867  , 0.42046312],
       [0.74807796, 0.705476  , 0.86395755],
       [0.80850248, 0.75865312, 0.14638065],
       [0.54480326, 0.62808002, 0.21312425],
       [0.24863277, 0.94402769, 0.02897317],
       [0.63715931, 0.9606411 , 0.14760444],
       [0.42101944, 0.45494445, 0.19071282],
       [0.41318068, 0.04799136, 0.39219205],
       [0.3610749 , 0.44599172, 0.09874665],
       [0.17298644, 0.97527637, 0.08646838],
       [0.07179254, 0.10645828, 0.01296037],
       [0.72215522, 0.21069213, 0.87181776],
       [0.73073255, 0.54531639, 0.42244929],
       [0.64828662, 0.53662285, 0.46583699],
       [0.61152711, 0.16450621, 0.97979907],
       [0.13721841, 0.4365581 , 0.1081302 ],
       [0.50779956, 0.57847172, 0.86904726],
       [0.82930967, 0.71705229, 0.09228038]])

Numpy统计分析

arr4=np.array([[3,7],[9,1]])
arr4
array([[3, 7],
       [9, 1]])
arr4.sort()
arr4
array([[3, 7],
       [1, 9]])
arr4.sort(axis=0)
arr4
array([[1, 7],
       [3, 9]])
arr5=np.array([4,2,3,1])
arr5.argsort()
array([3, 1, 2, 0], dtype=int64)
nm=('a','b','c','c')
dv=('z','x','y','o')

ind=np.lexsort((dv,nm))
print([nm[i]+","+dv[i] for i in ind])
['a,z', 'b,x', 'c,o', 'c,y']
arr6=np.array([1,1,2,2,3,3])
np.unique(arr6)
array([1, 2, 3])
np.tile(arr6,3)
array([1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3])
np.repeat(arr6,3,axis=0)
array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3])
arr2.sum()
66
arr2.sum(axis=0)
array([19, 22, 25])
arr2.sum(axis=1)
array([ 6, 13, 20, 27])
arr6.argmax()
4
arr7=np.array([0.1,0.9])
arr7.argmax()
1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值