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

How to do it...

You can create a voting ensemble model for a classification problem using the VotingClassifier class from Python's scikit-learn library. The following steps showcase an example of how to combine the predictions of the decision tree, SVMs, and logistic regression models for a classification problem:

  1. Import the required libraries for building the decision tree, SVM, and logistic regression models. We also import VotingClassifier for max-voting:
# Import required libraries
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import VotingClassifier
  1. We then move on to building our feature set and creating our train and test datasets:
# We create train & test sample from our dataset
from sklearn.cross_validation import train_test_split

# create feature & response sets
feature_columns = ['sex', 'age', 'Time', 'Number_of_Warts', 'Type', 'Area']
X = df_cryotherapydata[feature_columns]
Y = df_cryotherapydata['Result_of_Treatment']

# Create train & test sets
X_train, X_test, Y_train, Y_test = \
train_test_split(X, Y, test_size=0.20, random_state=1)

  1. We build our models with the decision tree, SVM, and logistic regression algorithms:
# create the sub models
estimators = []

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

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

logit_model = LogisticRegression(random_state=1)
estimators.append(('Logistic Regression', logit_model))
  1. We build individual models with each of the classifiers we've chosen:
from sklearn.metrics import accuracy_score

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))

We can then see the accuracy score of each of the individual base learners:

  1. We proceed to ensemble our models and use VotingClassifier to score the accuracy of the ensemble model:
#Using VotingClassifier() to build ensemble model with Hard Voting
ensemble_model = VotingClassifier(estimators=estimators, voting='hard')

ensemble_model.fit(X_train,Y_train)
predicted_labels = ensemble_model.predict(X_test)

print("Classifier Accuracy using Hard Voting: ", accuracy_score(Y_test, predicted_labels))

We can see the accuracy score of the ensemble model using Hard Voting:

主站蜘蛛池模板: 泊头市| 昌都县| 洛宁县| 腾冲县| 康马县| 丽江市| 肥东县| 勃利县| 扬州市| 启东市| 平顺县| 莫力| 陆良县| 桑日县| 贵定县| 临夏县| 句容市| 桐城市| 西贡区| 开阳县| 舞钢市| 扎赉特旗| 错那县| 右玉县| 岑溪市| 永吉县| 澄江县| 江都市| 封开县| 七台河市| 淅川县| 杭锦旗| 开封县| 天全县| 银川市| 石景山区| 黄浦区| 神池县| 陈巴尔虎旗| 淮南市| 沛县|