我在这里是因为我有一个关于函数numpy.where的问题。 我需要开发一个程序,用丹麦评分表对学生的成绩进行评分
(丹麦评分标准为7级,从最好的(12)到最差的(-3):1210740200−(三)
以下是等级数组:
grades=np.array([[-3,-2,-1,0],[1,2,3,4],[5,6,7,8],[9,10,11,12]])
我想做的是:
gradesrounded=np.where(grades<-1.5, -3, grades)
gradesrounded=np.where(-1.5<=grades and grades<1, 0, grades)
gradesrounded=np.where(grades>=1 and grades<3, 2, grades)
gradesrounded=np.where(grades>=3 and grades<5.5, 4, grades)
gradesrounded=np.where(grades>=5.5 and grades<8.5, 7, grades)
gradesrounded=np.where(grades>=8.5 and grades<11, 10, grades)
gradesrounded=np.where(grades>=11, 12, grades)
print(gradesrounded)
我发现np.where在有一个条件时有效(例如,低于-1.5的等级和高于11的等级),但如果有两个不同的条件(例如,这个:np.where(等级>;=1和等级<;3,2,等级))则不起作用
你知道我怎样才能解决这个问题吗
多谢各位
您使用的逻辑运算符
and
不适用于数组操作。改为使用逐元素操作的位运算符np.where((grades>=1) & (grades<3), 2, grades))
看看这个:link
另一种方法是
np.searchsorted
:输出:
这是
np.select()
函数的一个很好的例子The docs can be found here设置很简单:
list
丹麦系统等级李>list
个映射。下面的案例使用逻辑and&
运算符链接多个条件李>设置:
用法:
调用
np.select
函数并传入上面创建的两个列表输出:
相关问题 更多 >
编程相关推荐