Skip to content

Commit bb9b8de

Browse files
committed
ENH Average methods (instead of stacked learning)
This is mentioned in the book, but the code is only shown here.
1 parent 4205208 commit bb9b8de

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

ch08/README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ regression.py
2525
stacked.py
2626
Stacked predictions
2727
averaged.py
28-
Averaging of predictions
28+
Averaging of predictions (mentioned in book, but code is not shown there).
2929

3030
Association Rule Mining
3131
-----------------------

ch08/averaged.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import numpy as np
2+
import load_ml100k
3+
import regression
4+
import corrneighbours
5+
from sklearn import metrics
6+
import norm
7+
8+
def predict(train):
9+
predicted0 = regression.predict(train)
10+
predicted1 = regression.predict(train.T).T
11+
predicted2 = corrneighbours.predict(train)
12+
predicted3 = corrneighbours.predict(train.T).T
13+
predicted4 = norm.predict(train)
14+
predicted5 = norm.predict(train.T).T
15+
stack = np.array([
16+
predicted0,
17+
predicted1,
18+
predicted2,
19+
predicted3,
20+
predicted4,
21+
predicted5,
22+
])
23+
return stack.mean(0)
24+
25+
26+
def main():
27+
train,test = load_ml100k.get_train_test(random_state=12)
28+
predicted = predict(train)
29+
r2 = metrics.r2_score(test[test > 0], predicted[test > 0])
30+
print('R2 averaged: {:.2%}'.format(r2))
31+
32+
if __name__ == '__main__':
33+
main()

0 commit comments

Comments
 (0)