import numpy as np import random # #numpy中的bool类型 # t4=np.array([0,1,1,0,1,1],dtype=bool) # print(t4) # print(t4.dtype) # # 返回: # # [False True True False True True] # # bool # # #更改数据类型,init8 # t5=t4.astype("int8") # print(t5) # print(t5.dtype) # # 返回: # # [0 1 1 0 1 1] # # int8 # #取四舍五入的数字 # t7=np.array([random.random() for i in range(10)]) # print (t7) # #[0.5833703 0.15397013 0.72012901 0.48517398 0.68017715 0.21818563 # # 0.61719503 0.73300055 0.94033097 0.61417069] # t8=np.round(t7,2) # print(t8) # #[0.58 0.15 0.72 0.49 0.68 0.22 0.62 0.73 0.94 0.61] # #数据的reshape,降维或升维 # a=np.arange(24) # print (a) # #返回 # #[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] # # b=a.reshape(2,3,4) # print (b) # #返回一个三维数组,2组*3行*4列 # # [[[ 0 1 2 3] # # [ 4 5 6 7] # # [ 8 9 10 11]] # # # # [[12 13 14 15] # # [16 17 18 19] # # [20 21 22 23]]] # # c=b.reshape(4,6) # print(c) # #返回一个4行6列的数组 # # [[ 0 1 2 3 4 5] # # [ 6 7 8 9 10 11] # # [12 13 14 15 16 17] # # [18 19 20 21 22 23]] # # d=b.reshape(24) # print(d) # print(d.shape) # #返回了一个一维的数组 # #[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] # #(24,) # e=b.reshape((1,24)) # print(e) # print(e.shape) # #返回的是一个二维数组了,虽然只有一行,这个和reshape(24)是有区别的 # #所以reshape里面有几个值,所生成的数组就是几维 # #[[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]] # #(1, 24) # #将一个多维数组直接降为一维的另外两种方法 # a=np.arange(24).reshape(4,6) # b=a.reshape((a.shape[0]*a.shape[1])) # print (b) # #返回 # #[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] # # #还可以用flatten方法 # a=np.arange(24).reshape(4,6) # b=a.flatten() # print (b) # #返回 # #[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] # #数据和数值的计算 # a=np.arange(24).reshape(4,6) # print (a) # print('*'*20) # print(a+2) # #返回结果如下,数组中的每一个数字都加了2,就是数组的广播机制 # # [[ 0 1 2 3 4 5] # # [ 6 7 8 9 10 11] # # [12 13 14 15 16 17] # # [18 19 20 21 22 23]] # # ******************** # # [[ 2 3 4 5 6 7] # # [ 8 9 10 11 12 13] # # [14 15 16 17 18 19] # # [20 21 22 23 24 25]] # #数组和数组的计算 # a=np.arange(24).reshape(4,6) # b=np.arange(100,124).reshape(4,6) # print(a) # print('*'*20) # print(b) # print('*'*20) # print(a+b) # #返回的值是数组内对应的值相加 # # [[ 0 1 2 3 4 5] # # [ 6 7 8 9 10 11] # # [12 13 14 15 16 17] # # [18 19 20 21 22 23]] # # ******************** # # [[100 101 102 103 104 105] # # [106 107 108 109 110 111] # # [112 113 114 115 116 117] # # [118 119 120 121 122 123]] # # ******************** # # [[100 102 104 106 108 110] # # [112 114 116 118 120 122] # # [124 126 128 130 132 134] # # [136 138 140 142 144 146]] # #如果两个数组的维度不一样? # a=np.arange(24).reshape(4,6) # b=np.arange(6) # print(a) # print('*'*20) # print(b) # print('*'*20) # print(a-b) #返回的结果如下: # [[ 0 1 2 3 4 5] # [ 6 7 8 9 10 11] # [12 13 14 15 16 17] # [18 19 20 21 22 23]] # ******************** # [0 1 2 3 4 5] # ******************** # [[ 0 0 0 0 0 0] # [ 6 6 6 6 6 6] # [12 12 12 12 12 12] # [18 18 18 18 18 18]] #可以看出a里面的每一行都是会减去b所对应的值 # [ 0 1 2 3 4 5]会减去[ 0 1 2 3 4 5] # [ 6 7 8 9 10 11]会减去[ 0 1 2 3 4 5]... # #如果行的维度不一样? # a=np.arange(24).reshape(4,6) # b=np.arange(4).reshape(4,1) # print(a) # print('*'*20) # print(b) # print('*'*20) # print(a-b) # #返回如下: # # [[ 0 1 2 3 4 5] # # [ 6 7 8 9 10 11] # # [12 13 14 15 16 17] # # [18 19 20 21 22 23]] # # ******************** # # [[0] # # [1] # # [2] # # [3]] # # ******************** # # [[ 0 1 2 3 4 5] # # [ 5 6 7 8 9 10] # # [10 11 12 13 14 15] # # [15 16 17 18 19 20]] # #同样的也是a对应的列上面减去b所对应的列值 # #[0 6 12 18]减去[0 1 2 3 ],#[1 7 13 19]减去[0 1 2 3 ]... # #将csv文件导入到数组 # file_path="./loadtext.csv" # #skiprows表示需要跳过前面几行,一般是行头 # #dtype表示数据类型 # #delimiter表示分割符 # #unpack表示行列转换,默认为False # a=np.loadtxt(file_path,skiprows=1,dtype="int",delimiter=",") # print (a) # print("*"*20) # b=np.loadtxt(file_path,skiprows=1,dtype="int",delimiter=",",unpack=True) # print(b) # #返回结果: # # [[505 46 100] # # [549 5 100] # # [501 32 96] # # ... # # [681 30 91] # # [692 21 100] # # [701 11 30]] # # ******************** # # [[505 549 501 ... 681 692 701] # # [ 46 5 32 ... 30 21 11] # # [100 100 96 ... 91 100 30]] # # # #数组行列转换的三种方法: # # a=np.arange(24).reshape(4,6) # # print(a.transpose()) # # print(a.T) # # print(a.swapaxes(1,0))#交换x,y轴 #
Python之Numpy基础2
最后发布:2020-10-18 15:57:03首次发布:2020-10-18 15:57:03