Created
March 28, 2019 14:05
-
-
Save sebp/4a774eb42626dcc9ff12fb500afe07aa to your computer and use it in GitHub Desktop.
Inverse transform of coefficients of a linear model fit to standardized data with zero mean and unit variance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
class CoefficientRescaler: | |
"""Inverse transform of coefficients of a linear model. | |
Parameters | |
========== | |
scalar_mean : ndarray, shape=(n_features,) | |
Feature-wise mean. | |
scalar_scale : ndarray, shape=(n_features,) | |
Feature-wise standard deviation. | |
""" | |
def __init__(self, scalar_mean, scalar_scale): | |
self.scalar_mean = scalar_mean | |
self.scalar_scale = scalar_scale | |
def rescale(self, coef_scalar, intercept): | |
"""Apply inverse transform to coefficients. | |
Parameters | |
========== | |
coef_scalar : ndarray, shape=(n_features,) | |
Estimated coefficents after standardization. | |
intercept : float | |
Estimated intercept after standardization. | |
Returns | |
======= | |
coef_rescaled : ndarray, shape=(n_features,) | |
Rescaled coefficients. | |
intercept_rescaled : float | |
Rescaled intercept. | |
""" | |
coef_new = coef_scalar / self.scalar_scale | |
intercept_new = intercept - np.dot(coef_new, self.scalar_mean) | |
return coef_new, intercept_new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment