如何在 Python 中应用 Box-Cox 变换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30018875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:50:43  来源:igfitidea点击:

How to apply Box-Cox transformation in Python?

pythonnumpyscipycurve-fitting

提问by Tom Kurushingal

I have data of the form:

我有以下形式的数据:

X   Y
3.53    0
4.93    50
5.53    60
6.21    70
7.37    80
9.98    90
16.56   100

And I want to find out n so that this can be fit to a function of the form:

我想找出 n 以便它适合以下形式的函数:

enter image description here

在此处输入图片说明

I am trying to determine n by Box-Cox transformation. How can this be done in Python?

我试图通过 Box-Cox 变换来确定 n。这如何在 Python 中完成?

回答by blueogive

I think you want scipy.stats.boxcox.

我想你想要scipy.stats.boxcox

from scipy import stats
import numpy as np

data = np.fromstring('3.53    0 4.93    50 5.53    60 6.21    70 7.37    80 9.98    90 16.56   100', sep=' ').reshape(7, 2)

stats.boxcox(data[0,])
(array([ 0.91024309,  1.06300488,  1.10938333,  1.15334193,  1.213348  ,
     1.30668122,  1.43178909]), -0.54874593147877893)

回答by 1dhiman

Box-Cox of 1+x may be helpful in cases with zeros(boxcox1p)

1+x 的 Box-Cox 在零的情况下可能会有所帮助(boxcox1p

from scipy.special import boxcox1p
boxcox1p([0.01, 0.1], 0.25)

回答by yogesh agrawal

For Box-Cox Transformation in Python you must follow below steps:-

对于 Python 中的 Box-Cox 转换,您必须遵循以下步骤:-

from scipy.stats import boxcox
from scipy.special import inv_boxcox

y =[10,20,30,40,50]
y,fitted_lambda= boxcox(y,lmbda=None)
inv_boxcox(y,fitted_lambda)

in scipy.specialpackage box-coxmethod is present but that expect lambdaexplicitly.Hence i used box-cox from scipy.statsand inv_box-cox from special as inv_boxcox not available in scipy.stats.

inscipy.specialbox-cox方法存在,但lambda明确期望。因此,我使用了 box-cox fromscipy.stats和 inv_box-cox from special 因为 inv_boxcox 在 scipy.stats 中不可用。