<jupyter_start><jupyter_text>DATASET 1 - BANK DATASET<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns #visualisation
import matplotlib.pyplot as plt #visualisation
%matplotlib inline 
sns.set(color_codes=True)
df = pd.read_csv("C:\\ITM SPRING 2020\\New folder\\BANK_FULL.csv")


d1=pd.get_dummies(df['default'],drop_first=False).rename(columns=lambda x:'default_'+str(x))
d2=pd.get_dummies(df['housing'],drop_first=False).rename(columns=lambda x:'housing_'+str(x))
d3=pd.get_dummies(df['loan'],drop_first=False).rename(columns=lambda x:'loan_'+str(x))
d4=pd.get_dummies(df['marital'],drop_first=False).rename(columns=lambda x:'marital_' + str(x))
d5=pd.get_dummies(df['contact'],drop_first=False).rename(columns=lambda x:'contact_' + str(x))
d6=pd.get_dummies(df['poutcome'],drop_first=False).rename(columns=lambda x:'poutcome_' + str(x))

df.drop(columns =['marital','contact','poutcome','default','housing','loan'],inplace=True)
df=pd.concat([df,d1,d2,d3,d4,d5,d6], axis=1)


job_map       = {'management':1, 'technician':2, 'entrepreneur':3, 'blue-collar':4, 'unknown':5,
                 'retired':6, 'admin.':7 ,'services':8, 'self-employed':9, 'unemployed':10, 'housemaid':11,
                 'student':12}
education_map = {'tertiary':1 ,'secondary':2,'unknown':4, 'primary':3}
month_map     = {'may':5, 'jun':6, 'jul':7 ,'aug':8 ,'oct':10 ,'nov':11, 'dec':12, 'jan':1, 'feb':2 ,'mar':3, 'apr':4 ,'sep':9}
y_map         = {"no":0,"yes":1}

df["job_"]       = df.job.map(job_map)
df["education_"] = df.education.map(education_map)
df["month_"]     = df.month.map(month_map)
df["target"]     = df.y.map(y_map)
df.drop(columns  = ["job","education","month","y"],inplace=True)
data = df.copy()

import pandas as pd
import numpy as np
import seaborn as sns
import graphviz 
import pydotplus
import io
from scipy import misc
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier,export_graphviz
#from sklearn.cross_validation import train_test_split
from matplotlib import pyplot as plt
%matplotlib inline
X_dataset = data.drop(columns='target')
y = data['target']
from sklearn import preprocessing
X = preprocessing.scale(X_dataset)
tree_g         = DecisionTreeClassifier()
array_scores_g = cross_val_score(tree,X,y,cv=5)
array_mean_g   = array_scores_g.mean()

tree_e         = DecisionTreeClassifier(criterion='entropy')
array_scores_e = cross_val_score(tree_e,X,y,cv=5)
array_mean_e   = array_scores_e.mean()


print("GINI    : Accuracy of decision tree without hyperparameter tuning: ",array_mean_g)
print(tree_g)
print()
print()
print()
print("ENTROPY :Accuracy of decision tree without hyperparameter tuning: ",array_mean_e)
print(tree_e)
print(tree)
print("Accuracy of decision tree without hyperparameter tuning: ",array_mean)
array_mean
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score

max_depth_list = [5,10,15,20,25,30]
max_depth_ =[]
accuracy_list_g = []
accuracy_list_e = []
for i in max_depth_list:
    tree_g = DecisionTreeClassifier(criterion='gini',max_depth=i)
    array_scores_g = cross_val_score(tree_g,X,y,cv=5)
    array_mean_g = array_scores_g.mean()
    accuracy_list_g.append(array_mean_g)
    
    tree_e = DecisionTreeClassifier(criterion='entropy',max_depth=i)
    array_scores_e = cross_val_score(tree_e,X,y,cv=5)
    array_mean_e = array_scores_e.mean()
    accuracy_list_e.append(array_mean_e)
    
    max_depth_.append(i)
    

plt.plot(max_depth_,accuracy_list_g,label='Gini')
plt.plot(max_depth_,accuracy_list_e,label='Entropy')
plt.xlabel('Maximum Depth')
plt.ylabel('Accuracy')  
plt.title('Accuracy vs Maximum Depth')
plt.legend()
plt.show
plt.rcParams['figure.figsize']=(8,6)


from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
max_leaf_nodes_list = [5,10,15,20,25,30]
max_leaf_nodes_ =[]
accuracy_list_g = []
accuracy_list_e = []
C_params = []
for i in max_leaf_nodes_list:
    tree_g = DecisionTreeClassifier(criterion='gini',max_leaf_nodes=i)
    array_scores_g = cross_val_score(tree_g,X,y,cv=5)
    array_mean_g = array_scores_g.mean()
    accuracy_list_g.append(array_mean_g)
    
    tree_e = DecisionTreeClassifier(criterion='entropy',max_leaf_nodes=i)
    array_scores_e = cross_val_score(tree_e,X,y,cv=5)
    array_mean_e = array_scores_e.mean()
    accuracy_list_e.append(array_mean_e)
    
    max_leaf_nodes_.append(i)
    

plt.plot(max_leaf_nodes_,accuracy_list_g,label='Gini')
plt.plot(max_leaf_nodes_,accuracy_list_e,label='Entropy')
plt.xlabel('Maximum Leaf node')
plt.ylabel('Accuracy')  
plt.title('Accuracy vs Maximum Leaf Nodes')
plt.legend()
plt.show
plt.rcParams['figure.figsize']=(8,6)


from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
min_samples_leaf_list = [5,10,15,20,25,30]
min_samples_leaf_ =[]
accuracy_list_g = []
accuracy_list_e = []

for i in max_leaf_nodes_list:
    tree_g = DecisionTreeClassifier(criterion='gini',min_samples_leaf=i)
    array_scores_g = cross_val_score(tree_g,X,y,cv=5)
    array_mean_g = array_scores_g.mean()
    accuracy_list_g.append(array_mean_g)
    
    tree_e = DecisionTreeClassifier(criterion='entropy',min_samples_leaf=i)
    array_scores_e = cross_val_score(tree_e,X,y,cv=5)
    array_mean_e = array_scores_e.mean()
    accuracy_list_e.append(array_mean_e)
    
    min_samples_leaf_.append(i)
    

plt.plot(min_samples_leaf_,accuracy_list_g,label='Gini')
plt.plot(min_samples_leaf_,accuracy_list_e,label='Entropy')
plt.xlabel('Minimum Sample Leaf ')
plt.ylabel('Accuracy')  
plt.title('Accuracy vs Minimum Sample Leaf')
plt.legend()
plt.show
plt.rcParams['figure.figsize']=(8,6)<jupyter_output><empty_output><jupyter_text>ADAPTIVE BOOSTING<jupyter_code>accuracy_list = []

learning_rates =[.001,.01,.1,1]
for i in learning_rates:
    model = DecisionTreeClassifier(criterion='entropy',max_depth =15,min_samples_leaf=15,max_leaf_nodes=15)
    Adaboost = AdaBoostClassifier(base_estimator=model,n_estimators=10,learning_rate=i)
    #boostmodel = Adaboost.fit(X_train,y_train)
    array_scores = cross_val_score(Adaboost,X_train,y_train,cv=5)
    array_mean = array_scores.mean()
    print("for learning rate= ",i," Accuracy is : ",array_mean)
    accuracy_list.append(array_mean)
    

plt.plot(np.log10(learning_rates),accuracy_list)
#plt.plot(min_samples_leaf_,accuracy_list_e,label='Entropy')
plt.xlabel('Learning Rates ')
plt.ylabel('Accuracy')  
plt.title('Accuracy vs Learning Rates')
plt.legend()
plt.show
plt.rcParams['figure.figsize']=(8,6)
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
min_samples_leaf_list = [5,10,15,20,25,30]
min_samples_leaf_ =[]
accuracy_list_g = []
accuracy_list_e = []

for i in min_samples_leaf_list:
    model_g = DecisionTreeClassifier(criterion='gini',min_samples_leaf =i)
    Adaboost_g = AdaBoostClassifier(base_estimator=model_g,n_estimators=100,learning_rate=1)
    array_scores_g = cross_val_score(Adaboost_g,X,y,cv=5)
    array_mean_g = array_scores_g.mean()
    print('GINI: for minimum sample = ',i,' mean accuracy is ',array_mean_g)
    accuracy_list_g.append(array_mean_g)
    
    model_e = DecisionTreeClassifier(criterion='entropy',min_samples_leaf =i)
    Adaboost_e = AdaBoostClassifier(base_estimator=model_e,n_estimators=100,learning_rate=1)
    array_scores_e = cross_val_score(Adaboost_e,X,y,cv=5)
    array_mean_e = array_scores_e.mean()
    print('ENTROPY: for minimum sample = ',i,' mean accuracy is ',array_mean_e)
    accuracy_list_e.append(array_mean_e)

    min_samples_leaf_.append(i)
    

plt.plot(min_samples_leaf_,accuracy_list_g,label='Gini')
plt.plot(min_samples_leaf_,accuracy_list_e,label='Entropy')
plt.xlabel('Minimum Sample Leaf ')
plt.ylabel('Accuracy')  
plt.title('Accuracy vs Minimum Sample Leaf')
plt.legend()
plt.show
plt.rcParams['figure.figsize']=(8,6)



from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
max_leaf_nodes_list = [5,10,15,20,25,30]
max_leaf_nodes_ =[]
accuracy_list_g = []
accuracy_list_e = []

for i in max_leaf_nodes_list:
    model_g = DecisionTreeClassifier(criterion='gini',max_leaf_nodes=i)
    Adaboost_g = AdaBoostClassifier(base_estimator=model_g,n_estimators=100,learning_rate=1)
    array_scores_g = cross_val_score(Adaboost_g,X,y,cv=5)
    array_mean_g = array_scores_g.mean()
    print('GINI: for maximum leaf node = ',i,' mean accuracy is ',array_mean_g)
    accuracy_list_g.append(array_mean_g)
    
    model_e = DecisionTreeClassifier(criterion='entropy',min_samples_leaf =i)
    Adaboost_e = AdaBoostClassifier(base_estimator=model_e,n_estimators=100,learning_rate=1)
    array_scores_e = cross_val_score(Adaboost_e,X,y,cv=5)
    array_mean_e = array_scores_e.mean()
    print('ENTROPY: for maximum leaf nodes = ',i,' mean accuracy is ',array_mean_e)
    accuracy_list_e.append(array_mean_e)

    max_leaf_nodes_.append(i)
    

plt.plot(max_leaf_nodes_,accuracy_list_g,label='Gini')
plt.plot(max_leaf_nodes_,accuracy_list_e,label='Entropy')
plt.xlabel('Maximum Leaf Nodes ')
plt.ylabel('Accuracy')  
plt.title('Accuracy vs Maximum Lead Nodes')
plt.legend()
plt.show
plt.rcParams['figure.figsize']=(8,6)



from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score

max_depth_list = [5,10,15,20,25,30]
max_depth_ =[]
accuracy_list_g = []
accuracy_list_e = []



for i in max_leaf_nodes_list:
    model_g = DecisionTreeClassifier(criterion='gini',max_depth=i)
    Adaboost_g = AdaBoostClassifier(base_estimator=model_g,n_estimators=100,learning_rate=1)
    array_scores_g = cross_val_score(Adaboost_g,X,y,cv=5)
    array_mean_g = array_scores_g.mean()
    print('GINI: for maximum depth = ',i,' mean accuracy is ',array_mean_g)
    accuracy_list_g.append(array_mean_g)
    
    model_e = DecisionTreeClassifier(criterion='entropy',max_depth =i)
    Adaboost_e = AdaBoostClassifier(base_estimator=model_e,n_estimators=100,learning_rate=1)
    array_scores_e = cross_val_score(Adaboost_e,X,y,cv=5)
    array_mean_e = array_scores_e.mean()
    print('ENTROPY: for maximum depth = ',i,' mean accuracy is ',array_mean_e)
    accuracy_list_e.append(array_mean_e)

    max_depth_.append(i)
    

plt.plot(max_leaf_nodes_,accuracy_list_g,label='Gini')
plt.plot(max_leaf_nodes_,accuracy_list_e,label='Entropy')
plt.xlabel('Maximum Depth ')
plt.ylabel('Accuracy')  
plt.title('Accuracy vs Maximum Depth')
plt.legend()
plt.show
plt.rcParams['figure.figsize']=(8,6)



from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import GridSearchCV

def dtree_grid_search(X,y,nfolds):
    #create a dictionary of all values we want to test
    param_grid = { 'criterion':['gini','entropy'],
                  'max_depth': np.arange(5, 15),
                  'max_leaf_nodes':np.arange(15,30),
                  'min_samples_leaf':np.arange(15,30)
                  }
    

    # decision tree model
    dtree_model=DecisionTreeClassifier()
    #use gridsearch to test all values
    dtree_gscv = GridSearchCV(dtree_model, param_grid, cv=nfolds)
    #fit model to data
    dtree_gscv.fit(X, y)
    return dtree_gscv.best_params_
dtree_grid_search(X,y,3)
import pandas as pd
import numpy as np
import seaborn as sns
import graphviz 
import pydotplus
import io
from scipy import misc
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier,export_graphviz
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
%matplotlib inline
features = ['poutcome_other', 'poutcome_success', 'poutcome_unknown','contact_telephone', 
           'contact_unknown', 'poutcome_failure','marital_married', 'marital_single', 
           'contact_cellular','loan_yes', 'job_', 'education_','marital_divorced', 'default_no', 
           'default_yes', 'housing_no', 'housing_yes','month_', 'loan_no','age', 'balance', 
           'day', 'duration', 'campaign', 'pdays', 'previous']
c=DecisionTreeClassifier(criterion='gini',max_depth= 5,max_leaf_nodes= 26,min_samples_leaf=24)
array_scores = cross_val_score(c,X,y,cv=5)
Accuracy = array_scores.mean()
print(Accuracy)
d_t = c.fit(X,y)
def show_tree(tree, features, path):
    f = io.StringIO()
    export_graphviz(tree, out_file=f, feature_names=features)
    pydotplus.graph_from_dot_data(f.getvalue()).write_png(path)
    img = misc.imread(path)
    plt.rcParams["figure.figsize"]=(20,20)
    plt.imshow(img)
    
show_tree(d_t,features,'DT_Dataset1.png')<jupyter_output>C:\Users\Kamini\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imread`` instead.
  """<jupyter_text>ADAPTIVE BOOSTING - GRIDSEARCH<jupyter_code>from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import GridSearchCV

def dtree_grid_search(X,y,nfolds):
    #create a dictionary of all values we want to test
    param_grid = { 'criterion':['gini','entropy'],
                  'max_depth': np.arange(5, 15),
                  'max_leaf_nodes':np.arange(15,30),
                  'min_samples_leaf':np.arange(15,30)
                  }
    

    # decision tree model
    dtree_model=DecisionTreeClassifier()
    Adaboost_e = AdaBoostClassifier(base_estimator=dtree_model,n_estimators=100,learning_rate=1)
    #use gridsearch to test all values
    dtree_gscv = GridSearchCV(dtree_model, param_grid, cv=4)
    #fit model to data
    dtree_gscv.fit(X, y)
    return dtree_gscv.best_params_
dtree_grid_search(X,y,3)
c=DecisionTreeClassifier(criterion='gini',max_depth=5 ,max_leaf_nodes=22 ,min_samples_leaf=24)
Adaboost = AdaBoostClassifier(base_estimator=c,n_estimators=100,learning_rate=1)
array_scores = cross_val_score(Adaboost,X,y,cv=3)
Accuracy = array_scores.mean()
print(Accuracy)<jupyter_output>0.5698577804496893
{'fraction_non_alphanumeric': 0.12578389677791393, 'fraction_numerical': 0.019101852519282057, 'mean_word_length': 6.031931069437405, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '307502', 'license': 'Apache-2.0', 'n_tokens_mistral': 5520, 'n_tokens_neox': 5194, 'n_words': 793, 'path': 'Assignment 2 - Decision Tree - Dataset 1 - Bank Data set.ipynb', 'repo_name': 'Kaminibokefode/SVM--DecisionTree--Adaboost', 'size': '13873'}