mumpy常用函数

numpy.array(list(1,2,3,4)) 

#将一个list类型/tupe类型数据转换为一个array数组对象

#默认所有的数据类型都是相同,若传进来的参数类型不同,则遵循以下优先级:
    
    str > float > int
ndarray对象.dtype

#查看array对象中的数据类型
data = numpy.array(1,2,"1")
print(data.dtype)
# dtype('<U11')

#可以改变数据的类型,不过对于数据类型的转换必须是优先级大的转换为优先级小的,不然会报错
data.dtype = 'int' 


ndarray对象.shape

#查看array对象的形状

data.shape

#(3,)



ndarray对象.reshape(shape)

#返回一个新的以shape为构造的array对象,前提是转换前后的数据个数必须保持一致,不然会报错

data = np.full((3,4))

array([[3, 3, 3, 3],
       [3, 3, 3, 3],
       [3, 3, 3, 3]])

data2 = data.reshape((6,2))

array([[3, 3],
       [3, 3],
       [3, 3],
       [3, 3],
       [3, 3],
       [3, 3]])


numpy.ones(shape,dtype=None,order='C')

#返回一个被 1 填充的数组对象

np.ones((3,3,1),dtype='int') #返回一个3行3列的数组,数据个数为1个的全1的数组 

array([[[1],
        [1],
        [1]],

       [[1],
        [1],
        [1]],

       [[1],
        [1],
        [1]]])



numpy.zeros(shape,dtype="float",order='C')

#返回一个被 0 填充的数组对象,数据类型为“float”

np.zeros((3,3,1))   

array([[[0.],
        [0.],
        [0.]],

       [[0.],
        [0.],
        [0.]],

       [[0.],
        [0.],
        [0.]]])



np.full(shape, fill_value, dtype=None, order='C')

#返回一个被 fill_value 填充的以shape为形状的数组

np.full((3,3,2),fill_value = 2) #返回以2为填充内容的3行3列数据个数为2个的数组对象

array([[[2, 2],
        [2, 2],
        [2, 2]],

       [[2, 2],
        [2, 2],
        [2, 2]],

       [[2, 2],
        [2, 2],
        [2, 2]]])



np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

#linspace  --> line + space 线性 空间

#返回一个给定起始点和终点的array,其中默认的长度为50,当endpoint=False时,不包括终点数

np.linspace(0,9,num = 10,endpoint=False,dtype='int')
#array([0, 0, 1, 2, 3, 4, 5, 6, 7, 8])

np.linspace(0,11,num = 10,endpoint=True,dtype='int')
#array([ 0,  1,  2,  3,  4,  6,  7,  8,  9, 11])

np.linspace(0,10,num = 11,endpoint=True,dtype='int')
#array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])



np.arange([start, ]stop, [step, ]dtype=None)

#返回一个在给定始终点范围内,并以step为递增数的一个数组对象
#若不指定start,则默认从0开始,
#若不指定step,则默认递增数为1

np.arange(0,100,step=10)
#array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])

np.arange(-10,10)
#array([-10,  -9,  -8,  -7,  -6,  -5,  -4,  -3,  -2,  -1,   0,   1,   2,
#         3,   4,   5,   6,   7,   8,   9])


np.randint(low, high=None, size=None, dtype='l')

#返回一个以low为起点,high为终点范围的一个随机数组,个数为size个,并且不包括high的数组
#前闭后开的数组对象

np.randint(1,10,size=10)





np.np.random.randn(d0, d1, ..., dn)

#返回一个以0为基数,1为方差的一个 “standard normal”(标准正态分布)array对象

np.random.randn(10,2)

array([[ 1.06283548,  0.93768445],
       [ 0.15711565, -1.29453142],
       [ 0.34739985,  0.14974452],
       [ 0.03230129, -0.44508866],
       [ 0.03402864,  1.11649376],
       [-1.77600817, -0.99550157],
       [ 0.61341177,  1.0766857 ],
       [-0.97082646,  0.28436501],
       [-0.92214808, -1.33064102],
       [-0.86134443, -0.71598808]])




np.random.normal(loc=0.0, scale=1.0, size=None)

#返回一个以loc为基准点,方差为scale,个数为size的一个array对象,
#其中size可以是一个shape

np.random.normal(loc = 0, scale=1, size = 10)
#array([ 0.07770909,  0.47226847, -0.33909115,  0.56937667, -0.5731395 ,
#       -0.37128669,  1.03724079, -0.59273687,  0.92519545,  0.16801994])

np.random.normal(loc=10,scale=2,size=(3,3,2))

array([[[ 8.94910751, 11.70566086],
        [ 9.90713552, 10.91617556],
        [10.76627436,  8.61207198]],

       [[11.71162777, 13.88814743],
        [16.21048442, 10.2614945 ],
        [15.23452838,  7.90777894]],

       [[11.33803611, 10.19356861],
        [ 9.60406136, 12.11230304],
        [12.2128692 ,  9.86502732]]])


前言,这些库大家需要的时候再去学习即可,如果学会长期不用还是需要重新来过。

np.random.random(size = (1,1))

#返回一个以0为起始点,1为终点,数组个数为size的array对象。
#size可以是一个数字,也可以是一个shape

np.random.random(size = 10)
#array([0.60236131, 0.36293486, 0.88033242, 0.46875522, 0.59595679,
#       0.25667719, 0.13825517, 0.81876854, 0.0880346 , 0.31408352])

np.random.random(size=(3,2,2))

array([[[0.67918611, 0.08228113],
        [0.4241379 , 0.48925658]],

       [[0.49002966, 0.0061018 ],
        [0.67494826, 0.1707245 ]],

       [[0.76267958, 0.13341905],
        [0.72905039, 0.65318756]]])

 

上一篇:Lesson3——Pandas Series结构


下一篇:pandas库