本文介绍了python中的条件计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此处使用我自己的logsumexp()修改版本: https://github.com/scipy/scipy /blob/v0.14.0/scipy/misc/common.py#L18

I'm attempting to use my own modified version of the logsumexp() from here: https://github.com/scipy/scipy/blob/v0.14.0/scipy/misc/common.py#L18

第85行上的计算如下:

On line 85, is this calculation:

out = log(sum(exp(a - a_max), axis=0))

但是我有一个阈值,我不希望a - a_max超过该阈值.有没有一种方法可以进行条件计算,只有当差异不小于阈值时,才允许进行减法运算.像这样:

But I have a threshold and I don't want a - a_max to exceed that threshold.Is there a way to do a conditional calculation, which would allow the subtraction to take place only if the difference isn't less than the threshold.So something like:

out = log(sum(exp( (a - a_max < threshold) ? threshold : a - a_max), axis = 0))

推荐答案

Python中有一个条件内联语句:

There is a conditional inline statement in Python:

Value1 if Condition else Value2

您的公式将转换为:

out = log(sum(exp(threshold if a - a_max < threshold else a - a_max), axis = 0))

这篇关于python中的条件计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:45