Created
December 1, 2019 00:06
-
-
Save Mehdi-Amine/ac7b32ff686d28a0631b6a7a88ae0039 to your computer and use it in GitHub Desktop.
A bagging classifier.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bagging creates several models that rely on the same algorithm. | |
# The training of each model uses a different subset of data sampled randomly from the training set. | |
# By default Bagging uses soft voting when its base estimator can provide its measure of confidence, | |
# Hence the SVC model is set to have probability=True | |
from sklearn.svm import SVC | |
from sklearn.ensemble import BaggingClassifier | |
bagging_clf = BaggingClassifier(SVC(gamma='scale', probability=True, random_state=42), | |
bootstrap=True, # set to False to use Pasting instead of Bagging | |
n_estimators=100, # number of SVC models to create | |
max_samples=100, # each model is trained from randomly sampled 100 instances | |
random_state=42 | |
) | |
bagging_clf.fit(X_train, y_train) # training | |
y_pred_bagging = bagging_clf.predict(X_test) # predicting | |
accuracy_score(y_test, y_pred_bagging) # evaluating | |
# Output of the evaluation: 0.904 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment