Reputation: 129
So I am working with numpy
and I've been asked to create a function that returns the input "grades of students" as the average of the grades in a map without the use of a loop, and the only thing that came to my mind to make this possible is recursion
def hw_grade_average(_array):
condition=len(_array)
stop=condition
start=0
outp=[]
def calc(inp):
if stop-1==inp:
return outp
if stop!=inp:
calc=float(sum(_array[start]))/float(len(_array[start]))
outp.append(calc)
return calc(inp+1)
_returned = np.asarray(outp,dtype=float)
return calc(start)
in input for an example
hw_grade_average(hw_grades)
where
hw_grades=
array([[ 57, 99, 100, 81, 77],
[ 70, 91, 57, 77, 56],
[ 74, 89, 62, 100, 99],
[ 61, 53, 42, 65, 21],
[ 81, 65, 40, 37, 60],
[ 75, 88, 100, 92, 95]])
The output I am looking for is
array([82.8, 70.2, 84.8, 48.4, 56.5, 90. ])
but I get that an
TypeError: 'float' object is not callable
Upvotes: 0
Views: 82
Reputation: 16958
How about result = np.average(hwgrades, axis=1)
? You will find the documentation here.
Upvotes: 3