Skip to content

Commit 6948563

Browse files
committed
RFCT Simpler code
1 parent 2c530e1 commit 6948563

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

‎ch08/corrneighbours.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
defpredict(otrain):
1717
binary= (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

2121
dists=distance.pdist(binary, 'correlation')
2222
dists=distance.squareform(dists)
@@ -37,9 +37,9 @@ def predict(otrain):
3737
n//=2
3838
n+=1
3939
revs=revs[:n]
40-
filled[u,m] =revs.mean()
40+
filled[u,m] =np.mean(revs)
4141

42-
returnnorm.inverse_transform(filled.T).T
42+
returnnorm.inverse_transform(filled)
4343

4444
defmain(transpose_inputs=False):
4545
train, test=get_train_test(random_state=12)

‎ch08/norm.py‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,50 @@
22

33
classNormalizePositive(object):
44

5+
def__init__(self, axis=0):
6+
self.axis=axis
7+
58
deffit(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+
ifself.axis==1:
11+
features=features.T
712
binary= (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):
1823
diff= (features-self.mean) *binary
1924
diff**=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)
2227
returnself
2328

2429
deftransform(self, features):
30+
ifself.axis==1:
31+
features=features.T
2532
binary= (features>0)
2633
features=features-self.mean
2734
features/=self.std
2835
features*=binary
36+
ifself.axis==1:
37+
features=features.T
2938
returnfeatures
3039

3140
definverse_transform(self, features, copy=True):
3241
ifcopy:
3342
features=features.copy()
43+
ifself.axis==1:
44+
features=features.T
3445
features*=self.std
3546
features+=self.mean
47+
ifself.axis==1:
48+
features=features.T
3649
returnfeatures
3750

3851
deffit_transform(self, features):

0 commit comments

Comments
(0)