学习Numpy
import numpy as np score = np.array([[80,80,76,56,56], [53,65,54,78,97], [76,44,24,97,56], [21,64,34,74,34], [12,42,12,42,35]]) a = score.shape #形状:(5,5) 其中()代表元组 里面几个数字代表几维度 b = score.ndim #维度 c = score.size #几个元素 d = score.dtype #类型 创建ndarray的时候,如果没有指定类型,整数时默认"int64",小数为"float64" f = score.itemsize #每个元素占用字节 print(a,b,c,d,f) array_1 = np.array([1,2,3,4]) #一维 array_2 = np.array([[[1,2,3], [4,5,6]], [[1,2,3], [4,5,6]]]) #三维 (三个中括号) 维度表示方法(嵌套两个二维元素,二维数字几行,几列)即(2,2,3) print(array_2.shape)
#均匀分布
import matplotlib.pyplot as plt x1 = np.random.uniform(-1,1,1000000) plt.figure(figsize = (20,8), dpi = 80) plt.hist(x1,1000) plt.show()
#正态分布
#正态分布 x2 = np.random.normal(loc = 0, scale = 1, size = 1000000) plt.figure(figsize = (20,8), dpi = 80) plt.hist(x2,1000) plt.show()
import numpy as np x = np.random.normal(0,1,(8,10)) print(x) #布尔索引 统一操作 x[x>0.5]=1 print(x>0) #返回True #判断 a = x[0:2, 0:5]>0#返回True/False 判断前2组的前5哥 b = np.all(x[0:2, 0:5]>0) #全True才返回True c = np.any(x[0:2, 0:5]>0) #有一个True返回True print(a,b,c) #np.where temp = x[:4,:4] #取前四组的前4天 print("temp=",temp) d = np.where(temp>0, 1, 0) #将 大于0的置1,否则为0 print("d=",d) #逻辑关系 且and/或or f = np.logical_and(temp>0,temp<1) #选取temp>0且<1的数据 g = np.where(f,1,0) #将temp>0且<1置1,否则为0 print("g=",g) #同理 np.logical_or #统计运算 max_temp = temp.max(axis=1) #axis=1每组行中的元素 axis=0为列中的元素 建议测试便知 #等价max_temp = np.max(temp, axis=1) ,以下同理 loc_temp = temp.argmax(axis=1) #显示每行最大元素的位置 0为起始 print("max_temp=",max_temp) print("loc_temp=",loc_temp)