1.machinelearning的好伙伴numpy
作者:互联网
1.machinelearning的好伙伴numpy
import numpy as np from numpy import pi # np.array()函数,print(help(np.array))文档 vector = np.array([5,10,15,20]) print(vector) matrix = np.array([[1,2,3],[2,4,6]]) print(matrix) print(matrix.shape) # 得到数据大小是两行三列 print(matrix.dtype) # 得到数据类型numpy中矩阵的元素类型必须相同 print(matrix[1,0]) # 得到索引值 print(matrix[1,0:3]) # 取出一行,进行切片 print(matrix[:,1]) # 取出一列 print(matrix[:,0:2]) # 取出两列 [ 5 10 15 20] [[1 2 3] [2 4 6]] (2, 3) int32 2 [2 4 6] [2 4] [[1 2] [2 4]]
# 判断 vector == 10 print(vector == 10) # 对每个元素进行判断 print(vector[vector==10]) # 返回索引到的数据,否则返回空 print(matrix[:,1] == 4) # 定位到第二行为true print(matrix[matrix[:,1] == 4,:]) # 输出第二行所有数据 # 与或非判断 print((vector == 10) & (vector ==5)) print((vector == 10) | (vector ==5)) print((vector != 10) & (vector !=5)) [False False False] [] [False True] [[2 4 6]] [False False False] [False False False] [ True True True]
# 数据类型进行改变 vector = np.array(["1","2","3"]) print(vector,vector.dtype) vector = vector.astype(float) print(vector,vector.dtype) vector = vector.astype(int) print(vector,vector.dtype) ['1' '2' '3'] <U1 [1. 2. 3.] float64 [1 2 3] int32
# 求和 matrix = np.array([[1,2,3],[2,4,6]]) print(matrix.sum(axis=1)) print(matrix.sum(axis=0)) [ 6 12] [3 6 9]
# 矩阵维度变换 print(np.arange(15)) # 取出十五个数组成一行15列矩阵 print(np.arange(15).reshape(3,5)) # 变换成三行五列矩阵 # 初始化矩阵 print(np.zeros((3,4))) # 初始化零矩阵 print(np.ones((2,3,4),dtype=np.int32)) # 初始化单位矩阵,并指定元素类型 [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[[1 1 1 1] [1 1 1 1] [1 1 1 1]] [[1 1 1 1] [1 1 1 1] [1 1 1 1]]] # 指定特定的数组 vector = np.arange(5,50,10) # 从5.开始终点是50,每十个数输出 print(vector) vector = np.linspace(0,100*pi,100) # 起点是0,终点是100,取100个值,前一个值加平均数等于后一个值 print(vector) # 指定一个随机数列 print(np.random.random((2,3))) # 范围在-1到+1 上 [ 5 15 25 35 45] [ 0. 3.17332591 6.34665183 9.51997774 12.69330365 15.86662956 19.03995548 22.21328139 25.3866073 28.55993321 31.73325913 34.90658504 38.07991095 41.25323687 44.42656278 47.59988869 50.7732146 53.94654052 57.11986643 60.29319234 63.46651825 66.63984417 69.81317008 72.98649599 76.15982191 79.33314782 82.50647373 85.67979964 88.85312556 92.02645147 95.19977738 98.37310329 101.54642921 104.71975512 107.89308103 111.06640695 114.23973286 117.41305877 120.58638468 123.7597106 126.93303651 130.10636242 133.27968833 136.45301425 139.62634016 142.79966607 145.97299198 149.1463179 152.31964381 155.49296972 158.66629564 161.83962155 165.01294746 168.18627337 171.35959929 174.5329252 177.70625111 180.87957702 184.05290294 187.22622885 190.39955476 193.57288068 196.74620659 199.9195325 203.09285841 206.26618433 209.43951024 212.61283615 215.78616206 218.95948798 222.13281389 225.3061398 228.47946572 231.65279163 234.82611754 237.99944345 241.17276937 244.34609528 247.51942119 250.6927471 253.86607302 257.03939893 260.21272484 263.38605076 266.55937667 269.73270258 272.90602849 276.07935441 279.25268032 282.42600623 285.59933214 288.77265806 291.94598397 295.11930988 298.2926358 301.46596171 304.63928762 307.81261353 310.98593945 314.15926536] [[0.13523025 0.87619689 0.23210647] [0.80847253 0.60055585 0.62466086]]
# 矩阵的乘法 a = np.array([[1,1], [1,1]]) b = np.array([[1,2], [3,4]]) print(a*b) # 对应位置的相乘 print(a.dot(b)) # 矩阵相乘 print(np.dot(b,a)) # 幂次方计算 B= np.arange(3) print(np.exp(B)) # e的多少次方 print(np.sqrt(B)) # 每个元素的开根号 # 进行向下取整 a = np.floor(10*np.random.random((3,4))) print(a) [[1 2] [3 4]] [[4 6] [4 6]] [[3 3] [7 7]] [1. 2.71828183 7.3890561 ] [0. 1. 1.41421356] [[2. 1. 5. 9.] [1. 3. 2. 3.] [3. 5. 9. 9.]]
# 矩阵拉平 print(a.ravel().reshape(2,-1)) # 填写-1为默认进行计算 # 矩阵拼接 a = np.floor(10*np.random.random((2,2))) print('a',a) b = np.floor(10*np.random.random((2,2))) print('b',b) print(np.hstack((a,b))) # 行进行拼接 print(np.vstack((a,b))) # 直接拼接到列 # 矩阵切分 a = np.floor(10*np.random.random((2,12))) print(a) print(np.hsplit(a,3)) # 只能进行平均分 print(np.hsplit(a,(3,4))) # 对指定位置进行切分 a = np.floor(10*np.random.random((12,2))) print(np.vsplit(a,6)) print(np.vsplit(a,(3,6,9))) [[2. 1. 5. 9. 1. 3.] [2. 3. 3. 5. 9. 9.]] a [[1. 5.] [6. 5.]] b [[9. 4.] [6. 7.]] [[1. 5. 9. 4.] [6. 5. 6. 7.]] [[1. 5.] [6. 5.] [9. 4.] [6. 7.]] [[6. 0. 3. 3. 8. 5. 7. 9. 4. 3. 3. 1.] [6. 6. 8. 1. 5. 6. 6. 7. 6. 3. 3. 8.]] [array([[6., 0., 3., 3.], [6., 6., 8., 1.]]), array([[8., 5., 7., 9.], [5., 6., 6., 7.]]), array([[4., 3., 3., 1.], [6., 3., 3., 8.]])] [array([[6., 0., 3.], [6., 6., 8.]]), array([[3.], [1.]]), array([[8., 5., 7., 9., 4., 3., 3., 1.], [5., 6., 6., 7., 6., 3., 3., 8.]])] [array([[9., 2.], [8., 7.]]), array([[3., 8.], [5., 8.]]), array([[0., 2.], [5., 0.]]), array([[9., 6.], [0., 5.]]), array([[4., 8.], [3., 0.]]), array([[0., 3.], [3., 5.]])] [array([[9., 2.], [8., 7.], [3., 8.]]), array([[5., 8.], [0., 2.], [5., 0.]]), array([[9., 6.], [0., 5.], [4., 8.]]), array([[3., 0.], [0., 3.], [3., 5.]])]
# 矩阵的赋值复制 a = np.arange(12) b = a b.shape = 3,4 print('a',a) # 当b的改变a也改变 print(id(a)) # 指向的存储空间相同 print(id(b)) a [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] 2559984831664 2559984831664
# 矩阵的潜复制 c = a.view() c.shape = 2,6 print(c) print(a) c[0,4] = 1234 # 当c的数值改变a才改变 print(a) print(id(c)) # 指向的存储空间不同,但值是共用的 print(id(a)) # 矩阵的copy深复制 d = a.copy() print(d) print(a) d[1,0] = 150 print(d) print(a) print(id(a)) # 存储空间不同,值也不是共用的 print(id(d)) [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11]] [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[ 0 1 2 3] [1234 5 6 7] [ 8 9 10 11]] 2559984867888 2559984831664 [[ 0 1 2 3] [1234 5 6 7] [ 8 9 10 11]] [[ 0 1 2 3] [1234 5 6 7] [ 8 9 10 11]] [[ 0 1 2 3] [150 5 6 7] [ 8 9 10 11]] [[ 0 1 2 3] [1234 5 6 7] [ 8 9 10 11]] 2559984831664 2559984867568
# 极值的索引操作 data = np.floor(np.random.random((4,4))*10) print(data) ind = data.argmax(axis = 0) print(ind) # 求出列中最大值的行索引值 data_max = data[ind,range(data.shape[1])] # 给出行数组,再给出列数组 [[3. 6. 8. 3.] [3. 0. 5. 0.] [7. 9. 0. 8.] [7. 4. 0. 0.]] [2 2 0 2]
# 矩阵的扩展 a= np.arange(0,40,10) b = np.tile(a,(3,5)) # 对原数组进行成倍扩展 No output
# 矩阵的排序 a = np.array([[4,3,5],[1,2,6]]) print(a) b = np.sort(a,axis=1) # 一行进行排序 print(b) c = np.sort(a,axis=0) # 一列之间进行排序 print('c',c) print(a) a = np.array([1, 4,3 ,2]) # 对矩阵的进行索引排序 j = np.argsort(a) print(j) [[4 3 5] [1 2 6]] [[3 4 5] [1 2 6]] c [[1 2 5] [4 3 6]] [[4 3 5] [1 2 6]] [0 3 2 1]
标签:10,machinelearning,random,np,vector,print,array,numpy,伙伴 来源: https://www.cnblogs.com/wigginess/p/13069210.html