gpt4 book ai didi

python - numpy数组中的like语句时如何编写案例

转载 作者:行者123 更新时间:2023-12-04 13:30:21 28 4
gpt4 key购买 nike

def custom_asymmetric_train(y_true, y_pred):
residual = (y_true - y_pred).astype("float")
grad = np.where(residual>0, -2*10.0*residual, -2*residual)
hess = np.where(residual>0, 2*10.0, 2.0)
return grad, hess

我想写这样的声明:
    case when residual>=0 and residual<=0.5 then -2*1.2*residual
when residual>=0.5 and residual<=0.7 then -2*1.*residual
when residual>0.7 then -2*2*residual end )

但是 np.where无法编写&(and)逻辑。当在python中的 np.where中包含逻辑时,我该如何写这种情况。

谢谢

最佳答案

您可以在(condition1) & (condition2)调用中使用语法np.where(),因此您可以像下面这样修改函数的np.where()调用:

def custom_asymmetric_train(y_true, y_pred):
residual = (y_true - y_pred).astype("float")
residual = np.where((residual>=0.0)&(residual<=0.5), -2*1.2*residual, residual)
residual = np.where((residual>=0.5)&(residual<=0.7), -2*1.0*residual, residual)
residual = np.where(residual>0.7, -2*2.0*residual, residual)
...

第一个参数是要满足的条件,第二个参数是满足条件时要使用的值,第三个参数是不满足条件时要使用的值。

关于python - numpy数组中的like语句时如何编写案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59687514/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com