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.random(100) y_box, lambda_ = ss.boxcox(y + 1) # Add 1 to be able to transform 0 values
Then, I fit a statistical model to predict the values of this Box-Cox transformed variable. The model predictions are in the Box-Cox scale and I want to transform them to the original scale of the variable.
from sklearn.ensemble import RandomForestRegressor rf = RandomForestRegressor() X = np.random.random((100, 100)) rf.fit(X, y_box) pred_box = rf.predict(X)
However, I can't find a SciPy function that performs a reverse Box-Cox transformation given transformed data and lambda. Is there such a function? I coded an inverse transformation for now.
pred_y = np.power((y_box * lambda_) + 1, 1 / lambda_) - 1
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.])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With