<jupyter_start><jupyter_text>More males have applied for the loan than the females<jupyter_code>sns.countplot(df['Married'])<jupyter_output><empty_output><jupyter_text>More married people have applied for the loa than unmarried people<jupyter_code>sns.countplot(df['Dependents'])<jupyter_output><empty_output><jupyter_text>The number of people who applied for the loan decreases with the increase in the number of dependents<jupyter_code>sns.countplot(df['Education'])<jupyter_output><empty_output><jupyter_text>So more Graduates have applied for the loan than the Not Graduate people<jupyter_code>sns.countplot(df['Self_Employed'])<jupyter_output><empty_output><jupyter_text>So the people who are not Self Employed applied more for the Loan than the People who are Self Employed<jupyter_code>sns.distplot(df['ApplicantIncome'])
sns.distplot(df['CoapplicantIncome'])
sns.distplot(df['LoanAmount'])
sns.countplot(df['Loan_Amount_Term'])<jupyter_output><empty_output><jupyter_text>Most of the people who applied for the loan have applied for the Loan Amount term of 360 months<jupyter_code>sns.countplot(df['Credit_History'])<jupyter_output><empty_output><jupyter_text>so most of the people who applied for the loan have the credit history already<jupyter_code>sns.countplot(df['Property_Area'])<jupyter_output><empty_output><jupyter_text>People from Semiurban area have applied the most for the loan while the people from the rural area have applied the least<jupyter_code>sns.countplot(df['Loan_Status'])<jupyter_output><empty_output><jupyter_text>This is the target variable!! and we can clearly see that this 3/4 of the people who applied for the loan have got the loan approved  Now, Let's Analyse more through some more indepth visualization<jupyter_code>sns.countplot(df['Gender'],hue=df['Married'])<jupyter_output><empty_output><jupyter_text>So, the above plot shows that the number of married males is more than the number of unmarried males who applied for the Loan whereas the number of unmarried females is more than the number of married females for the Loan<jupyter_code>sns.countplot(df['Gender'],hue=df['Dependents'])<jupyter_output><empty_output><jupyter_text>There is not much difference in the no. of dependents on the basis of Gender<jupyter_code>sns.countplot(df['Gender'],hue=df['Education'])
sns.countplot(df['Gender'],hue=df['Self_Employed'])
sns.countplot(df['Gender'],hue=df['Loan_Amount_Term'])
sns.countplot(df['Gender'],hue=df['Credit_History'])
sns.countplot(df['Gender'],hue=df['Property_Area'])
sns.countplot(df['Married'],hue=df['Dependents'])<jupyter_output><empty_output><jupyter_text>This trend shown in the above plot is very much obvious as married person will obviously have more no of dependents(family) compared to the the unmarried person.<jupyter_code>sns.countplot(df['Married'],hue=df['Education'])
a=df[(df['Married']=='No')&(df['Education']=='Graduate')]['Loan_ID'].count()
b=df[(df['Married']=='No')&(df['Education']=='Not Graduate')]['Loan_ID'].count()
c=df[(df['Married']=='Yes')&(df['Education']=='Graduate')]['Loan_ID'].count()
d=df[(df['Married']=='Yes')&(df['Education']=='Not Graduate')]['Loan_ID'].count()
e=a/(a+b)
f=c/(c+d)
print(e,f)<jupyter_output>0.7887323943661971 0.7763819095477387<jupyter_text>Thus, Probability of a unmarried person of being graduate is slightly more than a married person!! :)<jupyter_code>sns.countplot(df['Married'],hue=df['Self_Employed'])<jupyter_output><empty_output><jupyter_text>Here also the probability of a Married person of not being Self Employed is less than the Unmarried person!!<jupyter_code>plt.figure(figsize=(15,7))
sns.countplot(df['Married'],hue=df['Loan_Amount_Term'])<jupyter_output><empty_output><jupyter_text>So the married person seeks for the long Loan amount term than the unmarried person.<jupyter_code>sns.countplot(df['Married'],hue=df['Credit_History'])
sns.countplot(df['Married'],hue=df['Property_Area'],palette='PuBu')
sns.countplot(df['Dependents'],hue=df['Education'],palette='Reds')
sns.countplot(df['Dependents'],hue=df['Self_Employed'],palette='Accent')
plt.figure(figsize=(15,7))
sns.countplot(df['Dependents'],hue=df['Loan_Amount_Term'])
sns.countplot(df['Dependents'],hue=df['Credit_History'])
sns.countplot(df['Dependents'],hue=df['Property_Area'])<jupyter_output><empty_output><jupyter_text>Here we have something to notice!!. In case where no of dependents is 1 most people are from Urban area and the persons from Rural Area are less than the persons from Urban Area, Similar trend we can observe for no of dependents=2 which is something opposite to the trends in cases where no of dependents are 0 and 3+<jupyter_code>sns.countplot(df['Education'],hue=df['Self_Employed'])
sns.countplot(df['Education'],hue=df['Loan_Amount_Term'])
sns.countplot(df['Education'],hue=df['Credit_History'])
sns.countplot(df['Education'],hue=df['Property_Area'])<jupyter_output><empty_output><jupyter_text>SO!!! the people who are not graduate have most no persons from Rural Area and least from Urban Area and the people who are graduate have most no of persons from Semiurban area and people from Urban Area are more than the People from Rural Area<jupyter_code>sns.countplot(df['Self_Employed'],hue=df['Loan_Amount_Term'])
sns.countplot(df['Self_Employed'],hue=df['Credit_History'])
sns.countplot(df['Self_Employed'],hue=df['Property_Area'])<jupyter_output><empty_output><jupyter_text>People who are Self Employed have people from Rural Area more than people from the Urban Area which is opposite in case of people who are not self employed.<jupyter_code>sns.countplot(df['Loan_Amount_Term'],hue=df['Credit_History'])
sns.countplot(df['Loan_Amount_Term'],hue=df['Property_Area'])
sns.countplot(df['Credit_History'],hue=df['Property_Area'])
sns.countplot(df['Gender'],hue=df['Loan_Status'])
sns.countplot(df['Married'],hue=df['Loan_Status'])
sns.countplot(df['Dependents'],hue=df['Loan_Status'])
sns.countplot(df['Education'],hue=df['Loan_Status'])
sns.countplot(df['Self_Employed'],hue=df['Loan_Status'])
sns.countplot(df['Loan_Amount_Term'],hue=df['Loan_Status'])
sns.countplot(df['Credit_History'],hue=df['Loan_Status'])<jupyter_output><empty_output><jupyter_text>SO!! Clearly the person who have a Credit history have a high probability of getting the loan as compared to the one who doesn't have credit history<jupyter_code>sns.countplot(df['Property_Area'],hue=df['Loan_Status'])
sns.relplot(x='ApplicantIncome',y='CoapplicantIncome',data=df,hue='Property_Area')
sns.relplot(x='ApplicantIncome',y='LoanAmount',data=df,hue='Property_Area')
sns.relplot(x='LoanAmount',y='CoapplicantIncome',data=df,hue='Property_Area')
# sns.pairplot(df,hue='Loan_Status')<jupyter_output><empty_output><jupyter_text>Let's Remove OUTLiers!!!<jupyter_code>#sns.boxplot(df['ApplicantIncome'])
#The Outliers in this attributes are natural Outliers.
df=df[df['ApplicantIncome']<8000]
#sns.boxplot(df['CoapplicantIncome'])
df=df[df['CoapplicantIncome']<7000]
sns.boxplot(df['LoanAmount'])<jupyter_output><empty_output><jupyter_text>Let's deal with null values<jupyter_code>df.isnull().sum()
#For Married

df.dropna(subset=['Married','Gender'],axis=0,inplace=True)
#For Gender

df[df['Gender'].isnull()]
df['Gender'].fillna('Male',inplace=True)
#For Dependents

df[df['Dependents'].isnull()]
df['Dependents'].fillna(0,inplace=True)
#For Self Employed

df[df['Self_Employed'].isnull()]
df['Self_Employed'].fillna('No',inplace=True)
#For Loan Amount Term

df['Loan_Amount_Term'].fillna(360,inplace=True)
#For LoanAmount

df['LoanAmount'].fillna(df['LoanAmount'].median(),inplace=True)
#For Credit History
df[df['Credit_History'].isnull()]
df['Credit_History'].fillna(0,inplace=True)<jupyter_output><empty_output><jupyter_text>Let's do some Feature Engineering<jupyter_code>#For Dependents

df['Dependents'].replace('3+',3,inplace=True)
df['Dependents']=pd.to_numeric(df['Dependents'])
#Encoding all the variables using Ordinal Encoding.

import category_encoders as ce

 #create object of Ordinalencoding
#1.
encoder1= ce.OrdinalEncoder(cols=['Married'],return_df=True,
                           mapping=[{'col':"Married",
'mapping':{'No':0,'Yes':1}}])#
df_train_transformed = encoder1.fit_transform(df)
df=df_train_transformed

#2.
encoder2= ce.OrdinalEncoder(cols=['Gender'],return_df=True,
                           mapping=[{'col':"Gender",
'mapping':{'Female':0,'Male':1}}])
df_train_transformed = encoder2.fit_transform(df)
df=df_train_transformed

#3.
encoder3= ce.OrdinalEncoder(cols=['Education'],return_df=True,
                           mapping=[{'col':"Education",
'mapping':{'Not Graduate':0,'Graduate':1}}])
df_train_transformed = encoder3.fit_transform(df)
df=df_train_transformed

#4.
encoder4= ce.OrdinalEncoder(cols=['Self_Employed'],return_df=True,
                           mapping=[{'col':"Self_Employed",
'mapping':{'No':0,'Yes':1}}])
df_train_transformed = encoder4.fit_transform(df)
df=df_train_transformed

#5.
encoder5= ce.OrdinalEncoder(cols=['Property_Area'],return_df=True,
                           mapping=[{'col':"Property_Area",
'mapping':{'Rural':0,'Semiurban':1,'Urban':2}}])
df_train_transformed = encoder5.fit_transform(df)
df=df_train_transformed

#6.
encoder= ce.OrdinalEncoder(cols=['Loan_Status'],return_df=True,
                         mapping=[{'col':"Loan_Status",
'mapping':{'N':0,'Y':1}}])
df_train_transformed = encoder.fit_transform(df)
df=df_train_transformed

df_train_transformed
a1=pd.get_dummies(df['Gender'],prefix='G')
df['G_Male']=a1['G_Male']
df['G_Female']=a1['G_Female']

a2=pd.get_dummies(df['Married'],prefix='M')
df['M_Yes']=a2['M_Yes']
df['M_No']=a2['M_No']

a3=pd.get_dummies(df['Education'])
df['Graduate']=a3['Graduate']
df['Not Graduate']=a3['Not Graduate']

a4=pd.get_dummies(df['Self_Employed'],prefix='S')
df['S_No']=a4['S_No']
df['S_Yes']=a4['S_Yes']

a5=pd.get_dummies(df['Property_Area'])
df['Rural']=a5['Rural']
df['Urban']=a5['Urban']
df['Semiurban']=a5['Semiurban']
df.head()<jupyter_output><empty_output><jupyter_text>Let's Classify the Data Using a suitable Classifier Algorithm<jupyter_code>#For Ordinal encoding.

X=df[['Gender','Married','Dependents','Self_Employed','ApplicantIncome','CoapplicantIncome','LoanAmount','Loan_Amount_Term','Credit_History','Education','Property_Area']]
y=df['Loan_Status']

#Using train test split
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.30,random_state=0)
#For the One Hot Encoding 

X=df[['G_Male','G_Female','Dependents','M_Yes','M_No','S_Yes','S_No','ApplicantIncome','CoapplicantIncome','LoanAmount','Loan_Amount_Term','Credit_History','Not Graduate','Graduate','Rural','Urban','Semiurban']]
y=df['Loan_Status']

#using train test split
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.30,random_state=0)
#Logistic Regression

from sklearn.linear_model import LogisticRegression
model1=LogisticRegression(random_state=0)
model1.fit(X_train,y_train)
y_pred1=model1.predict(X_test)
#Decision Tree Classifier

from sklearn.tree import DecisionTreeClassifier
model2=DecisionTreeClassifier(random_state=0)
model2.fit(X_train,y_train)
y_pred2=model2.predict(X_test)
#RandomForest CLassifier

from sklearn.ensemble import RandomForestClassifier
model3=RandomForestClassifier(n_estimators=250,max_depth=20,random_state=0)
model3.fit(X_train,y_train)
y_pred3=model3.predict(X_test)
#SVM Classifier

from sklearn.svm import SVC
model4=SVC(kernel='rbf')
model4.fit(X_train,y_train)
y_pred4=model4.predict(X_test)
#XGBoost Classifier

from xgboost import XGBClassifier
model5=XGBClassifier()
model5.fit(X_train,y_train)
y_pred5=model5.predict(X_test)
#Evaluating each model

from sklearn.metrics import classification_report,confusion_matrix

#Printing the Classification Report.
print(classification_report(y_test,y_pred5))

#Printing the Confusion Matrix.
print(confusion_matrix(y_test,y_pred5))<jupyter_output>precision    recall  f1-score   support

           0       0.52      0.54      0.53        52
           1       0.81      0.80      0.80       128

    accuracy                           0.72       180
   macro avg       0.66      0.67      0.67       180
weighted avg       0.73      0.72      0.72       180

[[ 28  24]
 [ 26 102]]<jupyter_text>Let's go ahead with Random Forest and XGBoost Classifier as they are performing better on comparing with others.we will perform hyperparameter tuning using GridSearchCv for better performance.<jupyter_code>#For Random FOrest CLassifier.

from sklearn.model_selection import GridSearchCV

# Create the parameter grid for the Random Forest Classifier. 
param_grid = {
    'bootstrap': [True],
    'max_depth': [10,15,20,25,30],
    'min_samples_leaf': [3, 4, 5,6],
    'min_samples_split': [6, 8, 10,12,15],
    'n_estimators': [100, 200, 250, 300]
}
# Create a based model
rf = RandomForestClassifier(random_state=0)
# Instantiate the grid search model
grid_search = GridSearchCV(estimator = rf, param_grid = param_grid, 
                          cv = 3, n_jobs = -1, verbose = 2)

grid_search.fit(X_train,y_train)
grid_search.best_params_
model=RandomForestClassifier(n_estimators=200,max_depth=10,min_samples_leaf=6,min_samples_split=15,random_state=0)
model.fit(X_train,y_train)
y_pred=model.predict(X_test)

#Printing the Classification Report.
print(classification_report(y_test,y_pred))

#Printing the Confusion Matrix.
print(confusion_matrix(y_test,y_pred))
#For XGBoost Classifier

#Creating the parameter grid for XGboost Classifier.
param_grid = {
    'learning_rate': [0.1,0.2,0.3],
    'max_depth': [5,10,15],
    'gamma': [0.1, 0.2, 0.3,0.4],
    'reg_alpha':[1e-5, 1e-2, 0.1, 100],
    'min_samples_split': [6, 8, 10,12,15],
    'n_estimators': [10,20,30,40,50],
}
# Create a based model
xgb=XGBClassifier(random_state=0)
# Instantiate the grid search model
grid_search = GridSearchCV(estimator = xgb, param_grid = param_grid, 
                          cv = 3, n_jobs = -1, verbose = 2)

grid_search.fit(X_train,y_train)
grid_search.best_params_
model=XGBClassifier(learning_rate=0.2,max_depth=5,min_samples_split=6,n_estimators=10,reg_alpha=0.1,gamma=0.3,random_state=0)
model.fit(X_train,y_train)
y_pred=model.predict(X_test)

#Printing the Classification Report.
print(classification_report(y_test,y_pred))

#Printing the Confusion Matrix.
print(confusion_matrix(y_test,y_pred))
#importing the test data.
dft=pd.read_csv('test_Loan.csv')
dft
dft.isnull().sum()
#Removing the null values.

dft['Gender'].fillna('Male',inplace=True)
dft['Dependents'].fillna(0,inplace=True)
dft['Self_Employed'].fillna('No',inplace=True)
dft['Loan_Amount_Term'].fillna(360,inplace=True)
dft['LoanAmount'].fillna(dft['LoanAmount'].median(),inplace=True)
dft['Credit_History'].fillna(0,inplace=True)
#For Dependents

dft['Dependents'].replace('3+',3,inplace=True)
dft['Dependents']=pd.to_numeric(dft['Dependents'])
#Encoding all the variables using Ordinal Encoding.

import category_encoders as ce

 #create object of Ordinalencoding
#1.
encoder1= ce.OrdinalEncoder(cols=['Married'],return_df=True,
                           mapping=[{'col':"Married",
'mapping':{'No':0,'Yes':1}}])
df_train_transformed = encoder1.fit_transform(dft)
dft=df_train_transformed

#2.
encoder2= ce.OrdinalEncoder(cols=['Gender'],return_df=True,
                           mapping=[{'col':"Gender",
'mapping':{'Female':0,'Male':1}}])
df_train_transformed = encoder2.fit_transform(dft)
dft=df_train_transformed

#3.
encoder3= ce.OrdinalEncoder(cols=['Education'],return_df=True,
                           mapping=[{'col':"Education",
'mapping':{'Not Graduate':0,'Graduate':1}}])
df_train_transformed = encoder3.fit_transform(dft)
dft=df_train_transformed

#4.
encoder4= ce.OrdinalEncoder(cols=['Self_Employed'],return_df=True,
                           mapping=[{'col':"Self_Employed",
'mapping':{'No':0,'Yes':1}}])
df_train_transformed = encoder4.fit_transform(dft)
dft=df_train_transformed

#5.
encoder5= ce.OrdinalEncoder(cols=['Property_Area'],return_df=True,
                           mapping=[{'col':"Property_Area",
'mapping':{'Rural':0,'Semiurban':1,'Urban':2}}])
df_train_transformed = encoder5.fit_transform(dft)
dft=df_train_transformed

dft
X_train=df[['Gender','Married','Dependents','Self_Employed','ApplicantIncome','CoapplicantIncome','LoanAmount','Loan_Amount_Term','Credit_History','Education','Property_Area']]
y_train=df['Loan_Status']
X_test=dft[['Gender','Married','Dependents','Self_Employed','ApplicantIncome','CoapplicantIncome','LoanAmount','Loan_Amount_Term','Credit_History','Education','Property_Area']]
model=RandomForestClassifier(n_estimators=200,max_depth=10,min_samples_leaf=6,min_samples_split=15,random_state=0)
model.fit(X_train,y_train)
y_pred=model.predict(X_test)
model=XGBClassifier(learning_rate=0.2,max_depth=5,min_samples_split=6,n_estimators=10,reg_alpha=0.1,gamma=0.3,random_state=0)
model.fit(X_train,y_train)
y_pred=model.predict(X_test)
dft['pred']=y_pred
b=pd.DataFrame(dft[['Loan_ID','pred']])
b['pred'].replace(1,'Y',inplace=True)
b['pred'].replace(0,'N',inplace=True)
b
b=b.rename(columns={'pred':'Loan_Status'})
b=b.set_index('Loan_ID')
b
b.to_csv('Result_newss.csv')
b<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15153448976039915, 'fraction_numerical': 0.02181354063932239, 'mean_word_length': 8.135135135135135, 'pattern_counts': {'":': 0, '<': 97, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '24', 'dir': 'jupyter-structured-clean-dedup', 'id': '418169', 'license': 'Apache-2.0', 'n_tokens_mistral': 6524, 'n_tokens_neox': 6160, 'n_words': 1015, 'path': 'Loan_Prediction.ipynb', 'repo_name': 'Shubham-Chauhan019/Loan-Prediction', 'size': '17237'}