# 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 numpy as np
import pandas as pd 
import matplotlib.pyplot as plt 
%matplotlib inline 
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')

"""
# Load the data
"""
train_data = pd.read_csv('../input/loan-prediction-analytics-vidhya/train_ctrUa4K.csv')
test_data = pd.read_csv('../input/loan-prediction-analytics-vidhya/test_lAUu6dG.csv')
train = train_data.copy()
test = test_data.copy()
train.head()
test.head()
"""
# Missing values 
"""
train.isna().sum()
test.isna().sum()
train.info()
train.drop(columns='Loan_ID',inplace =True)
test.drop(columns='Loan_ID',inplace =True)
#train.drop('Loan_Status',axis=1,inplace=True)
train['Credit_History']=train['Credit_History'].astype(str)
test['Credit_History']=test['Credit_History'].astype(str)

cat_cols=[]
num_cols=[]

for col in train.columns:
    if train[col].dtypes=='object':
        cat_cols.append(col)
        
for col in train.columns:
    if train[col].dtypes!='object':
        num_cols.append(col)
        
print(cat_cols)
print(num_cols)
i=1
plt.figure(figsize=(15,20))
for col in cat_cols:
    plt.subplot(4,2,i)
    sns.countplot(train[col])
    i=i+1
plt.show()
train['Loan_Status'].value_counts(normalize=True)
i=1
plt.figure(figsize=(15,20))
for col in num_cols:
    plt.subplot(2,2,i)
    sns.distplot(train[col])
    i=i+1
    
plt.show()
   
i=1
plt.figure(figsize=(20,18))
for col in num_cols:
    plt.subplot(4,2,i)
    sns.boxplot(train[col])
    i=i+1
    
plt.show()
   
def remove_outlier(col):
    sorted(col)
    Q1,Q3=np.percentile(col,[25,75])
    IQR=Q3-Q1
    lower_range= Q1-(1.5 * IQR)
    upper_range= Q3+(1.5 * IQR)
    return lower_range, upper_range
lr,ur=remove_outlier(train['ApplicantIncome'])
print('Lower Range :',lr,'\nUpper Range :',ur)
train['ApplicantIncome']=np.where(train['ApplicantIncome']>ur,ur,train['ApplicantIncome'])
train['ApplicantIncome']=np.where(train['ApplicantIncome']<lr,lr,train['ApplicantIncome'])
lr,ur=remove_outlier(train['LoanAmount'])
print('Lower Range :',lr,'\nUpper Range :',ur)
train['LoanAmount']=np.where(train['LoanAmount']>ur,ur,train['LoanAmount'])
train['LoanAmount']=np.where(train['LoanAmount']<lr,lr,train['LoanAmount'])
lr,ur=remove_outlier(train['CoapplicantIncome'])
print('Lower Range :',lr,'\nUpper Range :',ur)
train['CoapplicantIncome']=np.where(train['CoapplicantIncome']>ur,ur,train['CoapplicantIncome'])
train['CoapplicantIncome']=np.where(train['CoapplicantIncome']<lr,lr,train['CoapplicantIncome'])
lr,ur=remove_outlier(train['Loan_Amount_Term'])
print('Lower Range :',lr,'\nUpper Range :',ur)
train['Loan_Amount_Term']=np.where(train['Loan_Amount_Term']>ur,ur,train['Loan_Amount_Term'])
train['Loan_Amount_Term']=np.where(train['Loan_Amount_Term']<lr,lr,train['Loan_Amount_Term'])
train['Married'].fillna(train['Married'].mode()[0], inplace = True)
test['Married'].fillna(test['Married'].mode()[0], inplace = True)
train["Gender"].fillna(train["Gender"].mode()[0], inplace = True)
test["Gender"].fillna(test["Gender"].mode()[0], inplace = True)
train['Dependents'].fillna(train['Dependents'].mode()[0], inplace = True) 
test['Dependents'].fillna(test['Dependents'].mode()[0], inplace = True)
train['Self_Employed'].fillna(train['Self_Employed'].mode()[0], inplace = True) 
test['Self_Employed'].fillna(test['Self_Employed'].mode()[0], inplace = True)
train['Credit_History'].fillna(train['Credit_History'].mode()[0], inplace = True) 
test['Credit_History'].fillna(test['Credit_History'].mode()[0], inplace = True)
train["LoanAmount"].fillna(train["LoanAmount"].median(), inplace = True) 
test["LoanAmount"].fillna(test["LoanAmount"].median(), inplace = True)
train["Loan_Amount_Term"].fillna(train["Loan_Amount_Term"].median(), inplace = True) 
test["Loan_Amount_Term"].fillna(test["Loan_Amount_Term"].median(), inplace = True)
# Checking for missing values after preprocessing
train.isna().sum()
test.isna().sum()
i=1
plt.figure(figsize=(20,18))
for col in num_cols:
    plt.subplot(4,2,i)
    sns.boxplot(train[col])
    i=i+1
    
plt.show()
# Encoding Categorical Columns
from sklearn.preprocessing import LabelEncoder

le= LabelEncoder()

for col in cat_cols:
    train[col]= le.fit_transform(train[col])
train.head()
#Scaling Numerical Columns
from sklearn.preprocessing import StandardScaler

ss= StandardScaler()

train[num_cols]= ss.fit_transform(train[num_cols].values)
y= train['Loan_Status']
X= train.drop('Loan_Status', axis=1)
from sklearn.model_selection import train_test_split,GridSearchCV
X_train, X_test, y_train,y_test= train_test_split(X,y,test_size= 0.3, stratify=y, random_state=42)
# Building our Model
from sklearn import svm
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
import xgboost as xgb
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import GradientBoostingClassifier
from xgboost import XGBClassifier
from sklearn import metrics
from sklearn.metrics import accuracy_score
from sklearn.model_selection import KFold, cross_val_score
from sklearn.metrics import roc_auc_score,roc_curve,classification_report,confusion_matrix,plot_confusion_matrix
value= [LogisticRegression(), RandomForestClassifier(), AdaBoostClassifier(), KNeighborsClassifier(), XGBClassifier(),GradientBoostingClassifier()]

key= ['LogisticRegression', 'RandomForsetClassifier', 'AdaBoostClassifier',  'KNeighborsClassifier', 'XGBClassifier','GradientBoostingClassifier']

models= dict(zip(key,value))
accuracy_scores=[]
for key,value in models.items():
    value.fit(X_train,y_train)
    y_pred= value.predict(X_test)
    accuracy= accuracy_score(y_test, y_pred)
    accuracy_scores.append(accuracy)
    print(key)
    print(accuracy)
test.head()
test=test.fillna(test.median())
cat_cols1=[]
num_cols1=[]

for col in test.columns:
    if test[col].dtypes=='object':
        cat_cols1.append(col)
        
for col in test.columns:
    if test[col].dtypes!='object':
        num_cols1.append(col)
        
print(cat_cols1)
print(num_cols1)
from sklearn.preprocessing import LabelEncoder

le= LabelEncoder()

for col in cat_cols1:
    test[col]= le.fit_transform(test[col])
from sklearn.preprocessing import StandardScaler

ss= StandardScaler()

test[num_cols1]= ss.fit_transform(test[num_cols1].values)
test.head()
from sklearn.linear_model import LogisticRegression
lr=LogisticRegression()
lr.fit(X_train, y_train)
y_pred=lr.predict(test)
sample_submission=pd.read_csv(r'../input/loan-prediction-analytics-vidhya/sample_submission_49d68Cx.csv')
sample_submission['Loan_Status']=y_pred
status={1: 'Y', 0: 'N'}
sample_submission['Loan_Status']=sample_submission['Loan_Status'].map(status)
sample_submission.head()
sample_submission.to_csv(r'C:\DATA file\Hackathon data.csv')
"""
# #The Logistic Regression algorithm is the most accurate: approximately 84.8%
"""
"""
Please share your comment and UPVOTE ME !!!
HAVE A NICE DAY !!
"""