Recipe: How to implement voting ensemble in Python?
MACHINE LEARNING RECIPES

How to implement voting ensemble in Python?

This recipe helps you implement voting ensemble in Python
In [1]:
## How to implement voting ensemble in Python
## DataSet: skleran.datasets.load_breast_cancer()
def Snippet_184():
    print()
    print(format('How to implement voting ensemble in Python','*^82'))
    import warnings
    warnings.filterwarnings("ignore")
    # load libraries
    from sklearn import model_selection
    from sklearn.linear_model import LogisticRegression
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.svm import SVC
    from sklearn.ensemble import VotingClassifier
    from sklearn import datasets
    from sklearn.model_selection import train_test_split
    import matplotlib.pyplot as plt

    plt.style.use('ggplot')

    # load datasets
    seed = 42
    dataset = datasets.load_breast_cancer()
    X = dataset.data; y = dataset.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
    kfold = model_selection.KFold(n_splits=10, random_state=seed)

    # create different models
    estimators = []
    model1 = LogisticRegression(); estimators.append(('logistic', model1))
    model2 = DecisionTreeClassifier(); estimators.append(('cart', model2))
    model3 = SVC(); estimators.append(('svm', model3))

    # create the ensemble model
    ensemble = VotingClassifier(estimators)
    results = model_selection.cross_val_score(ensemble, X_train, y_train, cv=kfold)
    print(); print(results.mean())

Snippet_184()
********************How to implement voting ensemble in Python********************

0.9462901439645626


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe