当前位置:   article > 正文

机器学习实战4-教育领域:学生成绩的可视化分析与成绩预测-详细分析_如何通过深度学习方式预测学生的学习成绩

如何通过深度学习方式预测学生的学习成绩

大家好,我是微学AI,今天给大家带来机器学习实战4-学生成绩的可视化分析与成绩预测,机器学习在教育中的应用具有很大的潜力,特别是在学生成绩的可视化分析与成绩预测方面。

机器学习可以通过对学生的父母教育情况和学校表现等数据进行分析和挖掘,从而揭示潜在的学习模式和趋势。这种可视化分析可以帮助教师更好地了解学生的学习状况,并针对性地调整教学策略。机器学习还可以利用学生的历史数据、课程表、出勤记录等信息,建立模型来预测学生未来的成绩。这种预测可以帮助教师及时发现学生可能存在的问题并采取相应的措施加以干预,从而提高学生的学习效果和成绩。

一、导入库和数据

  1. import numpy as np
  2. import pandas as pd
  3. import seaborn as sns
  4. import matplotlib.pyplot as plt
  5. from sklearn.svm import SVR
  6. from sklearn.linear_model import LinearRegression
  7. from sklearn.tree import DecisionTreeRegressor
  8. from sklearn.ensemble import RandomForestRegressor
  9. from sklearn.model_selection import cross_val_score
  10. from sklearn.model_selection import train_test_split
  11. from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
  12. plt.rcParams['font.sans-serif'] = ['SimHei']
  13. df_pre = pd.read_csv('exams.csv')
  14. df_pre[['math score', 'reading score', 'writing score']].agg(['var', 'std'])
  15. correlation_matrix = df_pre.corr()

数据样例:

3e97413a2eb34e9e9653a5bb80f95bbe.png

二、创建一个热图的相关矩阵

  1. sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
  2. plt.show()

7bdf056042234a6683cb067ad0d6d0e9.png

 三、学生父母的教育水平和成绩之间的关系

  1. education_score = df_pre.groupby('parental level of education')[['math score', 'reading score', 'writing score']].mean().reset_index()
  2. education_score['average score'] = (education_score['math score']+education_score['reading score']+education_score['writing score'])/3
  3. education_score = education_score.sort_values('average score', ascending=False)
  4. plt.figure(figsize=(13,4))
  5. plt.plot(education_score['parental level of education'], education_score['math score'], marker='o', label='Math Score')
  6. plt.plot(education_score['parental level of education'], education_score['reading score'], marker='o', label='Reading Score')
  7. plt.plot(education_score['parental level of education'], education_score['writing score'], marker='o', label='Writing Score')
  8. plt.plot(education_score['parental level of education'], education_score['average score'], marker='s', label='Average Score')
  9. plt.title('学生父母的教育水平和成绩之间的关系')
  10. plt.xlabel('教育水平')
  11. plt.ylabel('成绩')
  12. plt.legend()
  13. plt.show()

dfc390d357554ca7bc2ffe5792b8458c.png

 四、种族和成绩之间的关系

  1. race_score = df_pre.groupby('race/ethnicity')[['math score', 'reading score', 'writing score']].mean().reset_index()
  2. race_score['average score'] = (race_score['math score']+race_score['reading score']+race_score['writing score'])/3
  3. race_score = race_score.sort_values('average score', ascending=False)
  4. plt.figure(figsize=(13,4))
  5. plt.plot(race_score['race/ethnicity'], race_score['math score'], marker='o', label='Math Score')
  6. plt.plot(race_score['race/ethnicity'], race_score['reading score'], marker='o', label='Reading Score')
  7. plt.plot(race_score['race/ethnicity'], race_score['writing score'], marker='o', label='Writing Score')
  8. plt.plot(race_score['race/ethnicity'], race_score['average score'], marker='s', label='Average Score')
  9. plt.title('种族和成绩之间的关系')
  10. plt.xlabel('种族')
  11. plt.ylabel('成绩')
  12. plt.legend()
  13. plt.show()

fef91abf7d044c638372eca609afb18d.png

五、测试准备课程和成绩之间的关系

  1. prep_score = df_pre.groupby('test preparation course')[['math score', 'reading score', 'writing score']].mean().reset_index()
  2. prep_score['average score'] = (prep_score['math score']+prep_score['reading score']+prep_score['writing score'])/3
  3. prep_score = prep_score.sort_values('average score', ascending=False)
  4. plt.figure(figsize=(13,4))
  5. plt.plot(prep_score['test preparation course'], prep_score['math score'], marker='o', label='Math Score')
  6. plt.plot(prep_score['test preparation course'], prep_score['reading score'], marker='o', label='Reading Score')
  7. plt.plot(prep_score['test preparation course'], prep_score['writing score'], marker='o', label='Writing Score')
  8. plt.plot(prep_score['test preparation course'], prep_score['average score'], marker='s', label='Average Score')
  9. plt.title('测试准备课程和成绩之间的关系')
  10. plt.xlabel('完成与否')
  11. plt.ylabel('成绩')
  12. plt.legend()
  13. plt.show()

820208a93a6646f7815f7be6d85a6bae.png

 六、父母的教育水平/学生是否完成测试准备课程的饼图

  1. df_pre.groupby('test preparation course')[['math score', 'reading score', 'writing score']].agg(['var', 'std'])
  2. par_test_count = df_pre[['parental level of education', 'test preparation course']].value_counts().to_frame().reset_index().rename(columns={0:'Count'}).sort_values('Count', ascending=False)
  3. # Create a figure with two subplots
  4. fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15,4))
  5. # Create the first pie chart for the count of students who completed the test preparation course
  6. ax1.pie(par_test_count[par_test_count['test preparation course']=='completed']['Count'],
  7. labels=par_test_count[par_test_count['test preparation course']=='completed']['parental level of education'],
  8. autopct='%1.2f%%')
  9. ax1.set_title('父母的教育水平的饼图 学生完成测试准备课程')
  10. # Create the second pie chart for the count of students who did not complete the test preparation course
  11. ax2.pie(par_test_count[par_test_count['test preparation course']=='none']['Count'],
  12. labels=par_test_count[par_test_count['test preparation course']=='none']['parental level of education'],
  13. autopct='%1.2f%%')
  14. ax2.set_title('父母的教育水平的饼图 学生没有完成测试准备课程')
  15. # Show the plot
  16. plt.show()

9d8abb680cb446da8b3bc6c651d0c972.png

 七、比较男性和女性之间的数学分数

  1. df_pre.groupby('gender').mean()
  2. sns.violinplot(x='gender', y='math score', data=df_pre)
  3. # Add labels and title
  4. plt.xlabel('Gender')
  5. plt.ylabel('Math Score')
  6. plt.title('比较男性和女性之间的数学分数')
  7. # Show the plot
  8. plt.show()

52570e5c672641b781d404d1cee4dc3b.png

 八、基于性别数学分数的散点图

  1. plt.figure(figsize=(10,5))
  2. sns.scatterplot(x=range(0, len(df_pre)), y="math score", hue="gender", data=df_pre)
  3. # Add labels and title
  4. plt.title('基于性别数学分数的散点图')
  5. plt.xlabel('学生数')
  6. plt.ylabel('成绩')
  7. # Show the plot
  8. plt.show()

3e6ca4f859dc42e781af2eee9f58cf14.png

 九、学生各科成绩分布图

  1. fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(20, 4))
  2. # Plot for math
  3. ax1.set_title('数学成绩的分布')
  4. ax1.hist(df_pre['math score'], edgecolor='black')
  5. # Plot for reading
  6. ax2.set_title('阅读成绩的分布')
  7. ax2.hist(df_pre['reading score'], edgecolor='black')
  8. # Plot for writing
  9. ax3.hist(df_pre['writing score'], edgecolor='black')
  10. ax3.set_title('写作成绩的分布')
  11. # Show plots
  12. plt.show()

80d5f0debc47446aafc92ee4af25104f.png

 十、机器学习模型比较

  1. df = pd.get_dummies(df_pre)
  2. # Assign variables
  3. X = df.drop('math score', axis=1)
  4. y = df['math score']
  5. # Split the data into training and testing sets
  6. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)
  7. models = [LinearRegression(), DecisionTreeRegressor(), RandomForestRegressor(), SVR(kernel='linear'), SVR(kernel='poly'), SVR(kernel='rbf')]
  8. # Use cross-validation to compute the R-squared score for each model
  9. cv_scores = []
  10. for model in models:
  11. scores = cross_val_score(model, X_train, y_train, cv=5, scoring='r2', n_jobs=-1)
  12. cv_scores.append(scores.mean())
  13. # Plot the results
  14. fig, ax = plt.subplots(figsize=(15, 6))
  15. rects = ax.bar(['Linear', 'Decision Tree', 'Random Forest', 'SVR - Linear', 'SVR - Poly', 'SVR - Rbf'], cv_scores, color='orange')
  16. ax.set_ylim(0, 1)
  17. ax.set_title('回归模型的比较')
  18. ax.set_xlabel('Model')
  19. ax.set_ylabel('R-squared')
  20. # Add labels above each bar
  21. for rect in rects:
  22. height = rect.get_height()
  23. ax.text(rect.get_x() + rect.get_width()/2., height, f'{height:.5f}', ha='center', va='bottom')
  24. # Show the plot
  25. plt.show()

755cc37e61c741019cce753e3b5f4e03.png

 欢迎大家持续关注,更多机器学习与深度学习的实战案例。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/814671
推荐阅读
相关标签
  

闽ICP备14008679号