numpy
之前学的:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# -*- coding: utf-8 -*- """ Created on Fri May 29 11:51:15 2020 @author: Administrator """ import numpy as np import random t1=np.array([2,3,4,5,6]) t2=np.array(range(10)) print(t1) print(type(t1)) print(t2) t3=np.arange(11,dtype="int8") print(t3) print(type(t3)) #输出数据类型 print(t3.dtype) #调整数据类型 t3.astype("int16") t4=np.array([random.random() for i in range(10)]) print(t4) #都可以,取两位小数 t5=t4.round(2) t5=np.round(t4,3) print(t5) #形状 t6=np.arange(30) print(t6) print(t6.shape) t7=t6.reshape(3,5,2) print(t7) print(t7.flatten()) print(t7.ravel()) print(t7.shape) #转化为一维数组 t8=t7.flatten() print(t8) #加法 print("*"*20) t9=np.arange(10)+10 print(t9*2) #ru如果行一样或者列一样都可以计算 #从右往左看,维度一样就可以计算 #print(np.arange(16).reshape(4,4)*np.arange(4).reshape(1,4)) print(np.arange(16).reshape(4,2,2)-np.arange(4).reshape(2,2)) #转置 t10=np.arange(1,17).reshape(4,4) print(t10) print(t10.T) print(t10.transpose()) #相同不转置,相反则转置 print(t10.swapaxes(0,0)) #洲 0=x 1=y 2=z axis #求x坐标轴的平均值 那就是其他的坐标都一样,然后 x坐标不同的值相加 t11=np.arange(1,17).reshape(2,2,4) for x in range(2): for y in range(2): for z in range(4): print("({0[0]},{0[1]},{0[2]})={0[3]}".format([x,y,z,t11[x][y][z]])) print(t11) print(t11.sum(axis=0)) print(t11.mean(axis=0)) print(t11.min(axis=0)) print(t11.max(axis=0)) #读取数据 #unpack=True转置 ft=np.loadtxt("F:\\a.csv",delimiter=",",dtype="int") #切片 t12=np.arange(1,17).reshape(4,4) print(t12) print(t12[[0,2]][:]) #条件 np.where(t12<10,0,16) print(np.where(t12<10,0,16)) #小于3换成3,大于8换成8 print(t12.clip(3,8)) #赋值nan t12=t12.astype(float) t12[0][0]=np.nan print(t12) print(t12) #数据的拼接 t13=np.arange(1,9).reshape(2,4) t14=np.arange(9,17).reshape(2,4) print(t13) print(t14) print(np.vstack((t13,t14))) print(np.hstack((t13,t14))) #行与行交换 t15=t14[[0,1],:]=t14[[1,0],:] print(t14) print(t15) #列与列交换 t16=t14[:,[0,1]]=t14[:,[1,0]] print(t14) print(t16) print(t14[:,[1,2,0]]) #全为0 np.zeros(2,2) np.ones(2,2) #对角线全为1的方阵 np.eye(3) #获取最大值,最小值的位置 t17=np.arange(1,16).reshape(1,15) print(t17) np.argmax(t17) np.argmin(t17) #随机数 np.random.rand()#均匀分布 np.random.randn()#正态分布 np.random.randint(a,b,(4,5)) np.random.uniform(a,b,(4,5)) np.random.normal() np.random.seed(s)#加上这个,效果一样,每次产生的随机数不变 #别用=号 a=b别用 切片也会相互影响 #用a=b.copy()这样不影响 #不为0的个数 #计算nan个数 np.count_nonzero(b!=b) np.isnan(b) np.count_nonzero(np.isnan(b)) #nan和啥计算都是nan #把nan换成平均数 np.random.seed(1) t18=np.random.randint(1,20,(4,4)) t18=t18.astype("float") t18[1,2]=np.nan #竟然可以这样写 print(t18[t18==t18]) t19=t18.copy() t19=t19.reshape(16) np.where(np.isnan(t19),0,t19) n=np.count_nonzero(t18==t18) my_mean=np.sum(np.where(np.isnan(t19),0,t19))/n t18=np.where(np.isnan(t18),my_mean,t18) print(t18) #二替换nan为均值 np.random.seed(1) t18=np.random.randint(1,20,(4,4)) t18=t18.astype("float") t18[1,2]=np.nan print(t18) def tr_nan(arr): t18=arr #t19=t18[t18==t18].copy().reshape(t18.shape[0]*t18.shape[1]) t19=t18[t18==t18].copy() my_mean=t19.sum()/t19.shape[0] t18[np.isnan(t18)]=my_mean print(t18) return t18 #判断提取数据用 print(t18[t18>10]) #判断且修改数据 np.where(t18==t18,t18,0) import numpy as np t20=np.arange(1,5) print(t20.shape) t21=t20.copy().reshape(4,1) print(t21) print(t20*t21)
-----------------------------------------------------------------------------------------------------------------------------------------