[英]Python stats.kstest bug?
I find the way to interpret the shape of random sample has big impact on kstest. 我发现解释随机样本形状的方法对kstest有很大影响。 I try the following codes:
我尝试以下代码:
import numpy as np
from scipy import stats
N = 260
np.random.seed(1)
X = np.random.rand(N)
Xarray = X.reshape(N,1)
XarrayT = Xarray.T
print('X' + str(X.shape) + ': ' + str(stats.kstest(X, 'uniform') ) )
print( 'Xarray' + str(Xarray.shape) + ':' + str( stats.kstest(Xarray, 'uniform') ) )
print( 'XarrayT' + str(XarrayT.shape) + ': ' + str( stats.kstest(XarrayT, 'uniform') ) )
It gives the results: 结果如下:
X(260,): KstestResult(statistic=0.052396054203786291, pvalue=0.46346349447418866)
Xarray(260, 1):KstestResult(statistic=0.99988562518265511, pvalue=0.0)
XarrayT(1, 260): KstestResult(statistic=0.99988562518265511, pvalue=0.00022874963468977327)
where X, Xarray, XarrayT have the same data, except that they have different shape. 其中X,Xarray,XarrayT具有相同的数据,但形状不同。 And the pvalues are totally different.
p值完全不同。 Is it due to a bug or I miss some point in order to use kstest correctly?
是由于错误还是为了正确使用kstest而错过了一点?
Thanks! 谢谢!
Well, the scipy kstest documentation tells us it should be a 1d array. 好吧, 科学的kstest文档告诉我们它应该是一维数组。
if we run the following: 如果我们运行以下命令:
print('X ' + 'ndimensions=' + str(X.ndim) + ' ' + (str(stats.kstest(X, 'uniform'))))
We see 1 dimensions in the target array. 我们在目标数组中看到1个维度。
output: 输出:
X ndimensions=1 KstestResult(statistic=0.052396054203786291, pvalue=0.46346349447418866)
However, when we try our other Xarray: 但是,当我们尝试其他Xarray时:
print('Xarray ' + 'ndimensions=' + str(Xarray.ndim) + ' ' + (str(stats.kstest(Xarray, 'uniform'))))
Xarray ndimensions=2 KstestResult(statistic=0.99988562518265511, pvalue=0.0)
This would indicate to me the use of two dimensions in the input array is screwing up the Kolmogorov-Smirnov test for goodness of fit. 这向我表明,在输入数组中使用二维会破坏Kolmogorov-Smirnov检验的拟合优度。
I would also suggest reading the answers at this stackoverflow question 我也建议阅读这个stackoverflow问题的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.