Skip to content

Instantly share code, notes, and snippets.

@yuempark
Last active October 9, 2019 18:38
Show Gist options
  • Save yuempark/d4d85ecd98759815af05f8bda834d800 to your computer and use it in GitHub Desktop.
Save yuempark/d4d85ecd98759815af05f8bda834d800 to your computer and use it in GitHub Desktop.
[bagging and pasting] #sklearn #machinelearning #classification #ensemble
from sklearn.ensemble import BaggingClassifier # or BaggingRegressor for regression
from sklearn.tree import DecisionTreeClassifier
# set bootstrap to False if you want to do pasting (sampling without replacement)
# n_jobs sets how many cores to use (-1 means all)
# oob_score is the out-of-bag score
bag_clf = BaggingClassifier(DecisionTreeClassifier(),
n_estimators=500,
max_samples=100,
bootstrap=True,
n_jobs=-1,
bootstrap_features=True,
max_features=4,
oob_score=True)
bag_clf.fit(X_train, y_train)
y_pred = bag_clf.predict(X_test)
# out-of-bag score
bag_clf.oob_score_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment