Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. def normalization(x_train):
  2.     '''
  3.        apply normalization to the samples (x_train)
  4.        returns a new array that has been properly normalized,
  5.        also returns the mean and standard deviation per feature in the training set
  6.    '''
  7.     # >>> YOUR CODE STARTS HERE <<<
  8.     x_train_normalized = np.empty(shape = x_train.shape)
  9.     i = 0
  10.     for features in x_train:
  11.         normed = []
  12.         feature_std = np.std(features, axis=0)
  13.         feature_mean = np.mean(features, axis=0)
  14.         for feature in features:
  15.             normed.append((feature-feature_mean)/feature_std)
  16.         x_train_normalized[i] = normed
  17.         i+=1
  18.        
  19.     print(x_train_normalized)
  20.        
  21.     # array of normalized features
  22.     mean = np.mean(x_train_normalized, axis=0) # array of mean value per feature
  23.     std = np.std(x_train_normalized, axis=0) # array of std value per feature
  24.        
  25.     # >>> YOUR CODE ENDS HERE <<<
  26.    
  27.     return x_train_normalized, mean, std
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement