Majority vote of classifiers

An ensemble's simplest rule: let several classifiers vote. Compare the vote with each model on its own.

What to build

A program that scores several classifiers and their majority vote.

Requirements

  • For each model, print model i: A, its accuracy with 3 decimal places (models counted from 1).
  • The vote predicts, for each example, the label most models predict; on a tie, the tied label of the earliest model.
  • Finally print vote: A, the vote's accuracy.

Input

Line 1: m, the number of models. Then m lines of predictions. Then a line of true labels. All lines have the same number of labels (words).

Output

m + 1 lines.

Constraints

1 ≤ m ≤ 25; 1 to 1000 examples.

Example

Input:

3
1 0 1 0 0 1 1 0 1 1
1 1 1 1 0 0 0 0 1 0
0 0 1 1 1 1 0 0 0 1
1 0 1 1 0 1 0 0 1 1

Output:

model 1: 0.800
model 2: 0.700
model 3: 0.700
vote: 1.000

Why it works

When the models are each right most of the time and wrong on different examples, the majority is right on examples where any single model fails.

Examples

Example 1 · Input
3
1 0 1 0 0 1 1 0 1 1
1 1 1 1 0 0 0 0 1 0
0 0 1 1 1 1 0 0 0 1
1 0 1 1 0 1 0 0 1 1
Expected output
model 1: 0.800
model 2: 0.700
model 3: 0.700
vote: 1.000
Example 2 · Input
1
a b
a a
Expected output
model 1: 0.500
vote: 0.500

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

For each example, count the labels the models predict and take the most common.

Hint 2

If labels tie, use the tied label predicted by the earliest model.

Solution

Show one correct solution (try it yourself first)
m = int(input())
predictions = [input().split() for _ in range(m)]
truth = input().split()


def accuracy(labels):
    return sum(1 for a, b in zip(labels, truth) if a == b) / len(truth)


for i, p in enumerate(predictions):
    print(f"model {i + 1}: {accuracy(p):.3f}")
vote = []
for column in zip(*predictions):
    counts = {}
    for label in column:
        counts[label] = counts.get(label, 0) + 1
    top = max(counts.values())
    vote.append(next(label for label in column if counts[label] == top))
print(f"vote: {accuracy(vote):.3f}")

Lesson: Ensembles: random forests and boosting →