《实验二Python语言基础函数包练习-.pdf》由会员分享,可在线阅读,更多相关《实验二Python语言基础函数包练习-.pdf(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、实验二Python语言基础函数包练习姓名:季鹏班级:1208 学号:2012301630030 实验目的1、熟练掌握 Python 语言基础函数包,如math、NumPy、SciPy和 Matplotlib 等函数包的使用实验内容练习 1 基本统计可视化:Step 1.导入编程所需要函数包:Step 2.柱状图可视化:可视化结果(将图粘贴到这个位置):Step 3.散点图可视化:可视化结果(将图粘贴到这个位置):Step 4:box-plot 可视化可视化结果(将图粘贴到这个位置):Step 5:三次样条插值可视化结果(将图粘贴到这个位置):练习内容1、要求读取某课程期末考试的成绩“score
2、s.csv”,字段信息如下:ID:学号;fscore:期末考试成绩groupe:分组组别class:班级score1:第一次平时成绩score2:第二次平时成绩score3:第三次平时成绩score:最终综合成绩要求:1)分别画出四个成绩的柱状图,观察是否满足正态分布;2)分别拟合第一次、第二次、第三次成绩和期末考试成绩之间的关系;3)分别按照班级和组别做boxplot,观察每一组和每一班之间成绩的浮动区间。提示 1:读取 csv 文件import csv in_file=open(scores.csv,r)csv_reader=csv.reader(in_file,delimiter=,)#
3、read the header info header=csv_reader.next()print header 代码#coding UTF-8 import numpy as np import scipy as spy import matplotlib.mlab as mlab import matplotlib.pyplot as plt from scipy.optimize import leastsq import pylab as pl import csv in_file=open(E:studtstudylearnpythongisExperiment2Experimen
4、t2scores.csv,r)csv_reader=csv.reader(in_file,delimiter=,)#read the header info header=csv_reader.next()#print header#define ID=group=Class=fscore=score1=score2=score3=score=#read data for line in csv_reader:#ingnore firstline if csv_reader.line_num=0:continue ID.append(line0)fscore.append(line1)grou
5、p.append(float(line2)Class.append(float(line3)score1.append(float(line4)score2.append(float(line5)score3.append(float(line6)score.append(float(line7)#print ID#print group#print score plt.figure(1)#score1 plt.subplot(411)n1,bins1,patches1=plt.hist(score1,25,normed=1,facecolor=green,alpha=0.8)y1=mlab.
6、normpdf(bins1,100,15)l=plt.plot(bins1,y1,r-,linewidth=1)plt.xlabel(ID)plt.ylabel(score1)plt.title(Histogram of score)#score2 plt.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(
7、score2)#score3 plt.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)#score ax4=plt.subplot(414)n1,bins1,patches1=plt.hist(score,25,normed=1,facecolor=blue,alpha=0.8)y1=
8、mlab.normpdf(bins1,100,15)l=plt.plot(bins1,y1,r-,linewidth=1)plt.xlabel(ID)plt.ylabel(score)#plt.show()#22222222222222#22222222222222222222222 plt.figure(2)fig=plt.figure(2)def pic(score1,score,ax):import random order=9 plt.plot(score,score1,color=m,linestyle=,marker=.)matA=for i in range(0,order+1)
9、:matA1=for j in range(0,order+1):tx=0.0 for k in range(0,len(score1):dx=1.0 for l in range(0,j+i):dx=dx*score1k tx+=dx matA1.append(tx)matA.append(matA1)#print(len(xa)#print(matA00)matA=np.array(matA)matB=for i in range(0,order+1):ty=0.0 for k in range(0,len(score1):dy=1.0 for l in range(0,i):dy=dy*
10、score1k ty+=scorek*dy matB.append(ty)matB=np.array(matB)matAA=np.linalg.solve(matA,matB)#print(matAA)yya=for i in range(0,len(score1):yy=0.0 for j in range(0,order+1):dy=1.0 for k in range(0,j):dy*=scorei dy*=matAAj yy+=dy yya.append(yy)ax.plot(score,yya,color=g,linestyle=-,marker=)ax.legend()return
11、 1 ax1=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)#33333333333333333 aa=for i in range(len(group):if groupi not in aa:temp=groupi for j in range(len(group):if groupj=temp:aa.append(temp)break ax=0 for col in rang
12、e(5)for row in range(len(aa)m=0 n=0 for i in range(len(aa):for j in range(len(group):if groupj=aai:axmn=scorej n=n+1 m=m+1 n=0#print ax i=0 labels=list(aa)for i in range(len(aa):labels.append(str(aai)fs=10 fig,axes=plt.subplots(nrows=1,ncols=len(aa),figsize=(40,10)for i in range(len(aa):axesi.boxplo
13、t(filter(lambda e:e!=0,axi),patch_artist=False,bootstrap=None)axesi.set_title(group+str(int(aai),fontsize=fs)i=0 bb=list(set(Class)bx=0 for col in range(len(Class)for row in range(len(bb)m=0 n=0 for i in range(len(bb):for j in range(len(Class):if Classj=bbi:bxmn=scorej n=n+1 m=m+1 n=0#print bx#print the picture fig2,axes2=plt.subplots(nrows=1,ncols=len(bb),figsize=(10,10)i=0 for i in range(len(bb):axes2i.boxplot(filter(lambda e:e!=0,bxi),patch_artist=False,bootstrap=None)axes2i.set_title(class+str(int(bbi),fontsize=fs)plt.show()