Skip to content

Instantly share code, notes, and snippets.

@sebp
Created March 28, 2019 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebp/4a774eb42626dcc9ff12fb500afe07aa to your computer and use it in GitHub Desktop.
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.
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