-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathmultivotelist.py
More file actions
82 lines (66 loc) · 2.83 KB
/
multivotelist.py
File metadata and controls
82 lines (66 loc) · 2.83 KB
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
77
78
79
80
81
82
# -*- coding: utf-8 -*-
#
# Copyright 2017-2025 BigML
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Auxiliar class for lists of predictions combination.
"""
import logging
from bigml.util import PRECISION
LOGGER = logging.getLogger('BigML')
class MultiVoteList():
"""A multiple vote prediction in compact format
Uses a number of predictions to generate a combined prediction.
The input should be an ordered list of probability, counts or confidences
for each of the classes in the objective field.
"""
def __init__(self, predictions):
"""Init method, builds a MultiVoteList with a list of predictions
The constuctor expects a list of well formed predictions like:
[0.2, 0.34, 0.48] which might correspond to confidences of
three different classes in the objective field.
"""
if isinstance(predictions, list):
self.predictions = predictions
else:
raise ValueError("Expected a list of values to create a"
"MultiVoteList. Found %s instead" % predictions)
def extend(self, predictions_list):
"""Extending the extend method in lists
"""
if isinstance(predictions_list, MultiVoteList):
predictions_list = predictions_list.predictions
self.predictions.extend(predictions_list)
def append(self, prediction):
"""Extending the append method in lists
"""
self.predictions.append(prediction)
def combine_to_distribution(self, normalize=True):
"""Receives a list of lists. Each element is the list of probabilities
or confidences
associated to each class in the ensemble, as described in the
`class_names` attribute and ordered in the same sequence. Returns the
probability obtained by adding these predictions into a single one
by adding their probabilities and normalizing.
"""
total = 0.0
output = [0.0] * len(self.predictions[0])
for distribution in self.predictions:
for i, vote_value in enumerate(distribution):
output[i] += vote_value
total += vote_value
if not normalize:
total = len(self.predictions)
for i, value in enumerate(output):
output[i] = round(value / total, PRECISION)
return output