Error_message: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I'm having a problem with my code: when I make a search in the i-th column of the matrix it gives me the following error:
if grades[:,i]!=-3: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I have tried to read the older posts on this issue, but can't really relate to my problem
I am trying to program a grading function according to danish grading-scale
def computeFinalGrades(grades): #looping over the leth of grades for i in range(len(loopingrange)): #checking if any grade in the i-th coloumn is -3 if grades[:,i]==-3: #if a grade is equal to -3 then the output variabel gradesFinal is -3 gradesFinal = -3 else: #looping over all grade coloumns that do not contain one or more grades of -3 for i in range(len(loopingrange)): #Checking to see if any coloumn only contains 1 grade if (len(grades[:,i]) == 1): # if a coloumn only contains 1 grade - that is also the final grade gradesFinal = grades[:,i] #Checking to see if coloumn contains more than 1 grade and those grades are not -3 elif (len(grades[:,i]) > 1): #storing all the grades in a variabel - in random order unsortedgrades = grades[:,i] #sorting all the grades from lowest to highest sortedgrades1 = np.sort(unsortedgrades) #slicing the lowest grade out using indexing sortedgrades = sortedgrades1[1::] #finding the final grade as the mean of the n-1 grades gradesFinal = np.mean(sortedgrades) return gradesFinal
grades[:,i]returns the ith column of the array. This means that it is an array with one element for each row.
You cannot use
grades[:,i]==-3returns a boolean array, which cannot be used in an if statement.
If you want to check if any grade in that column == -3, you can use
-3 in grades[:,i].