36. Box-Cox Transformation#

import pandas as pd
import numpy as np
from scipy import stats

import matplotlib.pyplot as plt

36.1. Data#

We generate some fake data to illustrate the Box-Cox transformation.

x = np.linspace(1, 100, 1000)

data = np.linspace(0.1,2, 1000) * np.sin(x) + (x/20) ** 2 + 10
fig, ax = plt.subplots(figsize=(10, 6.18))

ax.plot(x, data)
[<matplotlib.lines.Line2D at 0x7f2c48fc8fa0>]
../../_images/box-cox-transformation_5_1.png

36.2. Box-Cox Transformation#

data_bc, data_bc_lmbda = stats.boxcox(data)
data_bc_lmbda
-0.4043829916225981
nrows = 2
fig, ax = plt.subplots(figsize=(10, 6.18), nrows=nrows)

ax[0].plot(x, data)
ax[1].plot(x, data_bc)
[<matplotlib.lines.Line2D at 0x7f2c48e8c670>]
../../_images/box-cox-transformation_8_1.png
nrows = 20

fig, ax = plt.subplots(figsize=(10, nrows*6.18/2), nrows=nrows)

ax[0].plot(x, data)

for idx, lmbda in enumerate(np.linspace(-1,1,19)):
    ax[idx+1].plot(x, stats.boxcox(data, lmbda=lmbda))
../../_images/box-cox-transformation_9_0.png
Created in deepnote.com Created in Deepnote