|
2 | 2 |
|
3 | 3 | class NormalizePositive(object): |
4 | 4 |
|
5 | | - def fit(self, X, y=None): |
| 5 | + def fit(self, features, y=None): |
6 | 6 | # count features that are greater than zero in axis 0: |
7 | | - binary = (X > 0) |
| 7 | + binary = (features > 0) |
8 | 8 | count0 = binary.sum(axis=0) |
9 | 9 |
|
10 | 10 | # to avoid division by zero, set zero counts to one: |
11 | | - count0 += (count0 == 0) |
| 11 | + count0[count0 == 0] = 1. |
12 | 12 |
|
13 | | - self.mean = X.sum(axis=0)/count0 |
| 13 | + self.mean = features.sum(axis=0)/count0 |
14 | 14 |
|
15 | 15 | # Compute variance by average squared difference to the mean, but only |
16 | 16 | # consider differences where binary is True (i.e., where there was a |
17 | 17 | # true rating): |
18 | | - diff = (X - self.mean) * binary |
| 18 | + diff = (features - self.mean) * binary |
19 | 19 | diff **= 2 |
| 20 | + # regularize the estimate of std by adding 0.1 |
20 | 21 | self.std = np.sqrt(0.1 + diff.sum(axis=0)/count0) |
21 | 22 | return self |
22 | 23 |
|
23 | | - def fit_transform(self, X): |
24 | | - return self.fit(X).transform(X) |
| 24 | + def transform(self, features): |
| 25 | + binary = (features > 0) |
| 26 | + features = features - self.mean |
| 27 | + features /= self.std |
| 28 | + features *= binary |
| 29 | + return features |
25 | 30 |
|
26 | | - def transform(self, X): |
27 | | - binary = (X > 0) |
28 | | - X = X - self.mean |
29 | | - X /= self.std |
30 | | - X *= binary |
31 | | - return X |
32 | | - |
33 | | - def inverse_transform(self, X, copy=True): |
| 31 | + def inverse_transform(self, features, copy=True): |
34 | 32 | if copy: |
35 | | - X = X.copy() |
36 | | - X *= self.std |
37 | | - X += self.mean |
38 | | - return X |
| 33 | + features = features.copy() |
| 34 | + features *= self.std |
| 35 | + features += self.mean |
| 36 | + return features |
| 37 | + |
| 38 | + def fit_transform(self, features): |
| 39 | + return self.fit(features).transform(features) |
| 40 | + |
39 | 41 |
|
40 | 42 | def predict(train): |
41 | 43 | norm = NormalizePositive() |
|
0 commit comments