简体   繁体   English

numpy.where 有多个条件

[英]numpy.where with more than one condition

I am here because I have a question with the function numpy.where.我来这里是因为我对 numpy.where 函数有疑问。 I need to develop a program that rounds the grades of a student in the danish grading scale.我需要开发一个程序,在丹麦评分标准中对学生的成绩进行四舍五入。

(Danish grading scale is a 7-step-scale from the best one (12) to the worst one (-3) : 12 10 7 4 02 00 −3) (丹麦评分标准是从最好的 (12) 到最差的 (-3) 的 7 个等级:12 10 7 4 02 00 −3)

Here is the array of the grades :这是成绩的数组:

grades=np.array([[-3,-2,-1,0],[1,2,3,4],[5,6,7,8],[9,10,11,12]])

and what I am trying to do is this :我想要做的是:

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)

and what I found out is that np.where works when there is one condition (so grades below -1.5 works and grades over 11 works for example) but if there are 2 different conditions (for example this one : np.where(grades>=1 and grades<3, 2, grades)) it won't work.我发现 np.where 在有一个条件时有效(例如,低于 -1.5 的成绩和超过 11 的成绩)但如果有 2 个不同的条件(例如这个: np.where(grades> =1 and grades<3, 2, grades)) 不行。

Do you know how I could fix this ?你知道我怎么解决这个问题吗?

Thank you very much.非常感谢。

You are using the logical operator and which doesn't work for array operations.您正在使用逻辑运算符and它不适用于数组操作。 Use bitwise operators instead that will operate element by element.使用按位运算符来代替逐个元素操作。

np.where((grades>=1) & (grades<3), 2, grades))

Have a look at this: link看看这个:链接

Another way is np.searchsorted :另一种方法是np.searchsorted

scales = np.array([-3,0,2,4,7,10,12])

grades=np.array([[-3,-2,-1,0],[1,2,3,4],[5,6,7,8],[9,10,11,12]])

thresh = [-1.5, 0.5 ,2.5,5.5,8.5,10]
out = scales[np.searchsorted(thresh, grades)]

# or
# thresh = [-3, -1.5, 1, 3, 5.5, 8.5, 11]
# out = scales[np.searchsorted(thresh, grades, side='right')-1]

Out:出去:

array([[-3, -3,  0,  0],
       [ 2,  2,  4,  4],
       [ 4,  7,  7,  7],
       [10, 10, 12, 12]])

This is an excellent case for the np.select() function.这是np.select()函数的一个很好的例子。 The docs can be found here . 文档可以在这里找到

The setup is simple:设置很简单:

  • Create a list of Danish system grades.创建丹麦系统等级list
  • Create a list of mappings.创建映射list The case below uses the logical and & operator to link multiple conditions.下面的案例使用逻辑和&运算符来链接多个条件。

Setup:设置:

import numpy as np

# Sample grades.
x = np.array([-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

# Define limits and lookup.
grades = [12, 10, 7, 4, 2, 0, -3]
scale = [(x >= 11), 
         (x >= 8.5) & (x < 11),
         (x >= 5.5) & (x < 8.5),
         (x >= 3.0) & (x < 5.5),
         (x >= 1.0) & (x < 3.0),
         (x >= -1.5) & (x < 1.0 ),
         (x < -1.5)]

Use:用:
Call the np.select function and pass in the two lists created above.调用np.select函数并传入上面创建的两个列表。

# Map grades to Danish system.
np.select(condlist=scale, choicelist=grades)

Output:输出:

array([-3, -3,  0,  0,  2,  2,  4,  4,  4,  7,  7,  7, 10, 10, 12, 12])

If you want to put it as a string you could also use a pandas dataframe and the query function on it.如果你想把它作为一个字符串,你也可以使用一个 Pandas 数据框和它的查询函数。 Here is an example:下面是一个例子:

df = df.query('grades>=1 & grades<3')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM