python - scipy.stats.kstest 与规范以外的分布

标签 python scipy

我在 scipy (scipy.stats.kstest) 中使用 Kolmogorov-Smirnov 测试时遇到问题。在线文档(http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.kstest.html)说它需要样本,用于与仅命名其中一个 scipy 的选项进行比较的 cdf .stats 分布,cdf 参数(和几个可选值)

只要选择的 cdf 不需要任何额外的参数,一切看起来都很好

teststat,pval=stats.kstest(sample,'norm')

(其中示例是值列表。) 但是,对于需要额外参数的其他发行版,例如 t、chisquared 等,它对我不起作用。如果没有进一步的论据,它会正确抗议

teststat,pval=stats.kstest(sample,'t')

TypeError: _cdf() takes exactly 3 arguments (2 given)

如果给出参数,

teststat,pval=stats.kstest(sample,'t',24)

它提示

TypeError: cdf() argument after * must be a sequence, not int

现在我不太确定那是什么意思,但它似乎不需要 int,24,而是一个 int,(24) 的序列。然而:

teststat,pval=stats.kstest(sample,'t',24)

TypeError: cdf() argument after * must be a sequence, not int

手动定义分布也不会产生更好的结果,因为它不觉得这是可调用的:

numargs = stats.t.numargs
[ df ] = [0.9,] * numargs
rv = stats.t(df)
teststat,pval=stats.kstest(sample,stats.t.cdf(numpy.linspace(0, numpy.minimum(rv.dist.b, 3)),df))

TypeError: 'numpy.ndarray' object is not callable

我该怎么做才能让它发挥作用? (谷歌搜索 kstest 函数或各种错误消息都没有找到任何有用的答案来回答这个问题。)

谢谢

最佳答案

查看此错误:

TypeError: cdf() argument after * must be a sequence, not int

让我觉得你是对的,它想要一个序列,而不是一个整数。文档说

args : tuple, sequence
    distribution parameters, used if rvs or cdf are strings

这似乎有效:

>>> import scipy.stats
>>> sample = scipy.stats.t(1).rvs(size=10**6)
>>> scipy.stats.kstest(sample, 't', (1,))
(0.0006249662221899932, 0.82960203415652445)

或更明确地说:

>>> scipy.stats.kstest(sample, 't', args=(1,))
(0.0006249662221899932, 0.82960203415652445)

关于python - scipy.stats.kstest 与规范以外的分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136235/

相关文章:

python - exif 标签位于图像中的什么位置?

python - 如何有效地制作一个大的numpy数组

python - scipy.sparse.linalg.eigs 和 numpy/scipy.eig 之间的不同特征值

python - matplotlib 3D 数据的 2D 切片

python - 尽管使用 overwrite_a=True scipy BLAS 例程不会覆盖输入

Python-control - 步进系统

python - 用参数最小化功能

python - 无法理解 python 函数中的不可变、可变、作用域

python - 列表创建的空间复杂度

python - 谁能举一个小例子来解释 tf.random.categorical 的参数?