Change language

ML | Vote classifier using Sklearn

The voting classifier supports two types of voting.

  1. Hard voting. In hard voting, the predicted output class is — it is the class with the highest majority of votes, that is, the class that has the highest probability of being predicted by each of the classifiers. Suppose three classifiers predicted output class (A, A, B) , so here most people predicted A as output. Therefore, A will be the final prediction.
  2. Soft vote: With soft vote, the output class is — it is a prediction based on the average probability given to this class. Suppose given some input for three models: the prediction probability for the class A = (0.30, 0.47, 0.53) and B = (0.20, 0, 32, 0.40) . Thus, the average for class A is 0.4333, and B — 0.3067 , the winner is clearly A, as it had the highest probability averaged over each classifier.

Note. Make sure you include the different models for the submission of the qualifier to vote to ensure that a mistake made by one can be corrected by the other. 
Code: Python code to implement the voting classifier

# import libraries

from sklearn.ensemble import VotingClassifier

from sklearn.linear_model import LogisticRegression

from sklearn.svm import SVC

from sklearn.tree import DecisionTreeClassifier

from sklearn.datasets impor t load_iris

from sklearn.metrics import accuracy_score

from sklearn.model_selection import train_test_split

 
# load the iris dataset

iris = load_iris ()

X = iris.data [:,: 4 ]

Y = iris. target

 
# train_test_split

X_train, X_test, y_train, y_test = train_test_split (X, 

Y, 

test_size = 0.20

random_state = 42 )

  
# model group / ensemble

estimator = []

estimator.append (( ’ LR’

LogisticRegression (solver = ’lbfgs’

  multi_class = ’multinomial’

max_iter = 200 )))

estimator.append (( ’SVC’ , SVC (gamma = ’auto’ , probability = True )))

estimator.append (( ’DTC ’ , DecisionTreeClassifier ()))

  
# Vote classifier with gesture by vote

vot_hard = VotingClassifier (estimators = estimator, voting = ’ hard’ )

vot_hard.fit (X_train, y_train)

y_pred = vot_hard.predict (X_test)

 
# using precision_score metric to predict accuracy

score = accuracy_score (y_test, y_pred)

print ( "Hard Voting Score% d" % score)

  
# Soft vote classifier

vot_soft = VotingClassifier (estimators = estimator , voting = ’soft’ )

vot_soft.fit (X_train, y_train)

y_pred = vot_soft.predict (X_test)

  
# using precision_score

score = accuracy_score (y_test, y_pred)

print ( "Soft Voting Score% d" % score)

Exit :

 Hard Voting Score 1 Soft Voting Score 1 

Examples :

  Input: 4.7, 3.2, 1.3, 0.2   Output: Iris Setosa  

In practice, the output accuracy will be higher for a soft vote, since this is the average probability of all evaluators combined, since for we are already retraining our basic iris dataset, so there won’t be much difference in the output.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically