python中的select()以及choose()的用法

转自
一、什么是np.select()
顾名思义,这个函数用用来“ 根据某一些条件 ” 来筛选出 “某一些元素 ”的函数,比如我有一个数组,我如果用if-else语句去做,当然也可以,比如我们让小于6的元素各自加上10,大于等于6的元素统统变为100,我们可以这么做,代码如下:

a=np.array([1,2,3,4,5,6,7,8,9,10])
aa=[]
for i in a:
    if (i<6):
        i=i+10
    else:
        i=100
    aa.append(i)
print(aa)

运行结果为 [11, 12, 13, 14, 15, 100, 100, 100, 100, 100]

但是这样做的缺点就是效率太过于低下,因为这样做就是使用循环,条件判断去完成,select()函数就是专门针对这种情况提出来的。

1、select函数的定义

 def select(condlist, choicelist, default=0):

 condlist参数:操作数据所依据的条件

 choicelist参数:根据condlist条件,索要执行的操作

  返回值:返回的是一个“ 列表” 。

注意上面的condlist和choicelist都必须是写成“ 列表 ”的形式。

 要实现上面同样的操作,这里只需要一句话就可以完成

a=np.array([1,2,3,4,5,6,7,8,9,10])

condlist=[a<6] #第一个参数,必须用【】括起来,列表形式

print(condlist)

choicelist=[a+10] #第二个参数,必须用【】括起来,列表形式

print(choicelist)

aa=np.select(condlist,choicelist,default=100)

print(aa)

程序运行的结果为

[array([ True, True, True, True, True, False, False, False, False,False])]
[array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20])]
[11 12 13 14 15 100 100 100 100 100] #由此可见,依然得到的是上面的结果。

什么意思呢?可以这样理解,对于第一个参数condlist=[ a<6 ],我们将a<6看成是一个条件,只不过这个条件是针对array类型的a的,第二个参数choicelist所要执行的操作是依据condlist而言的,即这里的 [a+10]里面的a+10这个操作,和 [a<6]这个里面的,a<6是对应关系的,即当第一条件里的每个元素满足条件的时候,即为True的时候,就执行相应的操作,如果为false,那么久不执行,而对于不满足的元素,则执行默认的值,即default。

2、select() 对于多条件、多操作的筛选和执行

 比如针对一个数组,我们规定小于6的就加上10,介于10~15之间的就平方,大于20的就乘以10,其他的就默认变为100.

a=np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,25]])

condlist=[a<6,np.logical_and(a>10,a<16),a>20] #参数一,定义三个限制条件

print(condlist)

choicelist=[a+10,a**2,a*10] #参数二,定义三个不同的操作

print(choicelist)

aa=np.select(condlist,choicelist)

print(aa)

print(’=======================================’)

运行结果为:

[[ 11 12 13 14 15] #第一行分别加上了10
[ 0 0 0 0 0]
[121 144 169 196 225] #第三行每个数分别进行了平方
[ 0 0 0 0 0]
[210 220 230 240 250]] #第五行每个数分别乘以10

3、select函数总结

参数一,condlist=【条件一,条件二,条件三,,,,】

参数二,choicelist=【操作一,操作二,操作三,,,,,】

只有每个条件中,对应为true的才会执行相对应的操作,最终所有条件都不满足的元素,则执行默认值default。

二、np.choose()函数
choose()函数,顾名思义,也是通过某一些条件去“选择”相关的元素,choose的操作会比自己使用for-if-else效率要高。

1、choose()函数的定义

def choose(a, choices, out=None, mode=‘raise’):

参数 a :它必须是一个 int 型的 数组,并且 a 中的元素,必须是0~n-1之间的数,这里的n表示的就是数组choices数组最外层的维度数。

choices:表示的是要操作的数组,要注意的是choices的数组的维度是一定要和a进行匹配的,如果匹配不了,会出现错误。

参数out:接收运算结果的数组,它的维度一定要和 a 是一样的,是可选参数。

参数mode:默认的是raise,表示的是a数组中的元素不能超过 n ,她还有两个可选值,

           clip:将 a 中的 元素 如果小于0,则将其变为0,如果大于n-1,则变为n-1

          wrap:将a中的值 value变为value mod n,即值除以n的余数。

2、choose的应用

(1)当a和choices都是相同维数的时候——a为1维,choices为1维

result=np.array([0,0,0,0,0])

aa=np.choose([4,2,1,3,0],[11,22,33,44,55],out=result) #当a,与choices的尾数相同的时候

print(aa)

print(result) #result是out输出的,这里和aa的结果是一样的

运行结果:

[55 33 22 44 11]
[55 33 22 44 11] #二者的结果是一样的,这个地方使用了out,mode参数使用的是默认值

总结:因为choices的 n 为5,所以 a 中的元素不能够超过5 ,它代表的是 choices中的 索引index。11对应0,22对应1,33对应2,44对应3,55对应4.

(2)当a的维数比choices的维数多的时候——a为2维,choices为1维

bb=np.choose([[4,2,1,3,0],[3,4,2,0,1],[0,2,1,4,3]],[11,22,33,44,55])

print(bb)

运行结果为:

[[55 33 22 44 11]
[44 55 33 11 22]
[11 33 22 55 44]] # a 中的每一个索引 都分别与choices 对应着的。

(3)当a的维数比choices的维数少的时候——a为1维,choices为2维

cc=np.choose([4,2,1,3,0],[[11,22,33,32,31],[44,55,66,65,64],[77,88,99,98, 97],[111,222,333,332,331],[444,555,666,665,664]])

print(cc)

运行结果为:

[444 88 66 332 31] # 444 对应于 choices[4,0],88对应于 choices[2,1],66 对应于 choices[1,2],332 对应于 choices [3,3],31 对应于 choices[0,4]

总结:由此可知,choices的最外层索引index依然是与a进行匹配的,但是内层索引是按照从0开始,0、1、2、3、4逐渐递增的。鉴于此,choices的内层元素数量依然要与a的个数进行匹配才行,否则会报错。

(4)当a的维数和choices的维数都是多维的时候——a为2维,choices为2维

dd=np.choose([[4,2,1,3,0],[3,4,2,0,1],[0,2,1,4,3]],[[11,22,33,32,31],[44, 55,66,65,64],[77,88,99,98,97],[111,222,333,332,331],[444,555 ,666,665,664]])

print(dd)

运行结果为:

[[444 88 66 332 31]
[111 555 99 32 64]
[ 11 88 66 665 331]] #运算过程同上面的 (3)是类似的

总结:从上面的几个例子可以看出,choose最终的输出结果是和 a 一样的。a中的数值不能超过choices的索引值,但是没有要求一定要a和choices维度相同。

3、choose的综合应用

a=[[1,0,2],[2,1,0],[2,0,1]]

c1=[[1,2,3],[4,5,6],[7,8,9]]

choices=[c1,100,c2]

result=np.choose(a,choices)

print(result)

运行结果为:

[[100 2 33]
[ 44 100 6]
[ 77 8 100]] #原理同上

总结:choose()是根据给出的index值对choice参数获取,最后返回一个列表。

As an AI language model, I cannot provide a definitive answer without additional context and information about the variables and the code block. However, I can provide some general information about the np.where() function and the code snippet you provided. The np.where() function is a NumPy function that returns an array of elements from x or y, depending on the condition. The syntax of np.where() is as follows: np.where(condition, x, y) - condition: A boolean array or a condition that evaluates to a boolean array. - x: An array-like object that has the same shape as the condition. It is used to select elements where the condition is True. - y: An array-like object that has the same shape as the condition. It is used to select elements where the condition is False. In the code snippet you provided, the np.where() function is used to return an array of values where the condition is True, or np.inf (positive infinity) where the condition is False. The condition is: Self_Time < Hour[np.min(np.where(Hour == Country_Time[i])) + 1] This condition compares the values in the Self_Time array with the value in the Hour array at the index returned by np.min(np.where(Hour == Country_Time[i])) + 1. The np.min() function returns the index of the minimum value in the array returned by np.where(). The +1 is added to the index to get the next value in the Hour array. Overall, the code block you provided seems to be part of a larger program that involves time calculations and comparisons. Without additional context, it is difficult to provide a more specific explanation.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值