Reverse Box-Cox transformation

前端 未结 4 874
自闭症患者
自闭症患者 2021-02-05 05:25

I am using SciPy\'s boxcox function to perform a Box-Cox transformation on a continuous variable.

from scipy.stats import boxcox
import numpy as np
y = np.random         


        
相关标签:
4条回答
  • 2021-02-05 05:58

    SciPy has added an inverse Box-Cox transformation.

    https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.inv_boxcox.html

    scipy.special.inv_boxcox scipy.special.inv_boxcox(y, lmbda) =

    Compute the inverse of the Box-Cox transformation.

    Find x such that:

    y = (x**lmbda - 1) / lmbda  if lmbda != 0
        log(x)                  if lmbda == 0
    

    Parameters: y : array_like

    Data to be transformed.

    lmbda : array_like

    Power parameter of the Box-Cox transform.

    Returns:
    x : array

    Transformed data.

    Notes

    New in version 0.16.0.

    Example:

    from scipy.special import boxcox, inv_boxcox
    y = boxcox([1, 4, 10], 2.5)
    inv_boxcox(y, 2.5)
    
    output: array([1., 4., 10.])
    
    0 讨论(0)
  • 2021-02-05 05:58

    In order to inverse the boxcox transformation from scipy.stats.boxcox using scipy.special.inv_boxcox you have to identify the lambda which was generated.

    First apply the transformation and print the lambda (ie. param).

    df[feature_boxcox], param = stats.boxcox(df[feature])
    print('Optimal lambda', param)
    

    Then in order to inverse the transformation you input the generated lambda.

    inv_boxcox(df[feature_boxcox], param)
    
    0 讨论(0)
  • 2021-02-05 06:00
    1. Here it is the code. It is working and just test. Scipy used neperian logarithm, i check the BoxCox transformation paper and it seens that they used log10. I kept with neperian, because it works with scipy
    2. Follow the code:

      #Function
      def invboxcox(y,ld):
         if ld == 0:
            return(np.exp(y))
         else:
            return(np.exp(np.log(ld*y+1)/ld))
      
      # Test the code
      x=[100]
      ld = 0
      y = stats.boxcox(x,ld)
      print invboxcox(y[0],ld)
      
    0 讨论(0)
  • 2021-02-05 06:05

    Thanks to @Warren Weckesser, I've learned that the current implementation of SciPy does not have a function to reverse a Box-Cox transformation. However, a future SciPy release may have this function. For now, the code I provide in my question may serve others to reverse Box-Cox transformations.

    0 讨论(0)
提交回复
热议问题