如何将lambda函数转换为常规函数?

时间:2019-03-14 21:28:39

标签: python python-3.x lambda anonymous-function

我们如何将以下匿名lambda函数转换为常规函数,以便我可以定义变量muB0D0

alpha,beta,loc,scale = stats.beta.fit(value)  

error=(scale/(1.96))**2

gpdf = lambda B0, mu, sigma2: 1/np.sqrt(2*pi*sigma2)*np.exp(-1/2*((B0-mu)**2)/sigma2)
approx_sigma2 = lambda scale: (scale/(1.96))**2
ggpdf_v  = lambda B0, D0, error: gpdf(B0, mu=0.8, sigma2=error) * (D0 < 3) + (D0 >= 3) * gpdf(B0, mu=0.5, sigma2=error)
ggpdf_r  = lambda B0, D0, error: gpdf(B0, mu=0.5, sigma2=error)
ggpdf_c  = lambda B0, D0, error: gpdf(B0, mu=0.7, sigma2=error)
ggpdf_v  = lambda B0, D0, error: gpdf(B0, mu=0.9, sigma2=error)


ggpdf_v2(B0, D0, error):
    return gpdf(B0, mu >= 0.9, sigma2 = error)

2 个答案:

答案 0 :(得分:2)

gpdf = lambda B0, mu, sigma2: 1/np.sqrt(2*pi*sigma2)*np.exp(-1/2*((B0-mu)**2)/sigma2)

等效于:

def gpdf(B0, mu, sigma2):
    return 1/np.sqrt(2*pi*sigma2)*np.exp(-1/2*((B0-mu)**2)/sigma2)

在两种情况下,您都可以使用gpdf(B0, mu, sigma2)来以相同的方式调用它,因此它不会有所作为。

答案 1 :(得分:0)

您的意思是这样吗?

alpha, beta, loc, scale = stats.beta.fit(value)

error = (scale / (1.96))**2 

def gdpf (B0, mu, sigma2):
    return 1 / np.sqrt(2 * pi * sigma2) * np.exp(-1 / 2 * ((B0-mu)**2) / sigma2)

def approx_sigma2(scale):
    return (scale / (1.96))**2

def ggpdf_v(B0, D0, error):
    return gpdf(B0, 0.8, error) * (D0 < 3) + (D0 >= 3) * gpdf(B0, 0.5, error)

def ggpdf_r(B0, D0, error):
    return gpdf(B0, 0.5, error) 

def ggpdf_c(B0, D0, error):
    return gpdf(B0, 0.7, error) 

ggpdf_v(B0, D0, error):
    return gpdf(B0, 0.9, error)