python numpy库详解

来源:互联网 发布:金牌网吧奖励软件 编辑:程序博客网 时间:2024/06/12 20:57

拓展博客:https://www.cnblogs.com/TensorSense/p/6795995.html

数组的统计函数:
>>>#sum(a,axis=None):计算和>>>#mean(a,axis=None):计算平均值>>>#average(a,axis=None,weights=None):加权平均数>>>#std(a,axis=None):标准差>>>#var(a,axis=None):方差>>> a=np.arange(12).reshape(3,4)>>> aarray([[ 0,  1,  2,  3],       [ 4,  5,  6,  7],       [ 8,  9, 10, 11]])>>> np.sum(a)66>>> np.sum(a,axis=1)array([ 6, 22, 38])>>> np.mean(a)5.5>>> np.average(a,axis=0,weights=[1,2,3])array([ 5.33333333,  6.33333333,  7.33333333,  8.33333333])>>> np.std(a)3.4520525295346629>>> np.std(a)3.4520525295346629>>>#------------------------------------------------------------>>>#min(a),max(a),最小值,最大值>>>#argmin(a),argmax(a),最小值,最大值平铺成一维的下标>>>#unravel_index(index,shape)根据index转换成多维下标>>>#ptp(a)最大值和最小值之差>>>#median(a)中位数>>> aarray([[ 0,  1,  2,  3],       [ 4,  5,  6,  7],       [ 8,  9, 10, 11]])>>> np.max(a)11>>> np.argmax(a)11>>> np.unravel_index(np.argmax(a),a.shape)(2, 3)>>> np.median(a)5.5>>> np.ptp(a)11

数组的遍历:

import numpy as npimport matplotlib.pyplot as pltimport matplotlib#数组元素迭代器a=np.array([[1,2,3],[4,5,6]])for item in a.flat:    print(item,end='')#输出:123456#遍历数组的每一行for row in a:    print(row)#输出:[1 2 3]#      [4 5 6]#遍历数组的每一列,a.T将矩阵转置for col in a.T:    print(col)#输出:[1 4]#      [2 5]#      [3,6]

数组合并

import numpy as npimport matplotlib.pyplot as pltimport matplotliba=np.array([1,1,1])b=np.array([2,2,2])print(a.shape)#给数组a增加一个行维度print(np.shape(a[np.newaxis,:]))print(np.shape(a[:np.newaxis]))print(a)print(a[np.newaxis,:])print(a[:,np.newaxis])#输出:(3,)#(1, 3)#(3,)#[1 1 1]#[[1 1 1]]#[[1]#[1]#[1]]#矩阵合并法一:#verital stack,上下合并c=np.vstack((a,b))print(c)#horizontal stack,左右合并d=np.hstack((a,b))print(d)#输出:[[1 1 1]#[2 2 2]]#[1 1 1 2 2 2]#矩阵合并法2:A=np.array([1,1,1])[:,np.newaxis]B=np.array([2,2,2])[:,np.newaxis]#按照行合并C=np.concatenate((A,B),axis=0)print(C)#输出:#[[1]#[1]#[1]#[2]#[2]#[2]]#按照列合并D=np.concatenate((A,B),axis=1)print(D)#输出:#[[1 2]# [1 2]# [1 2]]


数组分割

import numpy as npa=np.array([[1,2,3],[4,5,6],[7,8,9]])#进行相等的分割,分别为横向分割和纵向分割print(np.split(a,3,axis=0))print(np.split(a,3,axis=1))#输出:#[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]#[array([[1],#       [4],#       [7]]), array([[2],#       [5],#       [8]]), array([[3],#       [6],#       [9]])]#不相等的分割print(np.array_split(a,2))#输出:#[array([[1, 2, 3],#       [4, 5, 6]]), array([[7, 8, 9]])]#相等的分割print(np.vsplit(a,3))print(np.hsplit(a,3))#输出:#[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]#[array([[1],#       [4],#       [7]]), array([[2],#       [5],#       [8]]), array([[3],#       [6],#       [9]])]

numpy的随机数函数

>>>#生成在[0,1)均匀分布的浮点数>>> np.random.rand(3,4)array([[ 0.44374911,  0.60026154,  0.82517151,  0.40989027],       [ 0.05557479,  0.59598147,  0.33718659,  0.46382554],       [ 0.45341894,  0.89121353,  0.19335915,  0.75323229]])>>>#标准正态分布的样本值>>> np.random.randn(3,4)array([[ 1.20569484,  0.59492977, -0.29785485, -0.18304835],       [-0.96979811, -0.55250456, -0.21085342, -0.1876273 ],       [ 1.05674833, -0.10866197,  0.77556511,  1.27915748]])>>>#随机生成确定上下限的样本值,[low,high]>>> np.random.randint(100,200,(3,4))array([[168, 191, 121, 136],       [169, 112, 186, 118],       [109, 176, 194, 178]])>>>#------------------------------------------------------------------->>>#使用相同的随机数种子,可使得产生的随机数相同>>> np.random.seed(1)>>> np.random.rand(1,3)array([[  4.17022005e-01,   7.20324493e-01,   1.14374817e-04]])>>> np.random.rand(1,3)array([[ 0.30233257,  0.14675589,  0.09233859]])>>> np.random.seed(1)>>> np.random.rand(1,3)array([[  4.17022005e-01,   7.20324493e-01,   1.14374817e-04]])>>>#------------------------------------------------------------------->>>#在数组的列内随机排列,改变数组>>> a=np.random.randint(100,200,(3,4))>>> aarray([[175, 105, 179, 164],       [116, 101, 176, 171],       [106, 125, 150, 120]])>>> np.random.shuffle(a)>>> aarray([[175, 105, 179, 164],       [106, 125, 150, 120],       [116, 101, 176, 171]])>>>#在数组的列内随机排列,不改变数组>>> np.random.permutation(a)array([[116, 101, 176, 171],       [106, 125, 150, 120],       [175, 105, 179, 164]])>>> aarray([[175, 105, 179, 164],       [106, 125, 150, 120],       [116, 101, 176, 171]])>>>#从一维数组中以概率p抽取元素,形成size形状新数组,replace表示是否可以重用元素,默认为False>>> b=np.random.randint(1,10,(6,))>>> barray([3, 5, 8, 8, 2, 8])>>>#越大的数,被选取的概率越大>>> np.random.choice(b,(2,1),p=b/np.sum(b),replace=True)array([[8],       [8]])>>>#------------------------------------------------------------------>>>#uniform(low,high,size):产生具有均匀分布的数组[low,high]>>> np.random.uniform(0,10,(3,4))array([[ 7.15970516,  8.02757504,  0.92800809,  5.18152549],       [ 8.65020252,  8.29146907,  8.29603359,  2.73049974],       [ 0.59243201,  6.7052804 ,  5.93065518,  6.71654097]])>>>#normal(loc,scale,size):产生具有正态分布的数组,loc均值,scale标准差>>> np.random.normal(10,5,(3,4))array([[  3.4756938 ,   8.09712482,   6.28186495,   7.81439113],       [  7.86774954,  16.90703652,  10.49185256,   8.15271258],       [  3.63390025,  15.07493401,   2.59470145,   8.56450053]])>>>#poisson(lam,size):产生具有泊松分布的数组,lam随机事件发生率,size形状





原创粉丝点击