Skip to content
This repository has been archived by the owner on Jul 27, 2018. It is now read-only.

#14 refactored combine method to support weighted combination rules #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions brew/combination/combiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class Combiner(object):

__VALID_WEIGHTED_COMBINATION_RULES = [
rules.majority_vote_rule,
rules.mean_rule,
]

def __init__(self, rule='majority_vote'):
self.combination_rule = rule

Expand All @@ -25,13 +30,38 @@ def __init__(self, rule='majority_vote'):
else:
raise Exception('invalid argument rule for Combiner class')

def combine(self, results):
def combine(self, results, weights=None):
"""
This method puts together the results of all classifiers
based on a pre-selected combination rule.

Parameters
----------
results: array-like, shape = [n_samples, n_classes, n_classifiers]
If combination rule is 'majority_vote' results should be Ensemble.output(X, mode='votes')
Otherwise, Ensemble.output(X, mode='probs')
weights: array-like, optional(default=None)
Weights of the classifiers. Must have the same size of n_classifiers.
Applies only to 'majority_vote' and 'mean' combination rules.
"""

nresults = results.copy().astype(float)
n_samples = nresults.shape[0]
y_pred = np.zeros((n_samples,))

n_samples = results.shape[0]
if weights is not None:
# verify valid combination rules
if self.rule in __VALID_WEIGHTED_COMBINATION_RULES:
# verify shapes
if weights.shape[0] != nresults.shape[2]:
raise Exception(
'weights and classifiers must have same size')

out = np.zeros((n_samples,))
# apply weights
for i in range(nresults.shape[2]):
nresults[:, :, i] = nresults[:, :, i] * weights[i]

for i in range(n_samples):
out[i] = self.rule(results[i, :, :])
y_pred[i] = self.rule(nresults[i, :, :])

return out
return y_pred