鱼C论坛

 找回密码
 立即注册
查看: 1096|回复: 1

[已解决]Python应用实训1

[复制链接]
发表于 2024-1-15 09:11:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
学院教务处需要设计一款教学系统,该系统可以实现学生成绩的上传、修改和查询,并可以形成班级学生的数据分析报表,包括课程最高分,最低分,不及格人数,优秀率,合格率等信息,能通过数据可视化更直观展现班级学情状况,并将数据报表保存为CSV格式。基本要求如下:
(1)针对项目需求,设计解决方案。
(2)通过Numpy或Pandas随机生成班级原始数据CSV文件,原始数据学生人数不少于100条,课程信息不少于3门,成绩分布0-100分。
(3)能按需上传,修改,查询学生信息。
(4)能实现数据的可视化。
(5)将分析报表保存为CSV格式。
最佳答案
2024-1-15 09:12:25
针对这个需求,你可以按照以下步骤设计解决方案:

1. 创建一个学生类,包含学生的姓名、学号、课程成绩等信息。
2. 创建一个班级类,包含班级的名称、学生列表等信息。该类应该提供方法来添加学生、修改学生成绩、查询学生成绩等功能。
3. 使用Numpy或Pandas库,随机生成班级的原始数据,并将数据保存为CSV文件。
4. 创建一个数据分析类,包含对班级成绩进行统计和分析的方法,如计算最高分、最低分、不及格人数、优秀率、合格率等。
5. 使用Matplotlib库进行数据可视化,将班级成绩分布等信息以图表形式展示,例如直方图、饼图等。
6. 将数据分析报表保存为CSV格式,使用Python的CSV模块来实现文件的读写操作。

下面是一个简单的示例代码,帮助你开始实现这个教学系统:

  1. import random
  2. import numpy as np
  3. import pandas as pd
  4. import matplotlib.pyplot as plt

  5. class Student:
  6.     def __init__(self, name, student_id):
  7.         self.name = name
  8.         self.student_id = student_id
  9.         self.scores = {}

  10.     def set_score(self, course, score):
  11.         self.scores[course] = score

  12.     def get_score(self, course):
  13.         return self.scores.get(course)

  14.     def get_all_scores(self):
  15.         return self.scores

  16. class Class:
  17.     def __init__(self, name):
  18.         self.name = name
  19.         self.students = []

  20.     def add_student(self, student):
  21.         self.students.append(student)

  22.     def get_student(self, student_id):
  23.         for student in self.students:
  24.             if student.student_id == student_id:
  25.                 return student
  26.         return None

  27. class DataAnalysis:
  28.     def __init__(self, class_obj):
  29.         self.class_obj = class_obj
  30.    
  31.     def calculate_highest_score(self, course):
  32.         highest_score = -1
  33.         for student in self.class_obj.students:
  34.             score = student.get_score(course)
  35.             if score is not None and score > highest_score:
  36.                 highest_score = score
  37.         return highest_score

  38.     def calculate_lowest_score(self, course):
  39.         lowest_score = 101
  40.         for student in self.class_obj.students:
  41.             score = student.get_score(course)
  42.             if score is not None and score < lowest_score:
  43.                 lowest_score = score
  44.         return lowest_score

  45.     def calculate_failed_students(self, course):
  46.         count = 0
  47.         for student in self.class_obj.students:
  48.             score = student.get_score(course)
  49.             if score is not None and score < 60:
  50.                 count += 1
  51.         return count

  52.     def calculate_excellent_rate(self, course):
  53.         count = 0
  54.         for student in self.class_obj.students:
  55.             score = student.get_score(course)
  56.             if score is not None and score >= 90:
  57.                 count += 1
  58.         return count / len(self.class_obj.students) * 100

  59.     def calculate_pass_rate(self, course):
  60.         count = 0
  61.         for student in self.class_obj.students:
  62.             score = student.get_score(course)
  63.             if score is not None and score >= 60:
  64.                 count += 1
  65.         return count / len(self.class_obj.students) * 100

  66.     def data_visualization(self, course):
  67.         scores = []
  68.         for student in self.class_obj.students:
  69.             score = student.get_score(course)
  70.             if score is not None:
  71.                 scores.append(score)
  72.         plt.hist(scores, bins=10, range=(0, 100))
  73.         plt.title(f"Distribution of Scores for {course}")
  74.         plt.xlabel("Score")
  75.         plt.ylabel("Number of Students")
  76.         plt.show()

  77.     def save_report_as_csv(self, filename):
  78.         data = []
  79.         header = ["Name", "Student ID"]
  80.         for student in self.class_obj.students:
  81.             header += list(student.get_all_scores().keys())
  82.             break
  83.         data.append(header)
  84.         for student in self.class_obj.students:
  85.             row = [student.name, student.student_id]
  86.             for score in student.get_all_scores().values():
  87.                 row.append(score)
  88.             data.append(row)
  89.         df = pd.DataFrame(data)
  90.         df.to_csv(filename, index=False)

  91. # 随机生成班级的原始数据
  92. courses = ["Math", "English", "Python"]
  93. class_obj = Class("Class A")
  94. for i in range(1, 101):
  95.     name = f"Student{i}"
  96.     student_id = f"S{i}"
  97.     student = Student(name, student_id)
  98.     for course in courses:
  99.         score = random.randint(0, 100)
  100.         student.set_score(course, score)
  101.     class_obj.add_student(student)

  102. # 查询学生成绩
  103. student = class_obj.get_student("S1")
  104. if student is not None:
  105.     score = student.get_score("Math")
  106.     print(f"The score of {student.name} in Math is {score}")

  107. # 修改学生成绩
  108. student = class_obj.get_student("S1")
  109. if student is not None:
  110.     student.set_score("Math", 90)

  111. # 进行数据分析和可视化
  112. data_analysis = DataAnalysis(class_obj)
  113. highest_score_math = data_analysis.calculate_highest_score("Math")
  114. lowest_score_math = data_analysis.calculate_lowest_score("Math")
  115. failed_students_math = data_analysis.calculate_failed_students("Math")
  116. excellent_rate_english = data_analysis.calculate_excellent_rate("English")
  117. pass_rate_python = data_analysis.calculate_pass_rate("Python")
  118. data_analysis.data_visualization("Math")

  119. # 保存数据报表
  120. data_analysis.save_report_as_csv("class_report.csv")
复制代码


这个示例代码演示了如何创建学生类、班级类,随机生成班级的原始数据,并进行学生成绩的上传、修改和查询。然后,使用数据分析类对班级成绩进行统计和分析,并通过Matplotlib库将数据可视化展示。最后,将分析报表保存为CSV格式。你可以根据实际需求进行修改和扩展。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-1-15 09:12:25 | 显示全部楼层    本楼为最佳答案   
针对这个需求,你可以按照以下步骤设计解决方案:

1. 创建一个学生类,包含学生的姓名、学号、课程成绩等信息。
2. 创建一个班级类,包含班级的名称、学生列表等信息。该类应该提供方法来添加学生、修改学生成绩、查询学生成绩等功能。
3. 使用Numpy或Pandas库,随机生成班级的原始数据,并将数据保存为CSV文件。
4. 创建一个数据分析类,包含对班级成绩进行统计和分析的方法,如计算最高分、最低分、不及格人数、优秀率、合格率等。
5. 使用Matplotlib库进行数据可视化,将班级成绩分布等信息以图表形式展示,例如直方图、饼图等。
6. 将数据分析报表保存为CSV格式,使用Python的CSV模块来实现文件的读写操作。

下面是一个简单的示例代码,帮助你开始实现这个教学系统:

  1. import random
  2. import numpy as np
  3. import pandas as pd
  4. import matplotlib.pyplot as plt

  5. class Student:
  6.     def __init__(self, name, student_id):
  7.         self.name = name
  8.         self.student_id = student_id
  9.         self.scores = {}

  10.     def set_score(self, course, score):
  11.         self.scores[course] = score

  12.     def get_score(self, course):
  13.         return self.scores.get(course)

  14.     def get_all_scores(self):
  15.         return self.scores

  16. class Class:
  17.     def __init__(self, name):
  18.         self.name = name
  19.         self.students = []

  20.     def add_student(self, student):
  21.         self.students.append(student)

  22.     def get_student(self, student_id):
  23.         for student in self.students:
  24.             if student.student_id == student_id:
  25.                 return student
  26.         return None

  27. class DataAnalysis:
  28.     def __init__(self, class_obj):
  29.         self.class_obj = class_obj
  30.    
  31.     def calculate_highest_score(self, course):
  32.         highest_score = -1
  33.         for student in self.class_obj.students:
  34.             score = student.get_score(course)
  35.             if score is not None and score > highest_score:
  36.                 highest_score = score
  37.         return highest_score

  38.     def calculate_lowest_score(self, course):
  39.         lowest_score = 101
  40.         for student in self.class_obj.students:
  41.             score = student.get_score(course)
  42.             if score is not None and score < lowest_score:
  43.                 lowest_score = score
  44.         return lowest_score

  45.     def calculate_failed_students(self, course):
  46.         count = 0
  47.         for student in self.class_obj.students:
  48.             score = student.get_score(course)
  49.             if score is not None and score < 60:
  50.                 count += 1
  51.         return count

  52.     def calculate_excellent_rate(self, course):
  53.         count = 0
  54.         for student in self.class_obj.students:
  55.             score = student.get_score(course)
  56.             if score is not None and score >= 90:
  57.                 count += 1
  58.         return count / len(self.class_obj.students) * 100

  59.     def calculate_pass_rate(self, course):
  60.         count = 0
  61.         for student in self.class_obj.students:
  62.             score = student.get_score(course)
  63.             if score is not None and score >= 60:
  64.                 count += 1
  65.         return count / len(self.class_obj.students) * 100

  66.     def data_visualization(self, course):
  67.         scores = []
  68.         for student in self.class_obj.students:
  69.             score = student.get_score(course)
  70.             if score is not None:
  71.                 scores.append(score)
  72.         plt.hist(scores, bins=10, range=(0, 100))
  73.         plt.title(f"Distribution of Scores for {course}")
  74.         plt.xlabel("Score")
  75.         plt.ylabel("Number of Students")
  76.         plt.show()

  77.     def save_report_as_csv(self, filename):
  78.         data = []
  79.         header = ["Name", "Student ID"]
  80.         for student in self.class_obj.students:
  81.             header += list(student.get_all_scores().keys())
  82.             break
  83.         data.append(header)
  84.         for student in self.class_obj.students:
  85.             row = [student.name, student.student_id]
  86.             for score in student.get_all_scores().values():
  87.                 row.append(score)
  88.             data.append(row)
  89.         df = pd.DataFrame(data)
  90.         df.to_csv(filename, index=False)

  91. # 随机生成班级的原始数据
  92. courses = ["Math", "English", "Python"]
  93. class_obj = Class("Class A")
  94. for i in range(1, 101):
  95.     name = f"Student{i}"
  96.     student_id = f"S{i}"
  97.     student = Student(name, student_id)
  98.     for course in courses:
  99.         score = random.randint(0, 100)
  100.         student.set_score(course, score)
  101.     class_obj.add_student(student)

  102. # 查询学生成绩
  103. student = class_obj.get_student("S1")
  104. if student is not None:
  105.     score = student.get_score("Math")
  106.     print(f"The score of {student.name} in Math is {score}")

  107. # 修改学生成绩
  108. student = class_obj.get_student("S1")
  109. if student is not None:
  110.     student.set_score("Math", 90)

  111. # 进行数据分析和可视化
  112. data_analysis = DataAnalysis(class_obj)
  113. highest_score_math = data_analysis.calculate_highest_score("Math")
  114. lowest_score_math = data_analysis.calculate_lowest_score("Math")
  115. failed_students_math = data_analysis.calculate_failed_students("Math")
  116. excellent_rate_english = data_analysis.calculate_excellent_rate("English")
  117. pass_rate_python = data_analysis.calculate_pass_rate("Python")
  118. data_analysis.data_visualization("Math")

  119. # 保存数据报表
  120. data_analysis.save_report_as_csv("class_report.csv")
复制代码


这个示例代码演示了如何创建学生类、班级类,随机生成班级的原始数据,并进行学生成绩的上传、修改和查询。然后,使用数据分析类对班级成绩进行统计和分析,并通过Matplotlib库将数据可视化展示。最后,将分析报表保存为CSV格式。你可以根据实际需求进行修改和扩展。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 07:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表