from sklearn.ensemble import VotingClassifier from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.svm import SVC clf1 = LogisticRegression() clf2 = DecisionTreeClassifier() clf3 = SVC(probability=True) ensemble_clf = VotingClassifier(estimators=[('lr', clf1), ('dt', clf2), ('svc', clf3)], voting='soft') ensemble_clf.fit(X_train, y_train)
from sklearn.ensemble import VotingClassifier from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.svm import SVC clf1 = LogisticRegression() clf2 = DecisionTreeClassifier() clf3 = SVC() ensemble_clf = VotingClassifier(estimators=[('lr', clf1), ('dt', clf2), ('svc', clf3)], voting='hard') ensemble_clf.fit(X_train, y_train)In this example, we are using the same three classifiers with hard voting instead of soft voting. Overall, the sklearn.ensemble package in Python provides powerful tools for creating ensemble classifiers. The VotingClassifier is just one of the many classifiers available in this library, which also includes Random Forests, Bagging, AdaBoost, and Gradient Boosting.