<jupyter_start><jupyter_text>Ensembling<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

import warnings
warnings.filterwarnings('ignore')
df = pd.read_csv("bank.csv")
df.head()
plt.figure(figsize=(6,6))
df["deposit"].value_counts().plot.pie(autopct="%1.1f%%")
plt.show()
X = df.iloc[:,:-1]
y = df.iloc[:,-1]
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=1)<jupyter_output><empty_output><jupyter_text>Naive Aggregation<jupyter_code>lr = LogisticRegression()
dt1 = DecisionTreeClassifier()
dt2 = DecisionTreeClassifier(criterion="entropy")
model_list = [('lr',lr),('dt1',dt1),('dt2',dt2)]
from sklearn.ensemble import VotingClassifier<jupyter_output><empty_output><jupyter_text>1) Hard voting<jupyter_code>vc1 =  VotingClassifier(estimators=model_list)
vc1.fit(X_train,y_train)
y_pred = vc1.predict(X_test)
print(classification_report(y_test,y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.83      0.83      0.83      1760
           1       0.81      0.81      0.81      1589

   micro avg       0.82      0.82      0.82      3349
   macro avg       0.82      0.82      0.82      3349
weighted avg       0.82      0.82      0.82      3349<jupyter_text>2) Soft voting<jupyter_code>vc2 =  VotingClassifier(estimators=model_list,voting="soft")
vc2.fit(X_train,y_train)
y_pred = vc2.predict(X_test)
print(classification_report(y_test,y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.84      0.83      0.83      1760
           1       0.81      0.82      0.82      1589

   micro avg       0.83      0.83      0.83      3349
   macro avg       0.83      0.83      0.83      3349
weighted avg       0.83      0.83      0.83      3349<jupyter_text>Bootstrap Aggregation<jupyter_code>from sklearn.ensemble import BaggingClassifier<jupyter_output><empty_output><jupyter_text>1) Bagging<jupyter_code>bg = BaggingClassifier(LogisticRegression(), n_estimators=10, max_samples=100,random_state=1)
bg.fit(X_train,y_train)
y_pred = bg.predict(X_test)
print(classification_report(y_test,y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.78      0.83      0.80      1760
           1       0.80      0.74      0.77      1589

   micro avg       0.79      0.79      0.79      3349
   macro avg       0.79      0.78      0.78      3349
weighted avg       0.79      0.79      0.79      3349<jupyter_text>2) Pasting<jupyter_code>bg2 = BaggingClassifier(LogisticRegression(), max_samples=100,random_state=1,bootstrap=False)
bg2.fit(X_train,y_train)
y_pred = bg2.predict(X_test)
print(classification_report(y_test,y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.78      0.83      0.81      1760
           1       0.80      0.74      0.77      1589

   micro avg       0.79      0.79      0.79      3349
   macro avg       0.79      0.79      0.79      3349
weighted avg       0.79      0.79      0.79      3349<jupyter_text>Random forest<jupyter_code># BaggingClassifier(DecisionTreeClassifier(), n_estimators=10, max_samples=100,random_state=1)
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=10,max_features=10,random_state=1)
rf.fit(X_train,y_train)
y_pred = rf.predict(X_test)
print(classification_report(y_test,y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.84      0.83      0.84      1760
           1       0.82      0.83      0.82      1589

   micro avg       0.83      0.83      0.83      3349
   macro avg       0.83      0.83      0.83      3349
weighted avg       0.83      0.83      0.83      3349<jupyter_text>Stacking<jupyter_code># conda install mlxtend --channel conda-forge
from mlxtend.classifier import StackingClassifier
lr = LogisticRegression()
dt1 = DecisionTreeClassifier()
dt2 = DecisionTreeClassifier(criterion="entropy")
model_list = [lr,dt1,dt2]
meta_lr = LogisticRegression()
stack = StackingClassifier(classifiers=model_list, meta_classifier=meta_lr)
stack.fit(X_train,y_train)
y_pred = stack.predict(X_test)
print(classification_report(y_test,y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.87      0.73      0.79      1760
           1       0.75      0.88      0.81      1589

   micro avg       0.80      0.80      0.80      3349
   macro avg       0.81      0.80      0.80      3349
weighted avg       0.81      0.80      0.80      3349
{'fraction_non_alphanumeric': 0.09237973605789698, 'fraction_numerical': 0.09812686249467859, 'mean_word_length': 2.549093655589124, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '80887', 'license': 'MIT', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1722, 'n_words': 314, 'path': 'Ensembling.ipynb', 'repo_name': 'laukik1/ML', 'size': '4698'}