我的代码出现问题:当我在矩阵的第i列进行搜索时,它会出现以下错误:
if grades[:,i]!=-3:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我曾尝试阅读有关此问题的较旧帖子,但无法真正解决我的问题
我正在尝试根据丹麦评分等级来编写评分函数
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
答案 0 :(得分:1)
grades[:,i]
返回数组的第i列。这意味着它是一个数组,每行有一个元素。
您不能使用if grades[:,i]==-3:
,因为grades[:,i]==-3
返回一个布尔数组,不能在if语句中使用。
如果您想检查该列中的任何成绩== -3,您可以使用-3 in grades[:,i]
。