<jupyter_start><jupyter_text>Bagging ClassifierA Bagging classifier is an ensemble meta-estimator that fits base classifiers each on random subsets of the original dataset and then aggregate their individual predictions (either by voting or by averaging) to form a final prediction. Such a meta-estimator can typically be used as a way to reduce the variance of a black-box estimator (e.g., a decision tree), by introducing randomization into its construction procedure and then making an ensemble out of it.Each base classifier is trained in parallel with a training set which is generated by randomly drawing, with replacement, N examples(or data) from the original training dataset – where N is the size of the original training set. Training set for each of the base classifiers is independent of each other. Many of the original data may be repeated in the resulting training set while others may be left out.Bagging reduces overfitting (variance) by averaging or voting, however, this leads to an increase in bias, which is compensated by the reduction in variance though. How Bagging works on training dataset ?How bagging works on an imaginary training dataset is shown below. Since Bagging resamples the original training dataset with replacement, some instance(or data) may be present multiple times while others are left out.The algorithm for the classifier generation and classification is given below Classifier generation:Let N be the size of the training set.for each of t iterations:    sample N instances with replacement from the original training set.    apply the learning algorithm to the sample.    store the resulting classifier.Classification:for each of the t classifiers:     predict class of instance using classifier.return class that was predicted most often.<jupyter_code>from sklearn import model_selection
from sklearn.datasets import load_iris
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
import pandas as pd

# load the data
iris = load_iris()
X = iris.data[:, :4]
Y = iris.target

seed = 2
kfold = model_selection.KFold(n_splits = 3,random_state = seed, shuffle= True)

# initialize the base classifier
base_cls = DecisionTreeClassifier()

# no. of base classifier
num_trees = 500

# bagging classifier
model = BaggingClassifier(base_estimator = base_cls,
						n_estimators = num_trees,
						random_state = seed)

results = model_selection.cross_val_score(model, X, Y, cv = kfold)
print("accuracy :")
print(results.mean())<jupyter_output>accuracy :
0.94
{'fraction_non_alphanumeric': 0.044488188976377956, 'fraction_numerical': 0.003543307086614173, 'mean_word_length': 5.108173076923077, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '147899', 'license': 'MIT', 'n_tokens_mistral': 652, 'n_tokens_neox': 610, 'n_words': 335, 'path': 'Bagging_in_Classification.ipynb', 'repo_name': 'Benjamindavid03/MachineLearningLab', 'size': '2540'}