<jupyter_start><jupyter_text>oob和更多Bagging相关<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets

X, y = datasets.make_moons(n_samples=500, noise=0.3, random_state=42) 
plt.scatter(X[y==0,0], X[y==0,1])
plt.scatter(X[y==1,0], X[y==1,1])
plt.show()<jupyter_output><empty_output><jupyter_text>oob<jupyter_code>from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import BaggingClassifier

bagging_clf = BaggingClassifier(DecisionTreeClassifier(),
                               n_estimators=500, max_samples=100,
                               bootstrap=True, oob_score=True)
bagging_clf.fit(X, y)
bagging_clf.oob_score_<jupyter_output><empty_output><jupyter_text>n_jobs<jupyter_code>%%time
bagging_clf = BaggingClassifier(DecisionTreeClassifier(),
                               n_estimators=500, max_samples=100,
                               bootstrap=True, oob_score=True)
bagging_clf.fit(X, y)
%%time
bagging_clf = BaggingClassifier(DecisionTreeClassifier(),
                               n_estimators=500, max_samples=100,
                               bootstrap=True, oob_score=True,
                               n_jobs=-1)
bagging_clf.fit(X, y)<jupyter_output>CPU times: user 385 ms, sys: 56.1 ms, total: 441 ms
Wall time: 1.83 s<jupyter_text>bootstrap_features<jupyter_code>random_subspaces_clf = BaggingClassifier(DecisionTreeClassifier(),
                               n_estimators=500, max_samples=500,
                               bootstrap=True, oob_score=True,
                               max_features=1, bootstrap_features=True)
random_subspaces_clf.fit(X, y)
random_subspaces_clf.oob_score_
random_patches_clf = BaggingClassifier(DecisionTreeClassifier(),
                               n_estimators=500, max_samples=100,
                               bootstrap=True, oob_score=True,
                               max_features=1, bootstrap_features=True)
random_patches_clf.fit(X, y)
random_patches_clf.oob_score_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09265648102513553, 'fraction_numerical': 0.02957121734844751, 'mean_word_length': 2.888888888888889, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '530958', 'license': 'MulanPSL-1.0', 'n_tokens_mistral': 732, 'n_tokens_neox': 682, 'n_words': 91, 'path': 'Part5Improve/13-Ensemble-Learning-and-Random-Forest/04-OOB-and-More-about-Bagging-Classifier/04-OOB-and-More-About-Bagging-Classifier.ipynb', 'repo_name': '19920625lsg/itcast-python', 'size': '2029'}