当我试图运行RuntimeWarning: divide by zero encountered in log
时,出现了stats.boxcox(list)
错误。
列表中没有零值。我引用了其他一些帖子,说我们可以使用np.errstate()
忽略这个错误。
但是,在我的例子中,我不认为这是正确的,因为stats.boxcox()
返回的值没有意义(所有常量,见下文)。
array([ 0.11472901, 0.11472901, 0.11472901, 0.11472901, 0.11472901,
0.11472901, 0.11472901, 0.11472901, 0.11472901, 0.11472901,
0.11472901, 0.11472901, 0.11472901, 0.11472901, 0.11472901,
0.11472901, 0.11472901, ... ... ]
错误详情如下:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/stats/morestats.py:591: RuntimeWarning: divide by zero encountered in log
llf -= N / 2.0 * np.log(np.sum((y - y_mean)**2. / N, axis=0))
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/optimize.py:2063: RuntimeWarning: invalid value encountered in double_scalars
w = xb - ((xb - xc) * tmp2 - (xb - xa) * tmp1) / denomenter code here
最佳答案
我在这个Github线程上遵循了一个建议,并为我工作:
https://github.com/scipy/scipy/issues/6873
建议使用stats.boxcox_normmax(array)
得到理想的lambda,然后进行boxcox
变换。
boxcox_lambda = boxcox_normmax(array, brack=(-1.9, 2.0), method='mle')
transformed_array = boxcox(array, boxcox_lambda)