<jupyter_start><jupyter_text>Social network Graph Link Prediction - Facebook Challenge * Apply XGBoost on all set of features<jupyter_code>#Importing Libraries
# please do go through this python notebook: 
import warnings
warnings.filterwarnings("ignore")

import csv
import pandas as pd#pandas to create small dataframes 
import datetime #Convert to unix time
import time #Convert to unix time
# if numpy is not installed already : pip3 install numpy
import numpy as np#Do aritmetic operations on arrays
# matplotlib: used to plot graphs
import matplotlib
import matplotlib.pylab as plt
import seaborn as sns#Plots
from matplotlib import rcParams#Size of plots  
from sklearn.cluster import MiniBatchKMeans, KMeans#Clustering
import math
import pickle
import os
# to install xgboost: pip3 install xgboost
import xgboost as xgb

import warnings
import networkx as nx
import pdb
import pickle
from pandas import HDFStore,DataFrame
from pandas import read_hdf
from scipy.sparse.linalg import svds, eigs
import gc
from tqdm import tqdm
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score
#reading
from pandas import read_hdf
df_final_train = read_hdf('data/fea_sample/storage_sample_stage5.h5', 'train_df',mode='r')
df_final_test = read_hdf('data/fea_sample/storage_sample_stage5.h5', 'test_df',mode='r')
df_final_train.columns
y_train = df_final_train.indicator_link
y_test = df_final_test.indicator_link
df_final_train.drop(['source_node', 'destination_node','indicator_link'],axis=1,inplace=True)
df_final_test.drop(['source_node', 'destination_node','indicator_link'],axis=1,inplace=True)
from sklearn.metrics import f1_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint as sp_randint
from scipy.stats import uniform

param_dist = {"n_estimators":sp_randint(105,125),
              "max_depth": sp_randint(10,15),
              "min_samples_split": sp_randint(110,190),
              "min_samples_leaf": sp_randint(25,65)}

estimator1 = xgb.XGBRFClassifier(objective= 'binary:logistic',
eval_metric= 'auc',scale_pos_weight = 1,random_state=123)

Rsearch1= RandomizedSearchCV(estimator1,param_distributions=param_dist,scoring='f1',n_jobs=-1,iid=False, cv=10,verbose=20,random_state=123)

Rsearch1.fit(df_final_train,y_train)

Rsearch1.best_params_, Rsearch1.best_score_




print('mean test scores',Rsearch1.cv_results_['mean_test_score'])
print('mean train scores',Rsearch1.cv_results_['mean_train_score'])
print(Rsearch1.best_estimator_)
print(Rsearch1.best_params_)
print(Rsearch1.best_score_)
l=Rsearch1.best_params_["min_samples_leaf"]
d=Rsearch1.best_params_['max_depth']
s=Rsearch1.best_params_['min_samples_split']
n=Rsearch1.best_params_['n_estimators']

params={

 "colsample_bytree"        : [ 0.3, 0.4, 0.5 , 0.7 ,0.9],
 'subsample'               :[0.75,0.80,0.85,0.90],
 
  }
estimator2 =xgb.XGBRFClassifier(objective= 'binary:logistic',
eval_metric= 'auc',scale_pos_weight = 1,random_state=123,min_samples_leaf=l,max_depth=d,min_samples_split=s,n_estimators=n)

Rsearch2= RandomizedSearchCV(estimator2,param_distributions=params,scoring='f1',n_jobs=8,iid=False, cv=10,verbose=20,random_state=123)

Rsearch2.fit(df_final_train,y_train)

Rsearch2.best_params_, Rsearch2.best_score_

print('mean test scores',Rsearch2.cv_results_['mean_test_score'])
print('mean train scores',Rsearch2.cv_results_['mean_train_score'])
print(Rsearch2.best_estimator_)
print(Rsearch2.best_params_)
print(Rsearch2.best_score_)
col=Rsearch2.best_params_["colsample_bytree"]
sub=Rsearch2.best_params_['subsample']


params={"learning_rate"    : [0.05, 0.10, 0.15, 0.20, 0.25, 0.30,0.5 ] ,
 "gamma"                   : [ 0.01,0.0, 0.1, 0.2 , 0.3, 0.4 ],
 
  }

estimator3 = xgb.XGBRFClassifier(objective= 'binary:logistic',
eval_metric= 'auc',scale_pos_weight = 1,random_state=123,min_samples_leaf=l,max_depth=d,min_samples_split=s,n_estimators=n,colsample_bytree=col,subsample=sub)
Rsearch3= RandomizedSearchCV(estimator3,param_distributions=params,scoring='f1',n_jobs=8,iid=False, cv=10,random_state=123,verbose=20)
Rsearch3.fit(df_final_train,y_train)
Rsearch3.best_params_, Rsearch3.best_score_

print('mean test scores',Rsearch3.cv_results_['mean_test_score'])
print('mean train scores',Rsearch3.cv_results_['mean_train_score'])
print(Rsearch3.best_estimator_)
print(Rsearch3.best_params_)
print(Rsearch3.best_score_)
rate = Rsearch3.best_params_["learning_rate"]
g = Rsearch3.best_params_['gamma']


params={
 'reg_alpha'               :[0, 0.001, 0.005, 0.01, 0.05],
 'reg_lamda'               :[0,0.0001, 0.001, 0.005, 0.01, 0.05],
  }
estimator4 = xgb.XGBRFClassifier(objective= 'binary:logistic',
eval_metric= 'auc',scale_pos_weight = 1,random_state=123,min_samples_leaf=l,max_depth=d,min_samples_split=s,n_estimators=n,colsample_bytree=col,subsample=sub,learning_rate=rate,gamma=g)
Rsearch4= RandomizedSearchCV(estimator4,param_distributions=params,scoring='f1',n_jobs=8,iid=False, cv=10,random_state=123,verbose=20)
Rsearch4.fit(df_final_train,y_train)
Rsearch4.best_params_, Rsearch4.best_score_

print('mean test scores',Rsearch4.cv_results_['mean_test_score'])
print('mean train scores',Rsearch4.cv_results_['mean_train_score'])
print(Rsearch4.best_estimator_)
print(Rsearch4.best_params_)
print(Rsearch4.best_score_)
alpha = Rsearch4.best_params_["reg_alpha"]
lamda = Rsearch4.best_params_['reg_lamda']

final_clf = xgb.XGBRFClassifier(base_score=0.5, colsample_bylevel=1, colsample_bynode=0.8,
        colsample_bytree=0.7, eval_metric='auc', gamma=0.2,
        learning_rate=0.5, max_delta_step=0, max_depth=14,
        min_child_weight=1, min_samples_leaf=32, min_samples_split=162,
        missing=None, n_estimators=111, n_jobs=1, nthread=None,
        objective='binary:logistic', random_state=123, reg_alpha=0.001,
        reg_lambda=1, reg_lamda=0.0001, scale_pos_weight=1, seed=None,
        silent=None, subsample=0.9, verbosity=1)

final_clf.fit(df_final_train,y_train)


predict_y_test_final = final_clf.predict(df_final_test)
predict_y_train_final = final_clf.predict(df_final_train)

print("The train f1 score is:",f1_score(y_train,predict_y_train_final))
print("The test f1 score is:",f1_score(y_test,predict_y_test_final))
from sklearn.metrics import confusion_matrix
def plot_confusion_matrix(test_y, predict_y):
    C = confusion_matrix(test_y, predict_y)
    
    A =(((C.T)/(C.sum(axis=1))).T)
    
    B =(C/C.sum(axis=0))
    plt.figure(figsize=(20,4))
    
    labels = [0,1]
    # representing A in heatmap format
    cmap=sns.light_palette("blue")
    plt.subplot(1, 3, 1)
    sns.heatmap(C, annot=True, cmap=cmap, fmt=".3f", xticklabels=labels, yticklabels=labels)
    plt.xlabel('Predicted Class')
    plt.ylabel('Original Class')
    plt.title("Confusion matrix")
    
    plt.subplot(1, 3, 2)
    sns.heatmap(B, annot=True, cmap=cmap, fmt=".3f", xticklabels=labels, yticklabels=labels)
    plt.xlabel('Predicted Class')
    plt.ylabel('Original Class')
    plt.title("Precision matrix")
    
    plt.subplot(1, 3, 3)
    # representing B in heatmap format
    sns.heatmap(A, annot=True, cmap=cmap, fmt=".3f", xticklabels=labels, yticklabels=labels)
    plt.xlabel('Predicted Class')
    plt.ylabel('Original Class')
    plt.title("Recall matrix")
    
    plt.show()
print('Train confusion_matrix')
plot_confusion_matrix(y_train,predict_y_train_final)
print('Test confusion_matrix')
plot_confusion_matrix(y_test,predict_y_test_final)
from sklearn.metrics import roc_curve, auc

fpr,tpr,ths = roc_curve(y_test,predict_y_test_final)
auc_sc = auc(fpr, tpr)
plt.plot(fpr, tpr, color='navy',label='ROC curve (area = %0.2f)' % auc_sc)
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic with test data')
plt.legend()
plt.show()
features = df_final_train.columns
importances = final_clf.feature_importances_
indices = (np.argsort(importances))[-25:]
plt.figure(figsize=(10,12))
plt.title('Feature Importances')
plt.barh(range(len(indices)), importances[indices], color='r', align='center')
plt.yticks(range(len(indices)), [features[i] for i in indices])
plt.xlabel('Relative Importance')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12340014489253803, 'fraction_numerical': 0.03610239072687756, 'mean_word_length': 6.8215297450424925, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '291769', 'license': 'MIT', 'n_tokens_mistral': 3243, 'n_tokens_neox': 3073, 'n_words': 505, 'path': 'FB_Models_XGBoost.ipynb', 'repo_name': 'Eshan-Agarwal/Social-network-Graph-Link-Prediction---Facebook-Challenge', 'size': '8282'}