Advertisement
_LINKI

DataScience Task3 - Python

Nov 17th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. import numpy as np
  2.  
  3. class Normalizer:
  4.     def fit(self, X):
  5.         X = np.array(X, float)
  6.         self.stats = np.arange(len(X), dtype=float)
  7.         i = 0
  8.         while i < len(X):
  9.             tab = X[:, i]
  10.             j, count, sum = (0, 0, 0)
  11.             while j < len(tab):
  12.                 if not np.isnan(tab[j]):
  13.                     sum = sum + tab[j]
  14.                     count = count + 1
  15.                 j = j + 1
  16.             self.stats[i] = sum / count
  17.             i = i + 1
  18.  
  19.     def transform(self, X):
  20.         X = np.array(X, float)
  21.         i = 0
  22.         newX = X.copy()
  23.         while i < len(self.stats):
  24.             tab = newX[:, i]
  25.             j = 0
  26.             while j < len(tab):
  27.                 if np.isnan(tab[j]):
  28.                     tab[j] = self.stats[i]
  29.                 j = j + 1
  30.             i = i + 1
  31.         return newX
  32.  
  33.     def fit_transform(self, X):
  34.         self.fit(X)
  35.         return self.transform(X)
  36.  
  37. normalizer = Normalizer()
  38. normalizer.fit([[7, 2, 3], [4, np.nan, 6], [10, 5, 9]])
  39. print(normalizer.transform([[np.nan, 2, 3], [4, np.nan, 6], [10, np.nan, 9]]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement