numpy常用操作笔记
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#encoding=utf-8 import numpy as np lst=[[1,2,3],[6,7,8]] print (type(lst)) np_lst=np.array(lst) print (type(np_lst)) np_lst=np.array(lst,dtype=np.float) print np_lst print (np_lst.shape)#几行几列 print (np_lst.ndim)#维度 print (np_lst.dtype)#类型 print (np_lst.itemsize)#所占字节 print (np_lst.size)#大小 print (np.zeros([2,4]))#定义数组 print (np.ones([3,2])) print (np.random.rand())#随机数 print (np.random.rand(2,4)) print (np.random.randint(1,10,3))#生成1-10随机的三个整数 print (np.random.choice([1,2,3,5,10,100,666]))#选择指定数字 print (np.random.beta(1,10,100))#beta分布 print (np.arange(1,11).reshape([2,-1]))#生成1-10的等差数列并显示为2行5列 list=np.array([[[1,2,3], [4,5,6]], [[7,8,9], [10,11,12]] ]) print (list.sum(axis=0))#axis指定一个维度求和 axis越大越深入 =0对最外层求和 1+7 2+8 3+9 4+10 5+11 6+12 print (list.sum(axis=1))#内层求和 1+4 2+5 3+6 7+10 8+11 9+12 print (list.sum(axis=2))#再深入一层 1+2+3 4+5+6 7+8+9 10+11+12 print (list.max(axis=1))#求最大值 print (list.min(axis=2))#求最小值 lst1=np.array([1,2,3]) lst2=np.array([10,11,12]) print (np.concatenate((lst1,lst2),axis=0))#追加到一起 print (np.vstack((lst1,lst2)))#上下追加到一起 两行 print (np.hstack((lst1,lst2)))#合在一起 print (np.split(lst1,3))#分成3组 |
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<type 'list'> <type 'numpy.ndarray'> [[ 1. 2. 3.] [ 6. 7. 8.]] (2, 3) 2 float64 8 6 [[ 0. 0. 0. 0.] [ 0. 0. 0. 0.]] [[ 1. 1.] [ 1. 1.] [ 1. 1.]] 0.0790300286594 [[ 0.40570725 0.40072322 0.75697423 0.45873189] [ 0.84684053 0.80063956 0.02231413 0.57768334]] [4 5 8] 10 [ 1.98939407e-02 1.61285413e-02 1.18716706e-02 8.10961771e-02 1.63533368e-01 1.44668421e-01 5.11693434e-02 6.59941834e-02 2.23286959e-02 2.15972016e-01 8.63734013e-02 8.06557908e-02 1.22880147e-01 1.09686347e-02 6.54980124e-02 1.98280109e-03 4.08960959e-05 4.99724974e-02 4.46814021e-02 1.38706779e-01 1.24582838e-01 5.32977093e-02 1.01838024e-01 2.46115301e-01 6.15260218e-03 1.08537025e-01 1.69927681e-02 1.03185373e-01 2.02785750e-02 4.68360049e-02 2.35353598e-02 1.53166093e-01 7.08847154e-02 1.43549143e-02 9.55657510e-02 7.62737256e-02 5.94670869e-03 5.71465328e-03 3.98867568e-02 6.37016822e-02 9.69537649e-02 3.52414465e-02 5.60588196e-03 1.45640846e-01 3.06005428e-02 7.66577878e-02 1.98901419e-02 1.04611224e-01 1.45486287e-02 4.63747783e-03 1.00559794e-01 4.14864166e-02 6.97617286e-02 1.21598675e-02 5.18675502e-02 2.67329995e-01 2.71693357e-01 7.61304550e-02 7.19672528e-02 6.30126936e-03 3.79026503e-02 2.35909430e-01 5.86051618e-02 2.11428839e-01 1.35133707e-01 6.62907241e-02 1.38296699e-01 2.88886522e-01 2.40987292e-02 1.76062934e-02 1.90230382e-01 2.09219429e-01 3.40778112e-02 4.66014797e-02 5.47428038e-03 2.20511410e-02 3.19067152e-02 2.47324231e-02 3.66452719e-02 2.97249683e-02 2.26196050e-01 1.59556704e-02 5.66573658e-04 1.44624707e-01 8.91173413e-02 4.21186597e-02 3.77105859e-03 2.68561111e-01 1.88337068e-02 1.12529504e-01 1.14325549e-01 6.22131659e-04 4.74532828e-01 1.57684391e-01 2.60435883e-02 1.53169399e-03 2.44662204e-02 2.86626061e-02 1.47198619e-01 4.32705246e-02] [[ 1 2 3 4 5] [ 6 7 8 9 10]] [[ 8 10 12] [14 16 18]] [[ 5 7 9] [17 19 21]] [[ 6 15] [24 33]] [[ 4 5 6] [10 11 12]] [[ 1 4] [ 7 10]] [ 1 2 3 10 11 12] [[ 1 2 3] [10 11 12]] [ 1 2 3 10 11 12] [array([1]), array([2]), array([3])] |
近期评论