There's more...

Here, we will showcase training a model using AdaBoost with a support vector machine (SVM) as the base learner. 

By default, AdaBoost uses a decision tree as the base learner. We can use different base learners as well. In the following example, we have used an SVM as our base learner with the AdaBoost algorithm. We use SVC with rbf as the kernel:

from sklearn.svm import SVC

Adaboost_with_svc_rbf = AdaBoostClassifier(n_estimators=100, base_estimator=SVC(probability=True, kernel='rbf'), learning_rate=1, random_state=0)
Adaboost_with_svc_rbf.fit(X_train, Y_train)

We can check the accuracy and the AUC values of our AdaBoost model with support vector classifier (SVC) as the base learner:

# Mean accuracy
print('The mean accuracy is: ',(Adaboost_with_svc_rbf.score(X_test,Y_test))*100,'%')

#AUC score
y_pred_svc_rbf = Adaboost_with_svc_rbf.predict_proba(X_test)
fpr_svc_rbf, tpr_svc_rbf, thresholds = roc_curve(Y_test, y_pred_svc_rbf[:,1])
auc_svc_rbf = auc(fpr_svc_rbf, tpr_svc_rbf)
print ('AUC Value: ', auc_svc_rbf)

We noticed that the accuracy and AUC values fall to 62.57 and 0.92, respectively.

Now, we will rebuild our AdaBoost model with SVC. This time, we will use a linear kernel:

Adaboost_with_svc_linear =AdaBoostClassifier(n_estimators=100, base_estimator=SVC(probability=True, kernel='linear'), learning_rate=1, random_state=0)
Adaboost_with_svc_linear.fit(X_train, Y_train)

We now get a mean accuracy of 90.64% and a decent AUC value of 0.96.

We will now plot a graph to compare the AUC value of each model using the following code:

import matplotlib.pyplot as plt
% matplotlib inline
plt.figure(figsize=(8,8))

plt.plot(fpr_dtree, tpr_dtree,label="Model1: Dtree, auc="+str(auc_dtree))
plt.plot(fpr_ab, tpr_ab,label="Model2: Adaboost, auc="+str(auc_adaboost))
plt.plot(fpr_ab_tune,tpr_ab_tune,label="Model3: Adaboost with Tuning, auc="+str(auc_adaboost_tune))
plt.plot(fpr_svc_rbf, tpr_svc_rbf, label="Model4: Adaboost with SVC (RBF Kernel), auc="+str(auc_svc_rbf))
plt.plot(fpr_svc_lin, tpr_svc_lin, label="Model5: Adaboost with SVC (Linear Kernel), auc="+str(auc_svc_linear))

plt.legend(loc=5)
plt.show()

This gives us the following plot:

We can also plot the accuracy of all the models with the following code:

import matplotlib.pyplot as plt
% matplotlib inline
plt.figure(figsize=(8,8))

label = ['Decison Tree', 'Adaboost', 'Adaboost with Tuning', 'Adaboost with SVC (RBF)', 'Adaboost with SVC (Linear)']

values = [dtree.score(X_test,Y_test),
AdaBoost.score(X_test,Y_test),
AdaBoost_with_tuning.score(X_test,Y_test),
Adaboost_with_svc_rbf.score(X_test,Y_test),
Adaboost_with_svc_linear.score(X_test,Y_test)]

def plot_bar_accuracy():
# this is for plotting purpose
index = np.arange(len(label))
plt.bar(index, values)
plt.xlabel('Algorithms', fontsize=10)
plt.ylabel('Accuracy', fontsize=10)
plt.xticks(index, label, fontsize=10, rotation=90)
plt.title('Model Accuracies')
plt.show()

plot_bar_accuracy()

This gives us the following output:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset