【问题标题】:Binned weighted variance in pythonpython中的分箱加权方差
【发布时间】:2017-06-01 02:10:44
【问题描述】:

我正在尝试获取一组分箱数据的每个箱的加权方差。我的尝试如下,我收到以下错误。

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)

【问题讨论】:

标签: python scipy statistics


【解决方案1】:

简而言之,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, ...)

【讨论】:

  • 这会导致错误“TypeError: Axis must be specified when a shape of a and weights different”。我假设是因为它期望 weight_data 是单个 bin 的大小;它想将此应用于每个垃圾箱。
猜你喜欢
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-17
  • 2017-06-13
  • 1970-01-01
  • 2017-07-31
  • 2013-09-22
相关资源
最近更新 更多