Skip to content

Instantly share code, notes, and snippets.

@amindadgar
Created August 1, 2022 17:12
Show Gist options
  • Save amindadgar/dabf25692d43f83a1cd196e85d804731 to your computer and use it in GitHub Desktop.
Save amindadgar/dabf25692d43f83a1cd196e85d804731 to your computer and use it in GitHub Desktop.
A simple program to evaluate the zero mean scaling using Sklearn library and manually
from sklearn.preprocessing import StandardScaler
def find_manually(data, mean, var):
"""
scale the data manually using mean(`mean`) and variance (`var`)
"""
return (data - mean) / var
def start_program():
"""
Start the process of the program
"""
data = [[0, 0], [0, 0], [1, 1], [1, 1]]
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)
print(f'Original data: {data}')
print(f'Scaled Original data: {data_scaled}')
print(f'mean: {scaler.mean_}, variance: {scaler.var_}')
print('-'* 50)
data_new = [[2, 2]]
print(f'new data: {data_new}, the new data scaled using sklearn: {scaler.transform(data_new)}')
print(f'new data: {data_new}, the new data scaled manually: {find_manually(data_new, scaler.mean_, scaler.var_)}')
if __name__ == '__main__':
start_program()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment