Reputation: 24492
I have the following values:
grade_list = [[99 73 97 98] [98 71 70 99]]
student_ids = [521 597 624 100]
Each array in the grade_list
represents a list of grades for each student (student #521 grades are 99, 98, student #597 grades are 73, 71, and so on..)
My goal is to return the student ID of the one with the highest average grade (should be 521
since the average is 98.5 which is the highest)
I have tried this:
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
which returned 521
, but I have tried to play with the grade_list and I see that even when I set this student grades to 0, it still return
521`.
not sure how to get the student ID of the best student, any idea?
Upvotes: 3
Views: 186
Reputation: 10819
I would use Counter for this.
from collections import Counter
import numpy as np
grade_list = [[99, 73, 97, 98], [98, 71, 70, 99]]
student_ids = [521, 597, 624, 100]
c = Counter()
for i,n in enumerate(student_ids):
c.update({n:np.mean([item[i] for item in grade_list])})
c.most_common()
[(521, 98.5), (100, 98.5), (624, 83.5), (597, 72.0)]
As you see, you get 2 best students in this case.
In [17]: best_score = c.most_common(1)[0][1]
In [18]: best_score
Out[18]: 98.5
In [19]: best_students = [k for k,v in c.items() if v == best_score]
In [20]: best_students
Out[20]: [521, 100]
Upvotes: 0
Reputation: 8302
try this,
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()]
Upvotes: 2
Reputation: 1822
You are taking the mean along the wrong axis:
>>> np.mean(grade_list, axis=1)
array([91.75, 84.5 ])
Change the axis, and check that you get as many mean values as there are student IDs.
>>> np.mean(grade_list, axis=0)
array([98.5, 72. , 83.5, 98.5])
Upvotes: 1