学生成绩分析项目

数据采集

导入必要的库

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

加载数据集

df = pd.read_csv('D:\\桌面\\数据\\student_marks.csv')

显示数据框的前几行
在这里插入图片描述

# 显示数据框的形状
print("Shape of the dataframe:", df.shape)

#显示列名称
print("\nColumns in the dataframe:", df.columns)

# 显示每列的数据类型
print("\nData types of the columns:")
print(df.dtypes)

# 显示每列的摘要统计信息
print("\nSummary statistics:")
print(df.describe())

在这里插入图片描述
在这里插入图片描述

数据加载和探索

# 计算每个测试的描述性统计数据
test_stats = df.describe()

# 计算每次测试的平均值
test_means = df.mean()

# 确定平均分数最高和最低的测试
highest_avg_test = test_means.idxmax()
lowest_avg_test = test_means.idxmin()
#打印最高和最低平均考试成绩
print("Test with the highest average score:", highest_avg_test)
print("Test with the lowest average score:", lowest_avg_test)

在这里插入图片描述
使用直方图可视化每个测试的分数分布


fig, axes = plt.subplots(nrows=3, ncols=4, figsize=(16, 12))

for i, col in enumerate(df.columns[1:]):
    ax = axes[i // 4, i % 4]
    df[col].plot(kind='hist', ax=ax, title=col)
    ax.set_xlabel('Score')
    ax.set_ylabel('Frequency')

plt.tight_layout()
plt.show()

在这里插入图片描述
使用箱线图可视化每个测试的分数分布


fig, axes = plt.subplots(nrows=3, ncols=4, figsize=(16, 12))

for i, col in enumerate(df.columns[1:]):
    ax = axes[i // 4, i % 4]
    df[col].plot(kind='box', ax=ax, vert=False, title=col)
    ax.set_xlabel('Score')

plt.tight_layout()
plt.show()

在这里插入图片描述

个人测试成绩分析

# 计算每次测试的平均分
test_means = df.mean()

# 创建测试名称列表
test_names = df.columns[1:]

使用折线图绘制测试中的分数趋势


plt.figure(figsize=(10, 6))
plt.plot(test_names, test_means[1:], marker='o')
plt.title('Trend of Scores Across Tests')
plt.xlabel('Test')
plt.ylabel('Mean Score')
plt.xticks(rotation=45)
plt.grid(True)
plt.show()

在这里插入图片描述
使用条形图绘制测试中的分数趋势


plt.figure(figsize=(10, 6))
plt.bar(test_names, test_means[1:])
plt.title('Trend of Scores Across Tests')
plt.xlabel('Test')
plt.ylabel('Mean Score')
plt.xticks(rotation=45)
plt.grid(True)
plt.show()

在这里插入图片描述

趋势分析

# 计算相关矩阵
correlation_matrix = df.corr()

#使用热图可视化相关矩阵
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f")
plt.title('Correlation Matrix of Test Scores')
plt.show()

在这里插入图片描述

报告

# 审查和完善

# 识别并处理丢失或不一致的数据
#检查缺失值
missing_values = df.isnull().sum()
print("\nMissing Values:\n", missing_values)

# 处理缺失值(例如:用平均值填充)
df_filled = df.fillna(df.mean())

# 使用精炼数据重新计算描述性统计数据和趋势
refined_test_stats = df_filled.describe()
refined_test_means = df_filled.mean()

# 用精炼后的数据重新计算相关矩阵
refined_correlation_matrix = df_filled.corr()

# 使用精炼的分析结果查看并更新报告 精炼报告 = '''
# 学生考试成绩分析报告(精炼版)

## 数据集概述

该数据集包含有关学生在 12 项测试中的成绩的信息。

- Number of students: {
    
    }
- Number of tests: {
    
    }

## 分析结果(精炼)

### 描述性统计

每个测试的描述性统计:

{
    
    }

### 趋势分析

各测试的分数趋势:

![Trend of Scores](trend_of_scores.png)

### 模式识别

测试成绩的相关矩阵:

![Correlation Matrix](correlation_matrix.png)

## 结论

基于对数据集的精细分析,可以突出以下观察结果和见解:
- The highest average score is obtained in the test: {
    
    }
- The lowest average score is obtained in the test: {
    
    }
- T测试分数显示测试 X 和 Y 之间存在正/负相关性,表明存在潜在关系。

可以进行进一步的分析和探索,以获得对数据集更深入的了解。

'''

# 保存细化的趋势分析图
plt.figure(figsize=(10, 6))
plt.plot(test_names, refined_test_means[1:], marker='o')
plt.title('Refined Trend of Scores Across Tests')
plt.xlabel('Test')
plt.ylabel('Mean Score')
plt.xticks(rotation=45)
plt.grid(True)
plt.savefig('refined_trend_of_scores.png')

# 保存细化的相关矩阵热图
plt.figure(figsize=(10, 8))
sns.heatmap(refined_correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f")
plt.title('Refined Correlation Matrix of Test Scores')
plt.savefig('refined_correlation_matrix.png')

# 使用精炼的分析结果更新精炼的报告
refined_report = refined_report.format(df_filled.shape[0], df_filled.shape[1] - 1, refined_test_stats.to_string(), highest_avg_test, lowest_avg_test)

# 将精炼后的报告保存为 Markdown 文件
with open('refined_student_scores_report.md', 'w') as f:
    f.write(refined_report)

猜你喜欢

转载自blog.csdn.net/m0_66106755/article/details/131623191