Probably Approximately Correct

MSc Machine Learning @ UCL | Alumnus @ IIT Madras| Google DeepMind Scholar | Interests: Machine learning

Sunday, December 24, 2023

Classifier Voting Mechanism for Multi-Class Decision Making : #High_Performance_Python(Post_2)

 


 

In classification tasks, two types of ensemble methods are utilized: hard voting and soft voting. Hard voting operates by collating the final class labels from a range of models and selecting the class that receives the majority of votes. Conversely, soft voting takes into account the predicted probabilities for each class label from various models. In this approach, the probabilities for each class are accumulated, and the class with the highest overall probability is selected as the prediction. Today, we shall explore hard voting through a 'toy' example.

 

The below code implements a Hard voting mechanism commonly used in ensemble machine learning methods, especially in scenarios involving multi-class classification. 




combinations = [pair for pair in itertools.combinations(range(3), 2)]
print("combination Oloop:",combinations)
def voting(output):
    vote_n = np.zeros(10)                        
    vote = ((np.sign(output) + 1) // 2).astype(int).tolist()
    print(vote)
    for i,j in enumerate(vote):
        print(i,j)
        print("combinations:",combinations[i][j])
        vote_n[combinations[i][j]] +=1  
        print("vote:",vote_n)
    return np.argmax(vote_n)   
output = [-1 if i % 2 == 0 else 1 for i in range(3)]
results = voting(output)


Output :

combination Oloop: [(0, 1), (0, 2), (1, 2)]
[0, 1, 0]
0 0
combinations: 0
vote: [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
1 1
combinations: 2
vote: [1. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
2 0
combinations: 1
vote: [1. 1. 1. 0. 0. 0. 0. 0. 0. 0.]



The combinations list is formulated utilising itertools.combinations, which assembles all conceivable pairings of classes from a set of three classes, namely 0, 1, and 2. The purpose of the voting function is to evaluate the outputs from various classifiers and ascertain the class receiving the majority of votes. Within this function, vote_n is a NumPy array, initially filled with zeros and dimensioned to tally votes for each class, presuming a total of ten classes. The vote process transforms the given output list into binary votes—0 or 1—based on their sign; thus, positive values in the output correspond to a vote of 1, while negative values yield a vote of 0.

 

As the function proceeds, it methodically iterates through these binary votes. Each vote is scrutinised, and the corresponding class pair from combinations is identified, subsequently incrementing the vote tally for the chosen class in vote_n. Following the accumulation of all votes, the function determines the class index with the highest vote count, employing np.argmax(vote_n) to do so.







Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home