Bagging with weak classifiers

Bagging works with samples in a way that is similar to that of pasting, but it allows replacement. Also, theoretically elaborated by Leo Breiman, bagging is implemented in a specific scikit-learn class for regression and one for classification. You just have to decide the algorithm that you'd like to use for the training. Plug it into BaggingClassifier, or BaggingRegressor for regression problems, and set a sufficiently high number of estimators (and consequently a high number of samples):

In: import numpy as np    from sklearn.model_selection import cross_val_score    from sklearn.ensemble import BaggingClassifier    from sklearn.neighbors import KNeighborsClassifier    hypothesis = BaggingClassifier(KNeighborsClassifier(n_neighbors=1), ...

Get Python Data Science Essentials - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.