实验二Python语言基础函数包练习姓名:季鹏班级:1208学号:2012301630030实验目的1、熟练掌握Python语言基础函数包,如math、NumPy、SciPy和Matplotlib等函数包的使用实验内容练习1基本统计可视化:Step1.导入编程所需要函数包:Step2.柱状图可视化:可视化结果(将图粘贴到这个位置):Step3.散点图可视化:可视化结果(将图粘贴到这个位置):Step4:box-plot可视化可视化结果(将图粘贴到这个位置):Step5:三次样条插值可视化结果(将图粘贴到这个位置):练习内容1、要求读取某课程期末考试的成绩“scores.csv”,字段信息如下:ID:学号;fscore:期末考试成绩groupe:分组组别class:班级score1:第一次平时成绩score2:第二次平时成绩score3:第三次平时成绩score:最终综合成绩要求:1)分别画出四个成绩的柱状图,观察是否满足正态分布;2)分别拟合第一次、第二次、第三次成绩和期末考试成绩之间的关系;3)分别按照班级和组别做boxplot,观察每一组和每一班之间成绩的浮动区间。提示1:读取csv文件importcsvin_file=open('scores.csv','r')csv_reader=csv.reader(in_file,delimiter=',')#readtheheaderinfoheader=csv_reader.next()printheader代码#codingUTF-8importnumpyasnpimportscipyasspyimportmatplotlib.mlabasmlabimportmatplotlib.pyplotaspltfromscipy.optimizeimportleastsqimportpylabasplimportcsvin_file=open('E:\studtstudylearn\python\gis\Experiment2\Experiment2\scores.csv','r')csv_reader=csv.reader(in_file,delimiter=',')#readtheheaderinfoheader=csv_reader.next()#printheader#defineID=[]group=[]Class=[]fscore=[]score1=[]score2=[]score3=[]score=[]#readdataforlineincsv_reader:#ingnorefirstlineifcsv_reader.line_num==0:continueID.append(line[0])fscore.append(line[1])group.append(float(line[2]))Class.append(float(line[3]))score1.append(float(line[4]))score2.append(float(line[5]))score3.append(float(line[6]))score.append(float(line[7]))#printID#printgroup#printscoreplt.figure(1)#score1plt.subplot(411)n1,bins1,patches1=plt.hist(score1,25,normed=1,facecolor='green',alpha=0.8)y1=mlab.normpdf(bins1,100,15)l=plt.plot(bins1,y1,'r--',linewidth=1)plt.xlabel('ID')plt.ylabel('score1')plt.title('Histogramofscore')#score2plt.subplot(412)n1,bins1,patches1=plt.hist(score2,25,normed=1,facecolor='red',alpha=0.8)y1=mlab.normpdf(bins1,100,15)l=plt.plot(bins1,y1,'r--',linewidth=1)plt.xlabel('ID')plt.ylabel('score2')#score3plt.subplot(413)n1,bins1,patches1=plt.hist(score3,25,normed=1,facecolor='yellow',alpha=0.8)y1=mlab.normpdf(bins1,100,15)l=plt.plot(bins1,y1,'r--',linewidth=1)plt.xlabel('ID')plt.ylabel('score3')#scoreax4=plt.subplot(414)n1,bins1,patches1=plt.hist(score,25,normed=1,facecolor='blue',alpha=0.8)y1=mlab.normpdf(bins1,100,15)l=plt.plot(bins1,y1,'r--',linewidth=1)plt.xlabel('ID')plt.ylabel('score')#plt.show()#22222222222222#22222222222222222222222plt.figure(2)fig=plt.figure(2)defpic(score1,score,ax):importrandomorder=9plt.plot(score,score1,color='m',linestyle='',marker='.')matA=[]foriinrange(0,order+1):matA1=[]forjinrange(0,order+1):tx=0.0forkinrange(0,len(score1)):dx=1.0forlinrange(0,j+i):dx=dx*score1[k]tx+=dxmatA1.append(tx)matA.append(matA1)#print(len(xa))#print(matA[0][0])matA=np.array(matA)matB=[]foriinrange(0,order+1):ty=0.0forkinrange(0,len(score1)):dy=1.0forlinrange(0,i):dy=dy*score1[k]ty+=score[k]*dymatB.append(ty)matB=np.array(matB)matAA=np.linalg.solve(matA,matB)#print(matAA)yya=[]foriinrange(0,len(score1)):yy=0.0forjinrange(0,order+1):dy=1.0forkinrange(0,j):dy*=score[i]dy*=matAA[j]yy+=dyyya.append(yy)ax.plot(score,yya,color='g',linestyle='-',marker='')ax.legend()return1ax1=fig.add_subplot(311)pic(score1,score,ax1)ax2=fig.add_subplot(312)pic(score2,score,ax2)ax3=fig.add_subplot(313)pic(score3,score,ax3)#33333333333333333aa=[]foriinrange(len(group)):ifgroup[i]notinaa:temp=group[i]forjinrange(len(group)):ifgroup[j]==temp:aa.append(temp)breakax=[[0forcolinrange(5)]forrowinrange(len(aa))]m=0n=0foriinrange(len(aa)):forjinrange(len(group)):ifgroup[j]==aa[i]:ax[m][n]=score[j]n=n+1m=m+1n=0#printaxi=0labels=list(aa)foriinrange(len(aa)):labels.append(str(aa[i]))fs=10fig,axes=plt.subplots(nrows=1,ncols=len(aa),figsize=(40,10))foriinrange(len(aa)):axes[i].boxplot(filter(lambdae:e!=0,ax[i]),patch_artist=False,bootstrap=None)axes[i].set_title('group'+str(int(aa[i])),fontsize=fs)i=0bb=list(set(Class))bx=[[0forcolinrange(len(Class))]forrowinrange(len(bb))]m=0n=0foriinrange(len(bb)):forjinrange(len(Class)):ifClass[j]==bb[i]:bx[m][n]=score[j]n=n+1m=m+1n=0#printbx#printthepicturefig2,axes2=plt.subplots(nrows=1,ncols=len(bb),figsize=(10,10))i=0foriinrange(len(bb)):axes2[i].boxplot(filter(lambdae:e!=0,bx[i]),patch_artist=False,bootstrap=None)axes2[i].set_title('class'+str(int(bb[i])),fontsize=fs)plt.show()