我有以下值:
grade_list = [[99 73 97 98] [98 71 70 99]]
student_ids = [521 597 624 100]
grade_list
中的每个数组代表每个学生的成绩列表(学生 #521 的成绩是 99、98,学生 #597 的成绩是 73、71,等等。)
我的目标是返回平均成绩最高的学生 ID(应该是 521
,因为平均成绩是 98.5,这是最高的)
我试过这个:
def find_student_with_max_avg(grade_list, student_ids):
mean_grades = np.mean(grade_list, axis=1)
best_student_index = student_ids[mean_grades.argmax()]
return best_student_index
它返回了 521
,但我尝试使用 grade_list,我发现即使我将这个学生的成绩设置为 0,它仍然返回
521`。
不确定如何获得最优秀学生的学号,知道吗?
最佳答案
试试这个,
import numpy as np
grade_list = [[99,73,97,98] ,[98,71,70,99]]
student_ids = [521,597,624,100]
# axis 0 is row-wise, and 1 is column-wise
student_ids[np.array(grade_list).mean(axis=0).argmax()]
关于python - NumPy - 最高平均值的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62621861/