Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply Box-Cox transformation in Python?

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:

enter image description here

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

like image 938
Tom Kurushingal Avatar asked May 03 '15 20:05

Tom Kurushingal


3 Answers

I think you want 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)
like image 151
blueogive Avatar answered Oct 04 '22 03:10

blueogive


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

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.special package box-cox method is present but that expect lambda explicitly.Hence i used box-cox from scipy.stats and inv_box-cox from special as inv_boxcox not available in scipy.stats.

like image 28
yogesh agrawal Avatar answered Oct 04 '22 04:10

yogesh agrawal


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

from scipy.special import boxcox1p
boxcox1p([0.01, 0.1], 0.25)
like image 45
1dhiman Avatar answered Oct 04 '22 03:10

1dhiman