<jupyter_start><jupyter_text>Dataset Information![](attachment:image.png)  Import modules<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Loading the dataset<jupyter_code>df = pd.read_csv("Loan Prediction Dataset.csv")
df.head()
df.describe()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 614 entries, 0 to 613
Data columns (total 13 columns):
Loan_ID              614 non-null object
Gender               601 non-null object
Married              611 non-null object
Dependents           599 non-null object
Education            614 non-null object
Self_Employed        582 non-null object
ApplicantIncome      614 non-null int64
CoapplicantIncome    614 non-null float64
LoanAmount           592 non-null float64
Loan_Amount_Term     600 non-null float64
Credit_History       564 non-null float64
Property_Area        614 non-null object
Loan_Status          614 non-null object
dtypes: float64(4), int64(1), object(8)
memory usage: 62.5+ KB<jupyter_text>Preprocessing the dataset<jupyter_code># finding the null values
df.isnull().sum()
#filling the missing values for numerical terms - mean
df['LoanAmount'] = df['LoanAmount'].fillna(df['LoanAmount'].mean())
df['Loan_Amount_Term'] = df['Loan_Amount_Term'].fillna(df['Loan_Amount_Term'].mean())
df['Credit_History'] = df['Credit_History'].fillna(df['Credit_History'].mean())
# fill the missing values for categorical terms - mode
df['Gender'] = df["Gender"].fillna(df['Gender'].mode()[0])
df['Married'] = df["Married"].fillna(df['Married'].mode()[0])
df['Dependents'] = df["Dependents"].fillna(df['Dependents'].mode()[0])
df['Self_Employed'] = df["Self_Employed"].fillna(df['Self_Employed'].mode()[0])
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis<jupyter_code># categorical attributes visualization
sns.countplot(df['Gender'])
sns.countplot(df['Married'])
sns.countplot(df['Dependents'])
sns.countplot(df['Education'])
sns.countplot(df['Self_Employed'])
sns.countplot(df['Property_Area'])
sns.countplot(df['Loan_Status'])
# numerical attributes visualization
sns.distplot(df["ApplicantIncome"])
sns.distplot(df["CoapplicantIncome"])
sns.distplot(df["LoanAmount"])
sns.distplot(df['Loan_Amount_Term'])
sns.distplot(df['Credit_History'])<jupyter_output><empty_output><jupyter_text>Creation of new attributes<jupyter_code># total income
df['Total_Income'] = df['ApplicantIncome'] + df['CoapplicantIncome']
df.head()
sns.distplot(df["Total_Income"])
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 614 entries, 0 to 613
Data columns (total 14 columns):
Loan_ID              614 non-null object
Gender               614 non-null object
Married              614 non-null object
Dependents           614 non-null object
Education            614 non-null object
Self_Employed        614 non-null object
ApplicantIncome      614 non-null int64
CoapplicantIncome    614 non-null float64
LoanAmount           614 non-null float64
Loan_Amount_Term     614 non-null float64
Credit_History       614 non-null float64
Property_Area        614 non-null object
Loan_Status          614 non-null object
Total_Income         614 non-null float64
dtypes: float64(5), int64(1), object(8)
memory usage: 67.3+ KB<jupyter_text>Log Transformation<jupyter_code># apply log transformation to the attribute
df['ApplicantIncomeLog'] = np.log(df['ApplicantIncome'])
sns.distplot(df["ApplicantIncomeLog"])
df['CoapplicantIncomeLog'] = np.log(df['CoapplicantIncome'])
sns.distplot(df["ApplicantIncomeLog"])
df['LoanAmountLog'] = np.log(df['LoanAmount'])
sns.distplot(df["LoanAmountLog"])

df['Loan_Amount_Term_Log'] = np.log(df['Loan_Amount_Term'])
sns.distplot(df["Loan_Amount_Term_Log"])
df['Total_Income_Log'] = np.log(df['Total_Income'])
sns.distplot(df["Total_Income_Log"])<jupyter_output><empty_output><jupyter_text>Coorelation Matrix<jupyter_code>corr = df.corr()
plt.figure(figsize=(15,10))
sns.heatmap(corr, annot = True, cmap="YlGnBu")
df.head()
df.info()
# dropping unnecessary columns
cols = ['ApplicantIncome', 'CoapplicantIncome', "LoanAmount", 
        "Loan_Amount_Term",  "Total_Income", 'Loan_ID', 
        'CoapplicantIncomeLog']
df = df.drop(columns=cols, axis=1)
df.head()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 614 entries, 0 to 613
Data columns (total 12 columns):
Gender                  614 non-null object
Married                 614 non-null object
Dependents              614 non-null object
Education               614 non-null object
Self_Employed           614 non-null object
Credit_History          614 non-null float64
Property_Area           614 non-null object
Loan_Status             614 non-null object
ApplicantIncomeLog      614 non-null float64
LoanAmountLog           614 non-null float64
Loan_Amount_Term_Log    614 non-null float64
Total_Income_Log        614 non-null float64
dtypes: float64(5), object(7)
memory usage: 57.7+ KB<jupyter_text>Label Encoding<jupyter_code>from sklearn.preprocessing import LabelEncoder
cols = ['Gender',"Married","Education",'Self_Employed',
        "Property_Area","Loan_Status", "Dependents"]
le = LabelEncoder()
for col in cols:
    df[col] = le.fit_transform(df[col])
df.head()
df.describe()<jupyter_output><empty_output><jupyter_text>Train-Test Split<jupyter_code># specify input and output attributes
X = df.drop(columns=['Loan_Status'], axis=1)
y = df['Loan_Status']
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)<jupyter_output><empty_output><jupyter_text>Model Training<jupyter_code># classify function
from sklearn.model_selection import cross_val_score
def classify(model, x, y):
    x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
    model.fit(x_train, y_train)
    print("Accuracy is", model.score(x_test, y_test)*100)
    
    # cross validation - it is used for better validation of model
    # taking cv-5, train-4, test-1
    score = cross_val_score(model, x, y, cv=5)
    print("Cross validation is",np.mean(score)*100)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
classify(model, X, y)
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
classify(model, X, y)<jupyter_output>Accuracy is 78.57142857142857
Cross validation is 78.01412768226042<jupyter_text>Hyperparameter tuning<jupyter_code>model = RandomForestClassifier(n_estimators=120, min_samples_split=25,max_depth=15, max_features=1)
classify(model, X, y)<jupyter_output>Accuracy is 77.92207792207793
Cross validation is 80.62108489937359<jupyter_text>Confusion Matrix<jupyter_code>model = RandomForestClassifier()
model.fit(x_train, y_train)

from sklearn.metrics import confusion_matrix
y_pred = model.predict(x_test)
cm = confusion_matrix(y_test, y_pred)
cm
sns.heatmap(cm, annot=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11820861996349853, 'fraction_numerical': 0.04281903692264495, 'mean_word_length': 5.412241224122412, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '119423', 'license': 'Apache-2.0', 'n_tokens_mistral': 2659, 'n_tokens_neox': 2402, 'n_words': 506, 'path': 'Prediction_main_v1.0.ipynb', 'repo_name': 'akshatrb/Loan-Prediction-V1', 'size': '7123'}