Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculating average without min value, array

Tags:

python

arrays

I have this array

 hw_grades=array([[ 57,  77,  81,  99, 100],
                  [ 56,  57,  70,  77,  91],
                  [ 62,  74,  89,  99, 100],
                  [ 21,  42,  53,  61,  65],
                  [ 37,  40,  60,  65,  81],
                  [ 75,  88,  92,  95, 100]])

I want to be able to return the average of each row without the lowest value (I already used sort() and so basically I wanna get rid of the first element of each row and then do the average

 def hw_grade_best(_array):
    _array.sort()
    _array[::][1:]
    result = np.average(_array, axis=1)
    return result

not really much of a code and the _array[::][1:] isn't really doing anything cause I can assume by far I am using it wrong

input
hw_grade_best(hw_grades)

output I want

array([ 89.25, 73.75, 90.5 , 55.25, 61.5 , 93.75])

like image 219
Enigma Avatar asked Feb 18 '26 19:02

Enigma


1 Answers

You need to slice the array to remove the value, then you can apply the mean function across the axis:

hw_grades[:, 1:].mean(axis=1)

# array([ 89.25, 73.75, 90.5 , 55.25, 61.5 , 93.75])

like image 90
Greg Jennings Avatar answered Feb 21 '26 09:02

Greg Jennings