Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- class Normalizer:
- def fit(self, X):
- X = np.array(X, float)
- self.stats = np.arange(len(X), dtype=float)
- i = 0
- while i < len(X):
- tab = X[:, i]
- j, count, sum = (0, 0, 0)
- while j < len(tab):
- if not np.isnan(tab[j]):
- sum = sum + tab[j]
- count = count + 1
- j = j + 1
- self.stats[i] = sum / count
- i = i + 1
- def transform(self, X):
- X = np.array(X, float)
- i = 0
- newX = X.copy()
- while i < len(self.stats):
- tab = newX[:, i]
- j = 0
- while j < len(tab):
- if np.isnan(tab[j]):
- tab[j] = self.stats[i]
- j = j + 1
- i = i + 1
- return newX
- def fit_transform(self, X):
- self.fit(X)
- return self.transform(X)
- normalizer = Normalizer()
- normalizer.fit([[7, 2, 3], [4, np.nan, 6], [10, 5, 9]])
- 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