Here I tried to return an array in letter grade, but it returns only a letter Grade. I need to get the whole numpz array in letter grade numpy array. I used a for loop, but since the first element is completed, it doesnt continue more.
import numpy as np
from scipy import stats
marks = np.array([90,72,82,90,69,19,23,30,45,5])
#Average of the array
avg = np.average(marks)
mdn = np.median(marks)
stand_dev = np.std(marks)
rang = np.ptp(marks)
mode = stats.mode(marks)
mode = mode[0]
# the loop that turns marks into letter Grade array
def letterGrade(mark):
for i in range (len(mark)):
list_new = []
score = mark[i]
if score <= avg:
return "F"
elif (( score > avg) & ( score <= mdn)):
return "D"
elif (( score > mdn) & (score <= stand_dev)):
return "C"
elif (( score > stand_dev) & ( score <= rang)):
return "B"
elif (( score > rang) & ( score <= mode)):
return "A"
elif score > mode:
return "A "
list_new.append(score)
letterGrade(marks)
CodePudding user response:
This can be elegantly solved with digitize
:
letters = np.array(['F', 'D', 'C', 'B', 'A', 'A '])
bins = [avg, mdn, avg stand_dev, rang, mode[0], 100]
letters[np.digitize(marks, bins, True)].tolist()
Result
['A', 'C', 'C', 'A', 'C', 'F', 'F', 'F', 'F', 'F']
(Note that I replaced stand_dev
with avg stand_dev
so that it makes sense)
CodePudding user response:
def letterGrade(mark):
for i in range (len(mark)):
list_new = []
score = mark[i]
if score <= avg:
list_new.append("F")
elif (( score > avg) & ( score <= mdn)):
list_new.append("D")
elif (( score > mdn) & (score <= stand_dev)):
list_new.append("C")
elif (( score > stand_dev) & ( score <= rang)):
list_new.append("B")
elif (( score > rang) & ( score <= mode)):
list_new.append("A")
elif score > mode:
list_new.append("A ")
return list_new
letterGrade(marks)