<jupyter_start><jupyter_text>Processing for All dataFeature Extraction using PCATraining Decision Tree ClassifierTraining Vanilla NN<jupyter_code>%cd /content/drive/MyDrive/
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import collections
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import  cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from xgboost import XGBClassifier
from sklearn.tree import DecisionTreeClassifier


%matplotlib inline
import os
df=pd.read_csv('pca_data.csv')
df.head()
df.describe()
df['0']
fig,ax=plt.subplots(nrows=3,ncols=5)
fig.set_figheight(5)
fig.set_figwidth(13)


for i in range(5):
  one = 3*i + 0;
  one1 = 3*i + 1;
  one2 = 3*i + 2;
  sns.distplot(df[str(one)],ax=ax[0, i])
  sns.distplot(df[str(one1)],ax=ax[1, i])
  sns.distplot(df[str(one2)],ax=ax[2, i])
# Run this at night for an hour top get the plots

# print("                                              add/non-ad  vs   Features")
# fig,ax=plt.subplots(nrows=1,ncols=1)
# fig.set_figheight(15)
# fig.set_figwidth(18)
# # sns.stripplot(y=1558,x=0,data=df,ax=ax[0])
# # sns.stripplot(y=1558,x=1,data=df,ax=ax[1])
# # sns.stripplot(y=1558,x=2,data=df,ax=ax[2])

# for i in range(1):
#   sns.stripplot(y = str(15), x = str(i), data = df, ax=ax[i])

# plt.show()
# plt.figure(figsize=(15,10))
# sns.boxplot(x='15',y='1',data=df)
# plt.xlabel('label-add/non-ad')
# plt.ylabel('width')
# plt.title("Boxplot-  Target Vs Width ")
# plt.show()
# columns_for_visualization = list()
# for i in range(15):
#   columns_for_visualization.append(str(i))
# corr=df[columns_for_visualization].corr()
# fig, ax = plt.subplots(figsize=(15,15))
# sns.heatmap(corr,annot=True,linewidths=.5, ax=ax)
# ax = sns.violinplot(x="15", y="1",data=df,split=True,inner="quart")
fig,ax=plt.subplots(nrows=3,ncols=5)
fig.set_figheight(30)
fig.set_figwidth(50)


for i in range(5):
 one = 3*i + 0;
 one1 = 3*i + 1;
 one2 = 3*i + 2;
 sns.violinplot(x="15", y=str(one),data=df,split=True,inner="quart",ax=ax[0,i])
 sns.violinplot(x="15", y=str(one1),data=df,split=True,inner="quart",ax=ax[1,i])
 sns.violinplot(x="15", y=str(one2),data=df,split=True,inner="quart",ax=ax[2,i])
da=df.values
X=da[:,:-1]
y=da[:,-1]


xtrain,xtest,ytrain,ytest=train_test_split(X,y,test_size=0.30,random_state=80)

def fit_models(classifiers,xtrain,ytrain):
    """This function fit multiple models by sklearn and return the dictionary with values as  objects of models"""
    models=collections.OrderedDict()
    for constructor in classifiers:
        obj=constructor()
        obj.fit(xtrain,ytrain)
        models[str(constructor).split(':')[0]]=obj
    return models

def classification_multi_report(ytest,models_array):
    """This function generate classification accuracy report for given input model objects"""
    for i in models_array:
        print('__________________________________________________')
        print('the model - '+str(i))
        print(classification_report(ytest,models_array[i].predict(xtest)))
def cross_Function(models,cv):
    """This function return cross validated accuray and the variance of given input model obejects"""
    accuracy={}
    for model in models:
        cross_val_array=cross_val_score(models[model],xtrain,ytrain,scoring='accuracy',cv=cv)
        accuracy[model]=[np.mean(cross_val_array),np.std(cross_val_array)]
    return accuracy

def multi_grid_search(param_grid_array,estimator_list,x,y):
    """This function calculate the grid search parameters and accuracy  for given input modles and return dictionary with each tupple containing accuracy and best parameters"""
    d={}
    count=0
    for i in estimator_list:
        gc=GridSearchCV(estimator=estimator_list[i],param_grid=param_grid_array[count],scoring ='accuracy',cv=5).fit(x,y)
        d[i]=(gc.best_params_,gc.best_score_)
        count+=1
    return d
classifiers=[SVC, KNeighborsClassifier, RandomForestClassifier, XGBClassifier, DecisionTreeClassifier]

# classifiers=[GaussianNB,SVC,KNeighborsClassifier]

model_list=fit_models(classifiers,xtrain,ytrain)

classification_multi_report(ytest,model_list)

obj=cross_Function(model_list,cv=20)
for model in obj:
    print('the model -'+str(model)+'has \n || crosss validated accuracy as  -> '+str(obj[model][0])+' | variance - '+str(obj[model][1])+' ||' )
    print('______________________________________________________________________________________________________________')
param_grid_svm=[
  {
      
  }
]

param_grid_knn=[

    {   

        
    }
]

param_RandomForest = [
    {
        'n_estimators': np.arange(100, 500, 50),
        'max_depth' : np.arange(5, 35, 5)
    }
]

param_XGB=[
    {
        
    }
]

param_logit = [
  {
      
  }
]

param_XGBoost=[
    {

    }
]
param_dc = [
  {
      
  }
]
param_grid_array=[param_grid_svm, param_grid_knn, param_RandomForest, param_XGBoost, param_dc]
multi_grid_search(param_grid_array,model_list,xtrain,ytrain)<jupyter_output><empty_output><jupyter_text>**Fitting model with best hyperparmater and cv score**<jupyter_code>classifier=RandomForestClassifier(max_depth = 20, n_estimators = 300)
classifier.fit(xtrain,ytrain)<jupyter_output><empty_output><jupyter_text>Confusion Matrix<jupyter_code>sns.heatmap(pd.crosstab(ytest,classifier.predict(xtest)),cmap='coolwarm')
plt.xlabel('predicted')
plt.ylabel('actual')
plt.show()
print(classification_report(ytest,classifier.predict(xtest)))<jupyter_output>precision    recall  f1-score   support

         0.0       0.97      0.97      0.97       409
         1.0       0.98      0.98      0.98       824

    accuracy                           0.98      1233
   macro avg       0.98      0.97      0.98      1233
weighted avg       0.98      0.98      0.98      1233<jupyter_text>**NN **<jupyter_code>#custom NN
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop,Adam
X_train1, X_test, y_train1, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

X_train, X_val, y_train, y_val = train_test_split(X_train1, y_train1, test_size=0.125, random_state=1)
#model params
model=Sequential()
model.add(Dense(30, input_dim=X_train.shape[1], activation='relu', kernel_initializer='he_normal'))
# model.add(Dense(200,activation='sigmoid',input_shape=(15,)))
# model.add(Dropout(0.2))
# model.add(Dense(300,activation='sigmoid'))
# model.add(Dropout(0.2))
# model.add(Dense(200,activation='sigmoid'))
# model.add(Dense(100,activation='sigmoid'))
model.add(Dense(30,activation='sigmoid'))
model.add(Dense(25,activation='sigmoid'))

# model.add(Dropout(0.2))
# model.add(Dense(20,activation='sigmoid'))
model.add(Dense(20,activation='sigmoid'))
# model.add(Dropout(0.2))
# model.add(Dense(25, activation='relu'))
# model.add(Dropout(0.2))
# model.add(Dense(10,activation='sigmoid'))

model.add(Dense(1, activation='sigmoid'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer=Adam(), metrics=['accuracy','AUC'])

# class_weights={0: 1,
#                 1:2.5,
#                 2: 5}
history = model.fit(X_train1, y_train1, batch_size=64, epochs=500, verbose=1, validation_data=(X_val, y_val))
score = model.evaluate(X_test,y_test, verbose=1)
print(score)
print('Test accuracy:', score[1])
model.save("ver1.9")
from keras.models import load_model
prevModel = load_model('ver1.8')
score = prevModel.evaluate(X_train,y_train, verbose=1)
print(score)
print('Test accuracy:', score[1])
prevModel.summary()
!ls<jupyter_output>2018MT60795_it.pdf		   Synthetic_data.csv
'Colab Notebooks'		  'Untitled document.gdoc'
'Getting started.pdf'		  'Untitled presentation.gslides'
'iaw-edau-jme – 9 Feb 2021.gjam'   ver1.2
'ift-djtf-qko – 1 Mar 2021.gjam'   ver1.3
 pca_data.csv			   ver1.4
{'fraction_non_alphanumeric': 0.13961711986343128, 'fraction_numerical': 0.0348737958785514, 'mean_word_length': 5.066568047337278, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '260579', 'license': 'MIT', 'n_tokens_mistral': 3215, 'n_tokens_neox': 3015, 'n_words': 524, 'path': 'Code/GeometryUsingPCAdata.ipynb', 'repo_name': 'sebstian0627/Internet-Advertisement-Detection', 'size': '8201'}