【问题标题】:scipy generic_filter cast the returned valuesscipy generic_filter 转换返回值
【发布时间】:2017-05-12 22:14:24
【问题描述】:

附上代码:

def stat_function(x):
    center = x[len(x) / 2]
    ecdf = ECDF(xx)
    percentile = (1 - ecdf(center)) * 100
    print percentile
    return percentile

主要:

print generic_filter(table,
    function=stat_function,
    size=window_size,
    mode=mode,
    extra_arguments=(-1,))

我得到了输出:

[[84 76 76 76 76 76 76 76 76 60]
[52 48 48 48 48 48 48 48 48 39]
[52 48 48 48 48 48 48 48 48 39]
[52 48 48 48 48 48 48 48 48 39]
[52 48 48 48 48 48 48 48 48 39]
[52 48 48 48 48 48 48 48 48 39]
[52 48 48 48 48 48 48 48 48 39]
[52 48 48 48 48 48 48 48 48 39]
[52 48 48 48 48 48 48 48 48 39]
[24 15 15 15 15 15 15 15 15  0]]

一切都很好,但是如果我在返回之前在我的函数中打印“百分位数”,我会看到我所有的 15s 实际上都是 16.0s 而我的 39s 是 40.0s。函数 generic_filter 需要返回一个浮点数和“16.0 " 被返回,但在构建的数组中,它被转换为 int 并变为 "15"。确实 print percentile, int(percentile) 将显示“16.0, 15”。
如果有人可以帮助我理解为什么这个 scipy 的函数需要一个浮点数然后将它转换成一个 int 以及为什么 int(16.0) 给出 15,我就在这里。

PS:即使使用numpy.array(generif_filter(...), dtype=numpy.float),我也得到了错误的整数表。

【问题讨论】:

    标签: python python-2.7 numpy casting scipy


    【解决方案1】:

    哇,解决方案很棘手。 Scipy 会将返回表的所有值转换为您的第一个表的类型。
    例如:

    table = [0,1,2,3,4,5,6,7,8,9] 
    generic_filter(table, ... ) # returns a table of integers
    table = numpy.array(table, numpy.float)
    generic_filter(table, ... ) # returns this time a table of floats
    

    因此,如果像我一样 scipy 无缘无故地强制转换他的输出,请更改您的输入;)

    【讨论】:

      猜你喜欢
      • 2013-12-30
      • 2014-01-06
      • 2013-11-03
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多