Python Numpy

it2023-10-03  80

Numpy

列表创建

x = np.array([1, 2, 3, 4, 5]) print(x)#[1 2 3 4 5] x = np.array([1, 2, 3, 4, 5], dtype="float32") x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) np.zeros(5, dtype=int) #array([0, 0, 0, 0, 0]) np.ones((2, 4), dtype=float) #array([[1., 1., 1., 1.], [1., 1., 1., 1.]]) np.full((3, 5), 8.8) #array([[8.8, 8.8, 8.8, 8.8, 8.8], [8.8, 8.8, 8.8, 8.8, 8.8], [8.8, 8.8, 8.8, 8.8, 8.8]]) np.eye(3) #array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) np.arange(1, 15, 2) array([ 1, 3, 5, 7, 9, 11, 13]) np.linspace(0, 1, 4) array([0. , 0.33333333, 0.66666667, 1. ])#创建一个4个元素的数组,这四个数均匀的分配到0~1 np.logspace(0, 9, 10) array([1.e+00, 1.e+01, 1.e+02, 1.e+03, 1.e+04, 1.e+05, 1.e+06, 1.e+07, 1.e+08, 1.e+09])#等比数列 np.random.random((3,3))#创建一个3*3的,在0~1之间均匀分布的随机数构成的数组 array([[0.24347952, 0.41715541, 0.41363866], [0.44869706, 0.18128167, 0.18568051], [0.05705023, 0.0689205 , 0.74837661]]) np.random.normal(0, 1, (3,3))#创建一个3*3的,均值为0,标准差为1的正态分布随机数构成的数组 array([[-0.38530465, 0.17474932, 0.31129291], [ 1.61626424, -2.18883854, 0.54043825], [-0.9141666 , -0.03804043, -0.6645122 ]]) np.random.randint(0, 10, (3,3))#创建一个3*3的,在[0,10)之间随机整数构成的数组 array([[9, 1, 9], [0, 3, 9], [8, 5, 4]]) x = np.array([10, 20, 30, 40]) np.random.permutation(x) # 生产新列表 array([20, 40, 10, 30]) print(x) np.random.shuffle(x) # 修改原列表 print(x) [10 20 30 40] [20 40 10 30]

数组的采样

np.random.choice(x, size=(4, 3)) np.random.choice(10, 10) import numpy as np x = np.arange(5).reshape(1,5)#[[0 1 2 3 4]] y = np.arange(5)#[0 1 2 3 4] x = np.arange(6).reshape(2,3) [[0 1 2] [3 4 5]] np.random.choice(x, size=(4, 3), p=x/np.sum(x))#p的意思是利用概率分布取样 比如4 就是40%

numpy数组的数据类型是固定的,向一个整型数组插入一个浮点值,浮点值会向下进行取整

x2 = np.random.randint(0, 20, (2,3)) array([[11, 3, 11], [ 6, 1, 5]]) x2[0, 0] = 1.618 array([[ 1, 3, 11], [ 6, 1, 5]]

切片获取的是视图,而非副本

x4 = np.random.randint(20, size=(3,4)) x4 x5 = x4[:2, :2]#这样修改了X5里面内容,同时也会影响X4 x6 = x4[:2, :2].copy()#这种就不会修改了

数组的变形

x5 = np.random.randint(0, 10, (12,)) array([9, 8, 5, 9, 2, 6, 2, 9, 4, 5, 1, 7]) x6 = x5.reshape(3, 4)#reshape返回的是视图,而非副本,新建3行,没行里面有4个元素 array([[9, 8, 5, 9], [2, 6, 2, 9], [4, 5, 1, 7]])

一维向量转行向量

array([0, 8, 5, 9, 2, 6, 2, 9, 4, 5, 1, 7]) x7 = x5.reshape(1, x5.shape[0]) #shape[0]是提取有多少列,[1]是提取多少行 x8 = x5[np.newaxis, :] #newaxis其实就是为了建设新轴

一维向量转列向量

x7 = x5.reshape(x5.shape[0], 1) x8 = x5[:, np.newaxis] array([[0], [8], [5], [9], [2], [6], [2], [9], [4], [5], [1], [7]])

多维向量转一维向量

x6 = np.random.randint(0, 10, (3, 4)) x9 = x6.flatten()#flatten返回的是副本 x10 = x6.ravel()#ravel返回的是视图 array([3, 7, 6, 4, 4, 5, 6, 3, 7, 6, 2, 3])

数组的分裂

x1, x2, x3 = np.split(x6, [2, 7]#从第二和第七切开分成3个数组 left, middle, right = np.hsplit(x7, [2,4])#也是一样,都是切列的位置 upper, middle, lower = np.vsplit(x7, [2,4])#这个行切

矩阵乘法

np.dot(y, x)#做矩阵乘法运算 x*y#只是简单的对应位置的数相乘

广播运算

如果两个数组的形状在维度上不匹配

那么数组的形式会沿着维度为1的维度进行扩展以匹配另一个数组的形状。 意思就是会自己填充到合适的维度计算

利用掩码计算

x2 array([[1, 4, 2, 9], [8, 8, 2, 4], [9, 5, 3, 6]]) x2 > 5 array([[False, False, False, True], [ True, True, False, False], [ True, False, False, True]]) x2[x2 > 5]#利用上面的X1>5作为掩码进行取出大于5的值 array([9, 8, 8, 9, 6])
最新回复(0)