鱼C论坛

 找回密码
 立即注册
查看: 604|回复: 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模块来实现文件的读写操作。

下面是一个简单的示例代码,帮助你开始实现这个教学系统:
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

class Student:
    def __init__(self, name, student_id):
        self.name = name
        self.student_id = student_id
        self.scores = {}

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

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

    def get_all_scores(self):
        return self.scores

class Class:
    def __init__(self, name):
        self.name = name
        self.students = []

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

    def get_student(self, student_id):
        for student in self.students:
            if student.student_id == student_id:
                return student
        return None

class DataAnalysis:
    def __init__(self, class_obj):
        self.class_obj = class_obj
    
    def calculate_highest_score(self, course):
        highest_score = -1
        for student in self.class_obj.students:
            score = student.get_score(course)
            if score is not None and score > highest_score:
                highest_score = score
        return highest_score

    def calculate_lowest_score(self, course):
        lowest_score = 101
        for student in self.class_obj.students:
            score = student.get_score(course)
            if score is not None and score < lowest_score:
                lowest_score = score
        return lowest_score

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

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

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

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

    def save_report_as_csv(self, filename):
        data = []
        header = ["Name", "Student ID"]
        for student in self.class_obj.students:
            header += list(student.get_all_scores().keys())
            break
        data.append(header)
        for student in self.class_obj.students:
            row = [student.name, student.student_id]
            for score in student.get_all_scores().values():
                row.append(score)
            data.append(row)
        df = pd.DataFrame(data)
        df.to_csv(filename, index=False)

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

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

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

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

# 保存数据报表
data_analysis.save_report_as_csv("class_report.csv")

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

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

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

下面是一个简单的示例代码,帮助你开始实现这个教学系统:
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

class Student:
    def __init__(self, name, student_id):
        self.name = name
        self.student_id = student_id
        self.scores = {}

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

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

    def get_all_scores(self):
        return self.scores

class Class:
    def __init__(self, name):
        self.name = name
        self.students = []

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

    def get_student(self, student_id):
        for student in self.students:
            if student.student_id == student_id:
                return student
        return None

class DataAnalysis:
    def __init__(self, class_obj):
        self.class_obj = class_obj
    
    def calculate_highest_score(self, course):
        highest_score = -1
        for student in self.class_obj.students:
            score = student.get_score(course)
            if score is not None and score > highest_score:
                highest_score = score
        return highest_score

    def calculate_lowest_score(self, course):
        lowest_score = 101
        for student in self.class_obj.students:
            score = student.get_score(course)
            if score is not None and score < lowest_score:
                lowest_score = score
        return lowest_score

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

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

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

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

    def save_report_as_csv(self, filename):
        data = []
        header = ["Name", "Student ID"]
        for student in self.class_obj.students:
            header += list(student.get_all_scores().keys())
            break
        data.append(header)
        for student in self.class_obj.students:
            row = [student.name, student.student_id]
            for score in student.get_all_scores().values():
                row.append(score)
            data.append(row)
        df = pd.DataFrame(data)
        df.to_csv(filename, index=False)

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

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

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

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

# 保存数据报表
data_analysis.save_report_as_csv("class_report.csv")

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-22 01:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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