-
Notifications
You must be signed in to change notification settings - Fork 0
/
BagLearner.py
76 lines (66 loc) · 4.99 KB
/
BagLearner.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
69
70
71
72
73
74
75
76
import random
import numpy as np
from scipy import stats
class BagLearner(object):
"""
This is a Bag Regression Learner. It is implemented correctly.
:param verbose: If “verbose” is True, your code can print out information for debugging.
If verbose = False your code should not generate ANY output. When we test your code, verbose will be False.
:type verbose: bool
"""
def __init__(self, learner, kwargs = {}, bags = 20, boost = False, verbose = False):
"""
Constructor method
"""
# The learner points to the learning class that will be used in the BagLearner
self.learner = learner
# keyword arguments that are passed on to the learner’s constructor and they can vary according to the learner
self.kwargs = kwargs
# number of learners you should train using Bootstrap Aggregation.
self.bags = bags
self.boost = boost
# generate output if true
self.vrbose = verbose
# creating an instance of a learner object using the keyword arguments provided in kwargs
# and then appending that learner object to the learners list.
self.learners = []
for i in range(bags):
self.learners.append(self.learner(**self.kwargs))
def author(self):
"""
:return: The GT username of the student
:rtype: str
"""
return "agandhi301"
def add_evidence(self, data_x, data_y):
"""
Add training data to learner
:param data_x: A set of feature values used to train the learner
:type data_x: numpy.ndarray
:param data_y: The value we are attempting to predict given the X data
:type data_y: numpy.ndarray
"""
for learner in self.learners:
# Generate a list of random numbers between 0 and the number of columns in a 2D array
random_numbers = [random.randint(0, data_x.shape[0] - 1) for _ in range(data_x.shape[0])]
# Get the bag x data as per the randomized numbers
self.bag_x = data_x[random_numbers]
self.bag_y = data_y[random_numbers]
# call learner with random x bag and random y bag data
learner.add_evidence(self.bag_x, self.bag_y)
def query(self, points):
"""
Estimate a set of test points given the model we built.
:param points: A numpy array with each row corresponding to a specific query.
:type points: numpy.ndarray
:return: The predicted result of the input data according to the trained model
:rtype: numpy.ndarray
"""
y_pred_results = []
# For each learner in the self.learners collection, it calls the query method of that learner with the provided points.
# The results of these queries are collected into a list and mode of that list is returned.
for learner in self.learners:
y_pred_results.append(learner.query(points))
return stats.mode(y_pred_results, axis = 0)[0][0]
if __name__ == "__main__":
print("Running Bag Learner")