【问题标题】:TypeError: "'numpy.float64' object is not callable" when using scipy.stats.bootstrap类型错误:使用 scipy.stats.bootstrap 时“'numpy.float64'对象不可调用”
【发布时间】:2022-01-19 14:09:07
【问题描述】:

我想用scipy.stats.bootstrap 应用统计引导方法。

在下面的代码中,我将两个不同的 .txt 文件加载到 Python 中。每个文件都包含一列数值(浮点数)。我想计算每个文件的变异系数 (CV),并比较它们在 CV 中的差异是否具有统计学意义。这就是我使用引导程序的原因。

这里是完整的代码:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import scipy as sp
from scipy import stats


# Coefficient of variation
Core_values = np.loadtxt(f"pathtofile/file1.txt", comments=None, delimiter=None, converters=None,
skiprows=0, usecols=0,unpack=False, ndmin=0, encoding=None, max_rows=None, like=None)

Periphery_values = np.loadtxt(f"pathtofile/file2.txt", comments=None, delimiter=None, converters=None,
skiprows=0, usecols=0, unpack=False, ndmin=0, encoding=None, max_rows=None, like=None)

Results = sp.stats.bootstrap((Core_values, Periphery_values), sp.stats.variation((Core_values, Periphery_values), axis=None), vectorized=False, paired=True, confidence_level=0.95, n_resamples=20000)

print(Results)

当我只应用以下计算两个文件的 CV 的代码时:

Results =  sp.stats.variation((Core_values, Periphery_values), axis=None)
print(Results)

Python 给了我正确的结果,即来自两个输入文件的值的一个 CV 值。但是,在完整代码中将sp.stats.variation((Core_values, Periphery_values), axis=None) 实现到引导代码中时,我收到以下错误消息:TypeError: 'numpy.float64' object is not callable

因此我认为我的错误是我将两个样本(Core_values 和 Periphery_values)都提供到了

Results = sp.stats.bootstrap((Core_values, Periphery_values), sp.stats.variation...

我无法弄清楚正确的实现是什么样子来告诉 Python 我想使用这两个示例进行引导以避免错误消息。

【问题讨论】:

    标签: python scipy.stats


    【解决方案1】:

    答案在scipy.stats.bootstrapstatistic 的第二个参数的文档中:“statistic 必须是一个 callable [强调添加] 接受 len(data) 样本作为单独参数 [其中databootstrap] 的第一个参数,并返回结果统计信息。如果 vectorized 设置为 True,统计信息还必须接受关键字参数轴并进行矢量化以沿提供的轴计算统计信息。"

    “可调用”是一个函数或类似函数的东西。您作为 statistic 参数提供的内容,sp.stats.variation((Core_values, Periphery_values), axis=None) 两者都不是。它只是一个数字(特别是浮点值),因此会出现错误“TypeError: numpy.float64 object is not callable`。

    可能想要作为statistic 参数传递的可能是这样的:lambda c,v: sp.stats.variation((c,v), axis=None)。一个 lambda 一个可调用的。请注意,根据statistic 的文档,lambda 中的参数数量与bootstrap 的第一个参数的长度相同,即元组(Core_values, Periphery_values)。但是,这个特定的 lambda 可能无法满足您的需求。 (是否确实超出了这个特定问题的范围。)

    【讨论】:

    • 感谢您提供的非常好的解释和帮助。非常感谢!
    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 2019-06-20
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    相关资源
    最近更新 更多