python - NumPy - 最高平均值的索引

标签 python numpy

我有以下值:

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/

相关文章:

python - 在Django中,如何获取通过外键连接的子对象?

python - 长时间运行过程中 numpy tolist() 的明显内存泄漏

python - Numpy:加入结构化数组?

performance - Pandas 或 scipy : looking for performance 中的 groupby.sum() 稀疏矩阵

python - 我可以发送 **kwargs 来代替 requests.post 中的设置参数吗?

python - 区 block 链信息钱包支票付款

python - ModuleNotFoundError : No module named 'sklearn.externals.six'

python - 从上一行/值 python 添加值

Python:用于元组的 Pandas DataFrame

python - 删除Python中的空格