Box-Cox Transformation
Contents
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>]
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>]
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))