Python numpy最多接受3个参数-一种解决方法?

Python numpy最多接受3个参数-一种解决方法?,python,numpy,Python,Numpy,假设我有以下代码: import numpy as np def myf(c): return c*11 def method_A(c): return c*999 def method_B(c): return c*55 minimum = 30 maximum = 100 the_method = 'A' b = np.array([1, 20, 35, 3, 45, 52, 78, 101, 127, 135]) 为了满足一些条件,我想在哪里使用nump

假设我有以下代码:

import numpy as np

def myf(c):
    return c*11

def method_A(c):
    return c*999

def method_B(c):
    return c*55

minimum = 30
maximum = 100
the_method = 'A'

b = np.array([1, 20, 35, 3, 45, 52, 78, 101, 127, 135])
为了满足一些条件,我想在哪里使用numpy

比如:

b = np.where( np.logical_or(b < minimum , b > maximum) , b, 
             (if the_method == 'A': method_A(b)) ,
             (if the_method == 'B': method_B(b)))
函数最多接受3个参数(4个给定),因为np.where不能接受超过3个参数


有办法解决我的问题吗?

您的括号不平衡,您需要在内部条件中添加额外的括号:

In [33]:
b = np.where( np.logical_or(b < minimum , b > maximum) , b,
             ((np.where(apply_method == 'A',method_A(b),
             (np.where(apply_method == 'B',method_B(b),None))
            ))))
b

Out[33]:
array([1, 20, 35000, 3, 45000, 52000, 78000, 101, 127, 135], dtype=object)
[33]中的

b=np.其中(np.逻辑_或(b<最小值,b>最大值),b,
((np.式中,应用方法='A',方法A(b),
(np.式中(应用方法='B',方法B(B),无))
))))
B
出[33]:
数组([1,20,35000,3,45000,52000,78000,101,127,135],数据类型=对象)
您最初的尝试:

b = np.where( np.logical_or(b < minimum , b > maximum) , b, 
             (np.where(the_method == 'A',method_A(b),b)),
             (np.where(the_method == 'B',method_B(b),b))
            )
b=np.其中(np.逻辑_或(b<最小值,b>最大值),b,
(np.式中(_方法='A',方法_A(b),b)),
(np.where(方法=='B',方法B(B),B))
)

其他条件中需要的括号

您的括号不平衡,内部条件中需要额外的括号:

In [33]:
b = np.where( np.logical_or(b < minimum , b > maximum) , b,
             ((np.where(apply_method == 'A',method_A(b),
             (np.where(apply_method == 'B',method_B(b),None))
            ))))
b

Out[33]:
array([1, 20, 35000, 3, 45000, 52000, 78000, 101, 127, 135], dtype=object)
[33]中的

b=np.其中(np.逻辑_或(b<最小值,b>最大值),b,
((np.式中,应用方法='A',方法A(b),
(np.式中(应用方法='B',方法B(B),无))
))))
B
出[33]:
数组([1,20,35000,3,45000,52000,78000,101,127,135],数据类型=对象)
您最初的尝试:

b = np.where( np.logical_or(b < minimum , b > maximum) , b, 
             (np.where(the_method == 'A',method_A(b),b)),
             (np.where(the_method == 'B',method_B(b),b))
            )
b=np.其中(np.逻辑_或(b<最小值,b>最大值),b,
(np.式中(_方法='A',方法_A(b),b)),
(np.where(方法=='B',方法B(B),B))
)

另一个条件所需的括号

方法
是标量而不是数组,因此不需要内部的
np。其中
s在这里:

if the_method == 'A':
    which_method = method_A 
elif the_method == 'B':
    which_method = method_B 
else:
    raise ValueError

b = np.where(
    (b < minimum) | (b > maximum),
     b, 
     which_method(b)
)

_方法是标量而不是数组,因此不需要内部的
np

if the_method == 'A':
    which_method = method_A 
elif the_method == 'B':
    which_method = method_B 
else:
    raise ValueError

b = np.where(
    (b < minimum) | (b > maximum),
     b, 
     which_method(b)
)

您是否需要在内部
位置周围加上括号?像这样:
In[20]:b=np.where(np.logical_或(bmaximum),b,((np.where(_方法='A',方法A(b),b)),(np.where(_方法='b',方法b(b),b)))b Out[20]:数组([1,20,34965,3,44955,51948,77922,101,127,135],[    1,    20,    35,     3,    45,    52,    78,   101,   127,           135]])
?嗯..是的!我只想要一个数组作为输出,而不是两个。
np.where
及其三个参数实现IF-ELSE,而您有两个IFs。因此,我不确定
np.where
在这里是否是一个好的选择。@EdChum:Hm,好的!我只是打开括号并使用
((np.where)(应用方法='a',方法a(b),(np.where(apply_method=='B',method_B(B),None))
很好用!如果你想回答这个问题,谢谢你发布的代码片段不起作用,
apply_method
没有定义你是否需要在内部
的位置
添加括号?比如:
在[20]:B=np.where(np.logical\u或(Bmaximum),b,((np.where(the_method='A',method_A(b),b)),(np.where(the_method='b',method_b(b),b)))b Out[20]:数组([1,20,34965,3,44955,51948,77922,101,127,135],[1,20,35,3,45,52,78,101,127,135])
?嗯..是的!我只想要一个数组作为输出,而不是两个。
np.where
及其三个参数实现IF-ELSE,而您有两个IFs。因此,我不确定
np.where
在这里是否是一个好的选择。@EdChum:Hm,好的!我只是打开括号并使用
((np.where)(应用方法='a',方法a(b),(np.where(应用方法=='B',方法B(B),无)
而且效果很好!如果您想回答这个问题,感谢您发布的代码片段不起作用,
apply\u方法
没有定义注意威胁插入的性能代价
None
在这里创建的数组现在是
dtype=object
注意威胁插入的性能代价
None
已在此处创建-数组现在是
dtype=object