Skip to content

Instantly share code, notes, and snippets.

@GermanCM
Last active December 20, 2018 23:34
Show Gist options
  • Save GermanCM/a6d072d88defd3a2f605ca9cfcb551c5 to your computer and use it in GitHub Desktop.
Save GermanCM/a6d072d88defd3a2f605ca9cfcb551c5 to your computer and use it in GitHub Desktop.
Features normalization
def normalize_features(df):
"""
Normalize the features in the data set.
Returns the normalized values, mean and standard deviation for each feature
"""
mu = df.mean()
sigma = df.std()
if (sigma == 0).any():
raise Exception("One or more features had the same value for all samples, so could not be normalized. Please do not include features with only a single value in your model")
df_normalized = (df - df.mean()) / df.std()
return df_normalized, mu, sigma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment