File tree Expand file tree Collapse file tree 2 files changed +22
-9
lines changed
Expand file tree Collapse file tree 2 files changed +22
-9
lines changed Original file line number Diff line number Diff line change 1515
1616def predict (otrain ):
1717binary = (otrain > 0 )
18- norm = NormalizePositive ()
19- train = norm .fit_transform (otrain . T ). T
18+ norm = NormalizePositive (axis = 1 )
19+ train = norm .fit_transform (otrain )
2020
2121dists = distance .pdist (binary , 'correlation' )
2222dists = distance .squareform (dists )
@@ -37,9 +37,9 @@ def predict(otrain):
3737n //= 2
3838n += 1
3939revs = revs [:n ]
40- filled [u ,m ] = revs .mean ()
40+ filled [u ,m ] = np .mean (revs )
4141
42- return norm .inverse_transform (filled . T ). T
42+ return norm .inverse_transform (filled )
4343
4444def main (transpose_inputs = False ):
4545train , test = get_train_test (random_state = 12 )
Original file line number Diff line number Diff line change 22
33class NormalizePositive (object ):
44
5+ def __init__ (self , axis = 0 ):
6+ self .axis = axis
7+
58def fit (self , features , y = None ):
6- # count features that are greater than zero in axis 0:
9+ # count features that are greater than zero in axis `self.axis`:
10+ if self .axis == 1 :
11+ features = features .T
712binary = (features > 0 )
8- count0 = binary .sum (axis = 0 )
13+ count = binary .sum (axis = 0 )
914
1015# to avoid division by zero, set zero counts to one:
11- count0 [ count0 == 0 ] = 1.
16+ count [ count == 0 ] = 1.
1217
13- self .mean = features .sum (axis = 0 )/ count0
18+ self .mean = features .sum (axis = 0 )/ count
1419
1520# Compute variance by average squared difference to the mean, but only
1621# consider differences where binary is True (i.e., where there was a
1722# true rating):
1823diff = (features - self .mean ) * binary
1924diff **= 2
2025# regularize the estimate of std by adding 0.1
21- self .std = np .sqrt (0.1 + diff .sum (axis = 0 )/ count0 )
26+ self .std = np .sqrt (0.1 + diff .sum (axis = 0 )/ count )
2227return self
2328
2429def transform (self , features ):
30+ if self .axis == 1 :
31+ features = features .T
2532binary = (features > 0 )
2633features = features - self .mean
2734features /= self .std
2835features *= binary
36+ if self .axis == 1 :
37+ features = features .T
2938return features
3039
3140def inverse_transform (self , features , copy = True ):
3241if copy :
3342features = features .copy ()
43+ if self .axis == 1 :
44+ features = features .T
3445features *= self .std
3546features += self .mean
47+ if self .axis == 1 :
48+ features = features .T
3649return features
3750
3851def fit_transform (self , features ):
You can’t perform that action at this time.
0 commit comments