We will try to combine three basic regression algorithms by voting to improve the MSE of the simple regression. To combine the algorithms, we will utilize the average of their predictions. Thus, we code a simple class that creates a dictionary of base learners and handles their training and prediction averaging. The main logic is the same as with the custom voting classifier we implemented in Chapter 3, Voting:
import numpy as np
from copy import deepcopy
class VotingRegressor():
# Accepts a list of (name, classifier) tuples
def __init__(self, base_learners):
self.base_learners = {}
for name, learner in base_learners:
self.base_learners[name] = deepcopy(learner)
# Fits each individual base learner
def fit(self, x_data, y_data):
for name in self.base_learners:
learner = self.base_learners[name]
learner.fit(x_data, y_data)
The predictions are stored in a NumPy matrix, in which...