问题
For a few days I was wondering on how to efficiently implement weighted majority voting of m experts in matlab. Here is an example of what I want. Suppose we have 3 experts with weights vector
w=[7 2 6]
Suppose they are voting n times on the options A/B/C/D, so we for example get the following n x m voting matrix, where the columns are votes of each expert.
A B B
C A A
D B A
A A C
Now I'd like to find weighted majority vote for each row. We calculate it by adding the weights of experts which voted for each option, and selecting the maximal weight. For example, in the first row, the option A has cumulative weight of 7 (vote of expert 1) and B has cumulative weight of 8 (votes of expert 2 and 3), and hence the final vote is B. So we get the following cumulative weights matrix and final votes:
A B C D
- - - -
7 8 0 0 -> B
8 0 7 0 -> A
6 2 0 7 -> D
9 0 6 0 -> A
Now, the implementation of this using for loops over number of rows n is more or less straightforward. I am now looking for solution, which doesn't require this potentially lengthy loop and instead uses vector arithmetic. I have had a few ideas, but ran into some problems with each of them, so will not mention them now. If anyone have had similar situation before, please share your solutions.
Thanks.
回答1:
w=[7 2 6];
votes = ['A' 'B' 'B'
'C' 'A' 'A'
'D' 'B' 'A'
'A' 'A' 'C'];
options = ['A', 'B', 'C', 'D']';
%'//Make a cube of the options that is number of options by m by n
OPTIONS = repmat(options, [1, size(w, 2), size(votes, 1)]);
%//Compare the votes (streched to make surface) against a uniforma surface of each option
B = bsxfun(@eq, permute(votes, [3 2 1]) ,OPTIONS);
%//Find a weighted sum
W = squeeze(sum(bsxfun(@times, repmat(w, size(options, 1), 1), B), 2))'
%'//Find the options with the highest weighted sum
[xx, i] = max(W, [], 2);
options(i)
result:
B
A
D
A
来源:https://stackoverflow.com/questions/15498368/efficient-multiclass-weighted-majority-voting-implementation-in-matlab