-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathbagging.py
executable file
·68 lines (54 loc) · 1.73 KB
/
bagging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Code from Chapter 13 of Machine Learning: An Algorithmic Perspective (2nd Edition)
# by Stephen Marsland (http://stephenmonika.net)
# You are free to use, change, or redistribute the code in any way you wish for
# non-commercial purposes, but please maintain the name of the original author.
# This code comes with no warranty of any kind.
# Stephen Marsland, 2008, 2014
import numpy as np
import dtree
class bagger:
"""The bagging algorithm based on the decision tree of Chapter 6"""
def __init__(self):
""" Constructor """
self.tree = dtree.dtree()
def bag(self,data,targets,features,nSamples):
nPoints = np.shape(data)[0]
nDim = np.shape(data)[1]
self.nSamples = nSamples
# Compute bootstrap samples
samplePoints = np.random.randint(0,nPoints,(nPoints,nSamples))
classifiers = []
for i in range(nSamples):
sample = []
sampleTarget = []
for j in range(nPoints):
sample.append(data[samplePoints[j,i]])
sampleTarget.append(targets[samplePoints[j,i]])
# Train classifiers
classifiers.append(self.tree.make_tree(sample,sampleTarget,features,1))
return classifiers
def bagclass(self,classifiers,data):
decision = []
# Majority voting
for j in range(len(data)):
outputs = []
#print data[j]
for i in range(self.nSamples):
out = self.tree.classify(classifiers[i],data[j])
if out is not None:
outputs.append(out)
# List the possible outputs
out = []
for each in outputs:
if out.count(each)==0:
out.append(each)
frequency = np.zeros(len(out))
index = 0
if len(out)>0:
for each in out:
frequency[index] = outputs.count(each)
index += 1
decision.append(out[frequency.argmax()])
else:
decision.append(None)
return decision