我正在尝试获取一组分箱数据的每个bin的加权方差。我的尝试在下面,我得到以下错误。
TypeError: <lambda>() missing 1 required positional argument: 'weight_data'
lambda函数不能有两个参数吗?
我很感激任何解决方案,无论是错误,给我一个工作功能,还是其他方法。我只使用python。
def wVar(values, weight_data, bias=None):
weighted_mean = np.average(values, weights=weight_data)
s2_bias = np.average((values - weighted_mean)**2, weights=weight_data)
s2_unbiased = s2_bias / (1.0 - ( np.sum(weight_data**2) / (np.sum(weight_data))**2))
if (bias == None):
return s2_unbiased
elif ( bias.lower() == "biased"):
return s2_bias
elif ( bias.lower() == "unbiased"):
return s2_unbiased
else:
return print(' "bias" must be assigned as either "unbiased" or "biased" or not at all (default = unbiased) ')
bin_wvar0, bin_edges, binnumber = sp.stats.binned_statistic(x_data, y_data, statistic=lambda y_data, weight_data: wVar(y_data, weight_data, bias="unbiased"), bins= bin_edge_data)
答案 0 :(得分:0)
简而言之,statistic
期望一个带有一个参数的函数。创建一个部分应用weight_data
并在通话中使用该功能的功能。
curried_weight = lambda y_data: wVar(y_data, weight_data, bias="unbiased")
sp.stats.binned_statistic(x_data, y_data, statistic=curried_weight, ...)