A Random Forest makes predictions by aggregating the outputs of multiple decision trees. For classification, each tree votes for a class and the final prediction is the class with the most votes (majority vote).
Given the predictions from T decision trees for N samples, compute the majority vote for each sample. Break ties by choosing the smallest class label.
For each sample, count votes from all trees
Select the class with the highest vote count
If multiple classes are tied, pick the smallest class label
Input:
predictions = [[0, 1, 0], [0, 1, 1], [0, 0, 0]]
Output:
[0, 1, 0]
Sample 0: votes {0:3} = 0. Sample 1: votes {1:2, 0:1} = 1. Sample 2: votes {0:2, 1:1} = 0.
Input:
predictions = [[0, 1], [1, 0]]
Output:
[0, 0]
Both samples have a 1-1 tie between classes 0 and 1. Ties are broken by choosing the smallest label, so both predict 0.
Loop over each sample index i. For each i, count how many times each class appears across all trees using a dictionary. Then find the class with the highest count.
To break ties by smallest label, find the max count first, then use min() over all keys that have that count.
Sign in to take notes on this problem
Accepts: array
A Random Forest makes predictions by aggregating the outputs of multiple decision trees. For classification, each tree votes for a class and the final prediction is the class with the most votes (majority vote).
Given the predictions from T decision trees for N samples, compute the majority vote for each sample. Break ties by choosing the smallest class label.
For each sample, count votes from all trees
Select the class with the highest vote count
If multiple classes are tied, pick the smallest class label
Input:
predictions = [[0, 1, 0], [0, 1, 1], [0, 0, 0]]
Output:
[0, 1, 0]
Sample 0: votes {0:3} = 0. Sample 1: votes {1:2, 0:1} = 1. Sample 2: votes {0:2, 1:1} = 0.
Input:
predictions = [[0, 1], [1, 0]]
Output:
[0, 0]
Both samples have a 1-1 tie between classes 0 and 1. Ties are broken by choosing the smallest label, so both predict 0.
Loop over each sample index i. For each i, count how many times each class appears across all trees using a dictionary. Then find the class with the highest count.
To break ties by smallest label, find the max count first, then use min() over all keys that have that count.
Sign in to take notes on this problem
Accepts: array