Class Probabilities Calculated by the VotingClassifier in Scikit-learn

Plot the class probabilities of the first sample in a toy dataset predicted by three different classifiers and averaged by the VotingClassifier.

First, three examplary classifiers are initialized (LogisticRegression, GaussianNB, and RandomForestClassifier) and used to initialize a soft-voting

VotingClassifier with weights [1, 1, 5], which means that the predicted probabilities of the RandomForestClassifier count 5 times as much as the weights of the other classifiers when the averaged probability is calculated.

To visualize the probability weighting, we fit each classifier on the training set and plot the predicted class probabilities for the first sample in this example dataset.

clf1=LogisticRegression(random_state=123)clf2=RandomForestClassifier(random_state=123)clf3=GaussianNB()X=np.array([[-1.0,-1.0],[-1.2,-1.4],[-3.4,-2.2],[1.1,1.2]])y=np.array([1,1,2,2])eclf=VotingClassifier(estimators=[('lr',clf1),('rf',clf2),('gnb',clf3)],voting='soft',weights=[1,1,5])# predict class probabilities for all classifiersprobas=[c.fit(X,y).predict_proba(X)forcin(clf1,clf2,clf3,eclf)]# get class probabilities for the first sample in the datasetclass1_1=[pr[0,0]forprinprobas]class2_1=[pr[0,1]forprinprobas]