记东北大学理学院大三暑期实训(第二天)
Numpy库
import numpy as np
one = np.ones(shape = (2,4),dtype=int) #创建2行4列的矩阵,元素为int型
#+-*/对矩阵操作遵守广播原理,即对所有元素都进行运算。
one.shape # 输出矩阵的维度
# 输出随机浮点数 维度为3*4
np.random.random(size=(3,4))
a = np.arange(12).reshape(3,4) # 重新排列数组
#索引
a[1:3,:] #选择第二行和第三行的内容
a[:,1] #选择第二列
a[1:3,::2] #第1、3行的从第一列开始以2为步长的所有列
a[:,::-1] #逆列
# python的条件索引
a = np.arange(36).reshape(6, 6) - 18
a[a > 0] = np.log(a[a > 0])#表示a中大于0的元素取对数
a[a < 0] = np.abs(a[a < 0])#a中小于0的元素取绝对值
#bool值在数组中的应用
#布尔值可以随意完成对数组某些元素的访问,只需要把需要访问的位置的bool值设为1即可。
a = np.arange(9).reshape(3, 3)
mask = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], dtype = bool)
zero = np.zeros(shape=(3,3))
##求极大值
max_row = np.max(a, axis = 1) #求解每一行的最大值
print(max_row)
max_col = np.max(a, axis = 0) #求解每一列的最大值
print(max_col)
max2 = np.max(a, axis=(0, 1)) # 求最大值
print(max2)
######accumulate的用法
a = np.random.random(size = 5)
b = np.random.random(size=(2,4))
print(np.maximum.accumulate(a)) #accumulate的作用是求累积最大值和最小值
print(np.maximum.accumulate(b, axis = 1)) #按列
print(np.minimum.accumulate(a))
##求逆
a = np.arange(16).reshape(4, 4)
print(np.linalg.inv(a)) #求逆
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
y = np.random.random(size=5)
x = range(5)
plt.plot(x)
plt.plot(x,y)
plt.plot(x,y,'ro--',linewidth =3)
colar:'r','b','g'
# o--代表在关键节点标注且用虚线联结
# ^- 代表在关键节点用三角标注用实线联结
# -. 代表点分线联结
###
# '--','s','*' 这些作为参数放在plot函数中可使得点的形状变化
plot(x,y,'s')
plot(x,y,'*')
plot(x,y,'--')
##子图
x=np.array(range(0,5))
y1=x**2
y2=x+0.5
y3=np.log(2*x+1)
y4=1/(x+1)*np.sin(x)
plt.subplot(2,2,1) #三个参数分别代表:行、列、当前子图的id号(1,2,3,4)
plt.plot(x,y1,'r')
plt.subplot(2,2,2)
plt.plot(x,y2,'g')
plt.subplot(2,2,3)
plt.plot(x,y3,'b')
plt.subplot(2,2,4)
plt.plot(x,y4,'y')
plt.show()
##备注说明
x = np.arange(10)
y1 = np.log(2*x+1)
y2 =1/(x+1)*np.sin(x)
plt.plot(x,y1,'ro--',label='log(2x+1)')
plt.plot(x,y2,'bo--',label='sinx/(x+1)')
plt.legend() # 显示图例说明
#参数loc= 'upper left'/'low right'
# arg1:显示内容 arg2:箭头指向的位置 arg3:xytext标注文本的位置
# arg4:字体大小 arg5:箭头的属性
plt.annotate('max_value',xy=(1,0.42),xytext=(2,0.42),fontsize=20,arrowprops=dict(facecolor='black',width=6))
plt.xticks(np.arange(10)) # xticks设置横轴的精度0,1,2,...9点设置辅助线
plt.grid(True) # 用于设置是否开启辅助线
plt.xlabel('x axis') # 给坐标系加注释
plt.ylabel('y axis')
plt.title('The six(x) figure')
plt.show()
Pandas
#arg1:要读取数据的路径 arg2:列名称 arg3:列之间的间隔 arg4:添加的列信息
data=pd.read_csv('data.csv',header=None,sep=r'\s+',name=col_names)
data.describe()
data.head()
data.info()
data.INDUS.plot(kind='line',color='g',label='INDUS',linewidth=2,grid=True,linestyle='-') #表格中INDUS列的数据的绘图
plt.legend() #有label一定要加
data.RM.plot(kind='hist',bins=50)
# lengend title xlabel ylabel
plt.show()
########################7-15##########################
data.RM.plot(kind='hist',bins=50,cumulative=True)
#累计直方图
# lengend title xlabel ylabel
plt.show()
-----------------------------------
#散点图
data.RM.plot(kind='scatter',x='TAX',y='PTRATIO',color='r')
#累计直方图
# lengend title xlabel ylabel
plt.show()
本文详细介绍Numpy库的基础操作,包括矩阵创建、条件索引、统计运算等,并通过实例展示如何使用Matplotlib进行数据可视化,如绘制图表、子图及添加注释。
1万+





