<jupyter_start><jupyter_text>**Bagging Machine Learning Algorithm***   Scikit-Learn Reference*   Bootstrap sampling:Sampling with replacement*   Combine by averaging the output (regression)*   Combine by voting (Classification)*   Can be applied to many classifiers which includes ANN,CART,etc.<jupyter_code>import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
df= sns.load_dataset('titanic')

df.shape
df.head()
# df.dropna(inplace=True)
df['pclass'].unique()
df['pclass'].value_counts( )
df['sex'].unique()
df['sex'].value_counts()
df['age'].hist(bins=50)<jupyter_output><empty_output><jupyter_text>**Data Pre**-**processing**<jupyter_code>subset=df[['pclass','sex','age','survived']].copy()
subset.dropna(inplace=True)
X=subset[['pclass','sex','age']].copy()
from sklearn import preprocessing
#lb= preprocessing.LabelBinarizer()
le= preprocessing.LabelEncoder()
X['sex']= le.fit_transform(subset['sex'])
X.head()
X.shape
X.describe()
X.info()
y=subset['survived'].copy()
y.value_counts()<jupyter_output><empty_output><jupyter_text>**Fit Model**<jupyter_code>from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test= train_test_split(X,y,test_size=0.3)
from sklearn import preprocessing
from sklearn.model_selection import cross_val_score, cross_val_predict
from sklearn.metrics import accuracy_score,classification_report
from sklearn.metrics import confusion_matrix, roc_auc_score
def print_score(clf,X_train,X_test,y_train,y_test,train=True):
  lb= preprocessing.LabelBinarizer()
  lb.fit(y_train)
  if train:

    ''' training performance'''
    res= clf.predict(X_train)
    print("Train Result:\n")
    print("accuracy score: {0:.4f}\n".format(accuracy_score(y_train,res)))
    print("classification Report: \n {}\n".format(classification_report(y_train,res)))
    print("Confusion Matrix: \n {}\n".format(confusion_matrix(y_train,res)))
    print("ROC AUC: {0:.4f}\n".format(roc_auc_score(lb.transform(y_train),lb.transform(res))))
    res= cross_val_score(clf,X_train,y_train,cv=10,scoring='accuracy')
    print("Average Accuracy: \t {0:.4f}".format(np.mean(res)))
    print("Accuracy SD: \t\t {0:.4f}".format(np.std(res)))

  elif train==False:
    ''' Test Performance'''
    res_test= clf.predict(X_test)
    print("Train Result:\n")
    print("accuracy score: {0:.4f}\n".format(accuracy_score(y_test,res_test)))
    print("classification Report: \n {}\n".format(classification_report(y_test,res_test)))
    print("Confusion Matrix: \n {}\n".format(confusion_matrix(y_test,res_test)))
    print("ROC AUC: {0:.4f}\n".format(roc_auc_score(lb.transform(y_test),lb.transform(res_test))))<jupyter_output><empty_output><jupyter_text>**Decision Tree**<jupyter_code>clf= DecisionTreeClassifier(random_state=42)
clf.fit(X_train,y_train)
DecisionTreeClassifier(class_weight=None,criterion='gini',max_depth=None,max_features=None,max_leaf_nodes=None,min_impurity_decrease=0.0,min_impurity_split=None,
                       min_samples_leaf=1,min_samples_split=2,min_weight_fraction_leaf=0.0,presort=False,random_state=42,splitter='best')
print_score(clf, X_train, X_test, y_train, y_test, train=True)
print("\n*********************************")
print_score(clf, X_train, X_test, y_train, y_test, train=False)<jupyter_output>Train Result:

accuracy score: 0.8958

classification Report: 
               precision    recall  f1-score   support

           0       0.87      0.98      0.92       300
           1       0.96      0.77      0.86       199

    accuracy                           0.90       499
   macro avg       0.91      0.88      0.89       499
weighted avg       0.90      0.90      0.89       499


Confusion Matrix: 
 [[293   7]
 [ 45 154]]

ROC AUC: 0.8753

Average Accuracy: 	 0.7916
Accuracy SD: 		 0.0523

*********************************
Train Result:

accuracy score: 0.8233

classification Report: 
               precision    recall  f1-score   support

           0       0.80      0.93      0.86       124
           1       0.87      0.68      0.77        91

    accuracy                           0.82       215
   macro avg       0.84      0.80      0.81       215
weighted avg       0.83      0.82      0.82       215


Confusion Matrix: 
 [[115   9]
 [ 29  62]]

ROC AUC: 0.8044<jupyter_text>**Bagging(oob_score=False)**<jupyter_code>bag_clf= BaggingClassifier(base_estimator=clf, n_estimators=1000,bootstrap=True,n_jobs=-1,random_state=42)
bag_clf.fit(X_train,y_train)
print_score(clf, X_train, X_test, y_train, y_test, train=True)
print("\n*********************************")
print_score(clf, X_train, X_test, y_train, y_test, train=False)<jupyter_output>Train Result:

accuracy score: 0.8958

classification Report: 
               precision    recall  f1-score   support

           0       0.87      0.98      0.92       300
           1       0.96      0.77      0.86       199

    accuracy                           0.90       499
   macro avg       0.91      0.88      0.89       499
weighted avg       0.90      0.90      0.89       499


Confusion Matrix: 
 [[293   7]
 [ 45 154]]

ROC AUC: 0.8753

Average Accuracy: 	 0.7916
Accuracy SD: 		 0.0523

*********************************
Train Result:

accuracy score: 0.8233

classification Report: 
               precision    recall  f1-score   support

           0       0.80      0.93      0.86       124
           1       0.87      0.68      0.77        91

    accuracy                           0.82       215
   macro avg       0.84      0.80      0.81       215
weighted avg       0.83      0.82      0.82       215


Confusion Matrix: 
 [[115   9]
 [ 29  62]]

ROC AUC: 0.8044<jupyter_text>**Bagging(oob_score=True)**<jupyter_code>bag_clf= BaggingClassifier(base_estimator=clf, n_estimators=1000,bootstrap=True,oob_score=True,n_jobs=-1,random_state=42)
bag_clf.fit(X_train,y_train)
print_score(clf, X_train, X_test, y_train, y_test, train=True)
print("\n*********************************")
print_score(clf, X_train, X_test, y_train, y_test, train=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14591093117408907, 'fraction_numerical': 0.05862348178137652, 'mean_word_length': 3.306834030683403, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '278619', 'license': 'Apache-2.0', 'n_tokens_mistral': 2395, 'n_tokens_neox': 2149, 'n_words': 409, 'path': 'Bagging.ipynb', 'repo_name': 'Pouryamec/UCI-Data-Analysis', 'size': '6175'}