官术网_书友最值得收藏!

There's more...

Many classifiers can estimate class probabilities. In this case, the class labels are predicted by averaging the class probabilities. This is called soft voting and is recommended for an ensemble of well-tuned classifiers.

In the scikit-learn library, many classification algorithms have the predict_proba() method to predict the class probabilities. To perform the ensemble with soft voting, simply replace voting='hard' with voting='soft' in VotingClassifier().

The following code creates an ensemble using soft voting:

# create the sub models
estimators = []

dt_model = DecisionTreeClassifier(random_state=1)
estimators.append(('DecisionTree', dt_model))

svm_model = SVC(random_state=1, probability=True)
estimators.append(('SupportVector', svm_model))

logit_model = LogisticRegression(random_state=1)
estimators.append(('Logistic Regression', logit_model))

for each_estimator in (dt_model, svm_model, logit_model):
each_estimator.fit(X_train, Y_train)
Y_pred = each_estimator.predict(X_test)
print(each_estimator.__class__.__name__, accuracy_score(Y_test, Y_pred))

# Using VotingClassifier() to build ensemble model with Soft Voting
ensemble_model = VotingClassifier(estimators=estimators, voting='soft')
ensemble_model.fit(X_train,Y_train)
predicted_labels = ensemble_model.predict(X_test)
print("Classifier Accuracy using Soft Voting: ", accuracy_score(Y_test, predicted_labels))

We get to see the accuracy from individual learners and the ensemble learner using soft voting:

The SVC class can't estimate class probabilities by default, so we've set its probability hyper-parameter to  True in the preceding code. With probability=True, SVC will be able to estimate class probabilities.
主站蜘蛛池模板: 扎鲁特旗| 阿瓦提县| 乌拉特中旗| 济南市| 铜鼓县| 元朗区| 庄河市| 井冈山市| 贡嘎县| 尼木县| 长治县| 普陀区| 公主岭市| 敦化市| 岑溪市| 宜宾市| 策勒县| 九龙城区| 定兴县| 瑞昌市| 宜阳县| 南溪县| 马龙县| 大新县| 唐河县| 望奎县| 社会| 阿图什市| 淳安县| 安平县| 南皮县| 葵青区| 西平县| 虎林市| 黎城县| 荆门市| 柏乡县| 岳普湖县| 秦安县| 济宁市| 美姑县|