# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
import matplotlib.pyplot as plt # --> for data visulization
import seaborn as sns # --> for data visulization
from dateutil import parser # --> convert time in date time datatype

df = pd.read_csv('../input/app-data/appdata10.csv')
df.head()
df.tail()
df.shape
for i in range(0,6):
    print(df.loc[i,'screen_list'],'\n') 
df.isnull().sum()
df.describe()
df.info()
features = df.columns
for i in features:
    print('unique value of {} \n {}  \n len is {} '.format(i, df[i].unique(), len(df[i].unique())))
df['hour'] = df.hour.str.slice(1,3).astype(int)
df.head()
df.dtypes
df1 = df.drop(['first_open', 'screen_list','user','enrolled_date'], axis = 1)
df1.head()
"""
# Heatmap using Correlation metrix
"""
plt.figure(figsize=(14,8))
sns.heatmap(df1.corr(),annot=True)
plt.title('DF1 Heatmap correlatin matrix', fontsize=15)
"""
# pair plot 
"""
sns.pairplot(df1, hue='enrolled') 
"""
# countplot of enrolled
"""
sns.countplot(df1.enrolled)
not_enrolled_user = (df1.enrolled<1).sum()
enrolled_user = (50000-not_enrolled_user).sum()
not_enrolled_user
enrolled_user
"""
# histogram of each feature of df1
"""
plt.figure(figsize=(20,10))
features = df1.columns
for i, j in enumerate(features):
    plt.subplot(3,3,i+1)
    plt.title(f'Histogram of {j}', fontsize= 15)
    bins = len(df1[j].unique())
    plt.hist(df1[j], bins=bins,rwidth=0.8,linewidth=2, edgecolor= 'y')
plt.subplots_adjust(hspace=0.5)
    
#   1) In first histogram easily find that wednesday (2) and thursday (3) less user enrolled 
#   2) In hour histogram near 8 to 12 less user enrolled 
#   3) Iin age histogram 20 to 40 aged user enrolled most 
#   4) In histogram of numscreen above 40 screen user is to less 
#   5) remaining all histogram is in 0 and 1
for i,j in enumerate(features):
    print(i,j)
"""
# correlation bar plot with 'enrolled ' features
"""
sns.set()
plt.figure(figsize=(15,7))
plt.title('Correlation Bar Plot of Enrolled Features', fontsize= 20)
df2 = df1.drop(['enrolled'], axis= 1)
axis = sns.barplot(df2.columns,df2.corrwith(df1.enrolled))
axis.tick_params(labelsize=15, labelrotation = 20)
df.dtypes
df.head()
screen_data = pd.read_csv('../input/app-data/top_screens.csv').top_screens.values
screen_data
type(screen_data)
df['screen_list'] = df.screen_list.astype(str)+','
df.head()
for screen_name in screen_data:
    df[screen_name] = df.screen_list.str.contains(screen_name).astype(int)
    df['screen_list'] = df.screen_list.str.replace(screen_name+',',"")
df.shape
df.head()
df.loc[0,'screen_list']
df['remain_screen_list'] = df.screen_list.str.count(",")
# droped both time columns
df.shape
df = df.drop(['first_open','enrolled_date','screen_list'], axis=1)
df.columns

df.head()
# sum of all saving screen at one place
saving_screen=[
    'Saving9',
    'Saving1',
    'Saving8',
    'Saving10',
    'Saving4',
    'Saving7',
    'Saving2',
    'Saving6',
    'Saving5',
    'Saving2Amount'
]
df['saving_screen_count']= df[saving_screen].sum(axis=1)
df = df.drop(columns= saving_screen)
df.shape
# sum of all loan column at one place
loan = [
    'Loan2',
    'Loan',
    'Loan4',
    'Loan3'
]
df['loan_col']= df[loan].sum(axis=1)

df = df.drop(columns= loan)
# credit column sum
credit = [
    'Credit3Container',
    'Credit3',
    'Credit2',
    'Credit3Dashboard',
    'Credit1'
]
df['credit_col']= df[credit].sum(axis=1)
df = df.drop(columns=credit)
# for cc 
cc = [
    'CC1',
    'CC1Category',
    'CC3',
]
df['cc_col'] = df[cc].sum(axis=1)
df =df.drop(columns=cc)
df.shape
df.describe()
df.head() # our pure cleaned data
# plt.figure(figsize=(20,10))
# sns.heatmap(df.corr(),annot=True, linewidth=2) # --> its take a to much time for run
x= df.drop(columns = 'enrolled')
y = df['enrolled']  # our target 
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.2, random_state=10)
x_train.shape
x_test.shape
y_train.shape
"""
The multiple features in the different units so for the best accuracy need to convert all features in a single unit.


"""
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train_sc = sc.fit_transform(x_train)
x_test_sc = sc.transform(x_test)
"""
# Model Building With Ml algorithm
"""
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
"""
# Decision Tree Classifier
"""
from sklearn.tree import DecisionTreeClassifier
model_DTC = DecisionTreeClassifier(criterion='entropy', random_state=10)  # for gini entropy score is 0.7081
model_DTC.fit(x_train,y_train)
y_predict_DTC = model_DTC.predict(x_test)
accuracy_score(y_test, y_predict_DTC) 
# standerd scaling data to build model
model_DTC_SC =  DecisionTreeClassifier(criterion='entropy', random_state=10) # for gini 0.5502
model_DTC_SC.fit(x_train_sc, y_train)
y_predict_DTC_SC = model_DTC_SC.predict(x_test_sc)
accuracy_score(y_test,y_predict_DTC_SC)
"""
#  K -  Nearest Neighbor 
"""
from sklearn.neighbors import KNeighborsClassifier
model_KN = KNeighborsClassifier(n_neighbors=5, p=2, metric='minkowski')
model_KN.fit(x_train,y_train)
y_predicted_KN = model_KN.predict(x_test)
accuracy_score(y_test,y_predicted_KN)
# standerd scaling data to build model
model_KN_SC  = KNeighborsClassifier(n_neighbors=5,p=2, metric='minkowski')
model_KN_SC.fit(x_train_sc,y_train)
y_predicted_KN_SC = model_KN_SC.predict(x_test_sc)
accuracy_score(y_test,y_predicted_KN_SC)
"""
#  Naive Bayes
"""
from sklearn.naive_bayes import GaussianNB
mod_G = GaussianNB()
mod_G.fit(x_train,y_train)
y_predicted_G = mod_G.predict(x_test)
accuracy_score(y_test,y_predicted_G)
# standerd scaling data to build model
mod_G_SC = GaussianNB()
mod_G_SC.fit(x_train_sc,y_train)
y_predicted_G_SC = mod_G_SC.predict(x_test_sc)
accuracy_score(y_test,y_predicted_G_SC)
"""
# Random Forest 
"""
from sklearn.ensemble import RandomForestClassifier
model_RFC = RandomForestClassifier(n_estimators=10, criterion='gini')
model_RFC.fit(x_train,y_train)
y_predicted_RFC = model_RFC.predict(x_test)
accuracy_score(y_test,y_predicted_RFC)
# standerd scaling data to build model
model_RFC_SC = RandomForestClassifier(n_estimators=10, criterion='gini')
model_RFC_SC.fit(x_train_sc,y_train)
y_predicted_RFC_SC = model_RFC_SC.predict(x_test_sc)
accuracy_score(y_test,y_predicted_RFC_SC)
"""
#  Logistic Regreesion
"""
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(penalty='l2', C = 1, random_state=10)
model.fit(x_train,y_train)
y_predicted_l = model.predict(x_test)
# accuracy_score(y_test,y_predicted_l)
model.score(x_test,y_test)
# standerd scaling data to build model
model_sc = LogisticRegression(penalty='l2', C = 1, random_state=10)
model_sc.fit(x_train_sc, y_train)
y_predicted_ls = model_sc.predict(x_test_sc)
accuracy_score(y_test,y_predicted_ls)
"""
#  Support vactor Machine
"""
from sklearn.svm import SVC
model_svc = SVC()
model_svc.fit(x_train,y_train)
model_svc.predict(x_test)
model.score(x_test,y_test)

# standerd scaling data to build model
model_svc_sc= SVC()
model_svc_sc.fit(x_train_sc,y_train)
y_predicted_svc_sc = model_svc_sc.predict(x_test_sc)
accuracy_score(y_test,y_predicted_svc_sc)
"""
# XGBoost 
"""
from xgboost import XGBClassifier
xgb_model = XGBClassifier(eval_metric='mlogloss', use_label_encoder=False)
 # eval_metric='mlogloss' parameter to not change behavior and not show a warning
xgb_model.fit(x_train, y_train)
y_pred_xgb = xgb_model.predict(x_test)
accuracy_score(y_test, y_pred_xgb)
# train with Standert Scaling dataset
xgb_model_sc = XGBClassifier(eval_metric='mlogloss', use_label_encoder=False)
#  use_label_encoder=False to handle warning
xgb_model_sc.fit(x_train_sc, y_train)
y_pred_xgb_sc = xgb_model_sc.predict(x_test_sc)
accuracy_score(y_test, y_pred_xgb_sc)
data = { 'model_name' : ['Desicion Tree', 'K_Nearest', 'naive Bayes', 'Random forest','Logistic Regression', 'SVM', 'XGBoost'],
        'Training Type' : ['Reguler','SC','SC','SC','SC','SC','SC'],
        'score' : ['0.7106','0.7368','0.6967','0.7615','0.7522','0.7743','0.7814']
}
best_model = pd.DataFrame(data)
best_model
# we use xgboost and svm give the best score than other ML algorithm
# but we are going with xgboost because of xgboost is much more better then svm
"""
Confusion Matrix
"""
plt.figure(figsize=(7,4))
cm_xgb = confusion_matrix(y_test,y_pred_xgb_sc)
sns.heatmap(cm_xgb, annot=True, fmt='g')
plt.title('XGBoost model Confusion Matrix')
"""
#  Classification report
"""
cr_xgb = classification_report(y_test,y_pred_xgb_sc)
print('Classification Report --------> \n', cr_xgb)
"""
# Cross Validation
"""
from sklearn.model_selection import cross_val_score
model = XGBClassifier(eval_metric='mlogloss',use_label_encoder=False)
cross_validation = cross_val_score(model, x_train_sc, y_train, cv=5)
cross_validation
cross_validation.mean()
"""
**The mean value cross-validation and XGBoost model accuracy is 78%. That means our XGBoost model is a generalized model.**
"""
# save the model using pickle
import pickle
with open('app_data_pickle','wb') as file:
    pickle.dump(xgb_model_sc,file)
# save the model using joblib
import joblib
joblib.dump(xgb_model_sc,'app_data_model_joblib')