数据准备
1、数据概览
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pyecharts import Bar,WordCloud,Pie,Line
df=pd.read_csv(r'C:\Users\Administrator\Desktop\StudentsPerformance.csv')
df.head()
这个是数据的字段,前面我已经说过了,很简单。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pyecharts import Bar,WordCloud,Pie,Line
df=pd.read_csv(r'C:\Users\Administrator\Desktop\StudentsPerformance.csv')
df.info()
数据类型也不多,就两个,从图上可以看到,这份数据很干净,不需要进行数据清洗。那我们就直接开始分析吧。
— 2 —
数据分析
1、各科学生成绩分布箱式图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pyecharts import Bar,WordCloud,Pie,Line
df=pd.read_csv(r'C:\Users\Administrator\Desktop\StudentsPerformance.csv')
plt.rcParams['font.sans-serif']=['SimHei']
y1=df['math score']
y2=df['reading score']
y3=df['writing score']
x=[y1,y2,y3]
plt.figure(figsize=(10,5))
labels=['math score','reading score','writing score']
plt.boxplot(x,labels=labels,vert=True)
plt.title('各科目成绩分布箱式图',loc='left',size=15)
plt.xlabel('科目',size=15)
plt.ylabel('分数',size=15)
plt.xticks(size=15)
plt.yticks(size=15)#plt.yticks([]) 可以去掉y轴
plt.grid(False)
sns.despine(left=False )#去掉上面和右边边框
plt.show();
在数据分析中,箱式图是用的比较多的,特别是看分布的时候,非常实用,中间的矩形就是上四分位和下四分位,代表大部分数据集中在这里,下面超过箱式图的代表是异常值,也就是特别低的数据。
从图片看出,各科成绩的集中分数段都是差不多的,都在60到80之间,但是数学成绩的异常值更多,说明数学是最难的。
2、学生整体成绩分组情况
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pyecharts import Bar,WordCloud,Pie,Line
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
df=pd.read_csv(r'C:\Users\Administrator\Desktop\StudentsPerformance.csv')
y1=df['math score']
y2=df['reading score']
y3=df['writing score']
df['总分']= y1 + y2 + y3
def GetGrade(总分):
if ( 总分 >=270 ):
return '优秀'
if ( 总分 >=240):
return '良好'
if ( 总分 >=180):
return '及格'
else:
return '不及格'
df['等级'] = df.apply(lambda x :GetGrade(x['总分']), axis=1)
plt.rcParams['font.sans-serif']=['SimHei']
plt.figure(figsize=(10,5))
sns.countplot(x="等级",data=df, order=['优秀','良好','及格','不及格'],palette="muted")
plt.title('学生成绩分组情况分析',loc='left',size=15)
plt.xlabel('成绩分组情况',size=15)
plt.ylabel('人数',size=15)
plt.grid(False)
sns.despine(left=False )#去掉上面和右边边框
plt.show()
这份图是学生三科汇总分数的分层情况,我把总分在270分以上的,归为优秀;240以上的,归为良好;180以上的归为及格;其余则为不及格。分组的代码已经在上面的代码块中写出来了。
但是上面这两个图还只是学生的成绩情况,并不能看出成绩的影响因素。
下面开始,我将特意从家长学历的角度分析对孩子成绩的影响。
3、父母学历对子女成绩影响1--考试分数
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pyecharts import Bar,WordCloud,Pie,Line
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
df=pd.read_csv(r'C:\Users\Administrator\Desktop\StudentsPerformance.csv')
y1=df['math score']
y2=df['reading score']
y3=df['writing score']
df['总分']= y1 + y2 + y3
plt.figure(figsize=(10,5))
sns.violinplot(x="parental level of education",y="总分",data=df,palette="Set3");
plt.title('父母学历对子女成绩影响1--考试分数',loc='left',size=15)
plt.xlabel('父母学历',size=15)
plt.ylabel('分数',size=15)
plt.xticks(size=12)
plt.yticks(size=12)#plt.yticks([]) 可以去掉y轴
plt.grid(False)
sns.despine(left=False )#去掉上面和右边边框
plt.show();
这个图是小提琴图,类似于箱式图,看法也差不多,尾巴越长,说明低分越多。
从图可以看出,高中未毕业(some high school)的家长子女低分最多;其次是高中家长(high school);然后是大学未毕业家长(some college);然后是大专家长(associate's degree);接下来是大学生家长(bachelor's degree);低分最少的家长是硕士家长(master's degree)。
4、父母学历对子女成绩影响2--合格人数
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pyecharts import Bar,WordCloud,Pie,Line
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
df=pd.read_csv(r'C:\Users\Administrator\Desktop\StudentsPerformance.csv')
#算总分
y1=df['math score']
y2=df['reading score']
y3=df['writing score']
df['总分']= y1 + y2 + y3
#计算合格或者不合格情况
passmark =180
df['考试合格'] = np.where(df['总分']<passmark, 'N', 'Y')
df['考试合格'].value_counts()
plt.figure(figsize=(12,6))
p= sns.countplot(x='parental level of education', data = df, hue='考试合格', palette='bright')
_ = plt.setp(p.get_xticklabels(), rotation=0)
plt.title('父母学历对子女成绩影响2--合格人数',loc='left',size=15)
plt.xlabel('父母学历',size=15)
plt.ylabel('考试合格或不合格人数',size=15)
plt.xticks(size=12)
plt.yticks(size=12)#plt.yticks([]) 可以去掉y轴
plt.grid(False)
sns.despine(left=False )#去掉上面和右边边框
plt.show();
这个图知道做出来后,我才发现其实不太容易看出父母学历对子女成绩的影响,但既然做出来了,也就先放到这,当做练手了。
由图可知,无论国内还是国外,其实高学历人群还是少数,你看美国,家长是硕士学位的也是最少的。
5、父母学历对子女成绩影响3--及格率
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pyecharts import Bar,WordCloud,Pie,Line
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
df=pd.read_csv(r'C:\Users\Administrator\Desktop\StudentsPerformance.csv')
y1=df['math score']
y2=df['reading score']
y3=df['writing score']
df['总分']= y1 + y2 + y3
passmark =180
df['考试合格'] = np.where(df['总分']<passmark, 'N', 'Y')
df1=df.pivot_table('考试合格',index='parental level of education',aggfunc='count').reset_index()
df2=df[df['考试合格']=='Y']
df2=df2.pivot_table('考试合格',index='parental level of education',aggfunc='count').reset_index()
df3=pd.merge(df1,df2,on='parental level of education')
df3['合格率']=df3['考试合格_y']/df3['考试合格_x']
plt.rcParams['font.sans-serif']=['SimHei']
x=df3['parental level of education']
y=round(df3['合格率'],2)
plt.figure(figsize=(12,6))
plt.bar(x,y,width=0.5,align='center')
plt.title('父母学历对子女成绩影响3--合格率',loc='left',size=15)
for a,b in zip(x,y):
plt.text(a,b,b,ha='center',va='bottom',fontsize=12)#显示额度标签
plt.xlabel('父母学历',size=15)
plt.ylabel('合格率',size=15)
plt.xticks(x,size=12)
plt.yticks(size=15)#plt.yticks([]) 可以去掉y轴
plt.grid(False)
sns.despine(left=False )#去掉上面和右边边框
plt.show()
这个是不同学历家长的子女成绩合格率情况,一目了然就能看出来,学历越高的家长,子女成绩合格率越高。
虽然图是做出来了,但是代码略显繁琐,大家可以批判阅读,先通过这种方法做出来,再想其他更简单的方法。
python培训班:http://www.baizhiedu.com/python2019
注释:文章来自公众号POINT小数点数据