Galle, LK--:--5/6 sites live

Project 6 Loan Status Prediction

සිව්වෙනි සම්පූර්ණ project එක, numeric සහ categorical features මිශ්‍ර වුන dataset එකක් — course එකෙන් එළියේදි ඔයාට හම්බුවෙන ඇත්තම, අවුල් සහගත data shape එකමයි.

A fourth complete project, on a dataset with a mix of numeric and categorical features together — exactly the messy, real shape of data you'll meet outside a course.

11 May 20265 min readSelf Guide · Stage 14#Maths & First Models#Hands-on Projects#Machine learning

Lesson 31 of 64 · Part 2 · Maths & First Models · Hands-on Projects · bilingual notes (English + සිංහල)

Why this matters. A fourth complete project, on a dataset with a mix of numeric and categorical features together — exactly the messy, real shape of data you'll meet outside a course.

සිංහලෙන්: සිව්වෙනි සම්පූර්ණ project එක, numeric සහ categorical features මිශ්‍ර වුන dataset එකක් — course එකෙන් එළියේදි ඔයාට හම්බුවෙන ඇත්තම, අවුල් සහගත data shape එකමයි.

What you'll use this for. This is the same core problem banks solve every day: given an applicant's details, should this loan be approved?

සිංහලෙන්: Banks දවසින් දවස විසඳන core problem එකමයි මේක: applicant කෙනෙකුගෙ details බලලා, මේ loan එක approve කරන්නද?

Key principles

  • Categorical and numeric columns need different pre-processing before they can sit in the same matrix. You'll encode the categorical columns (like Part 1's label encoding) and standardize the numeric ones — a mix that shows up constantly in real datasets.
    Categorical සහ numeric columns, එකම matrix එකේ ඉන්න කලින් වෙනස් pre-processing එකක් ඕන

Walkthrough

Importing the Dependencies

ExamplePython
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.metrics import accuracy_score

Data Collection and Processing

ExamplePython
# loading the dataset to pandas DataFrame
loan_dataset = pd.read_csv('/content/dataset.csv')
ExamplePython
type(loan_dataset)

Output

Output
pandas.core.frame.DataFrame
ExamplePython
# printing the first 5 rows of the dataframe
loan_dataset.head()
Loan_IDGenderMarriedDependentsEducationSelf_EmployedApplicantIncome…
0LP001002MaleNo0GraduateNo5849…
1LP001003MaleYes1GraduateNo4583…
2LP001005MaleYes0GraduateYes3000…
3LP001006MaleYes0Not GraduateNo2583…
4LP001008MaleNo0GraduateNo6000…
ExamplePython
# number of rows and columns
loan_dataset.shape

Output

Output
(614, 13)
ExamplePython
# statistical measures
loan_dataset.describe()
ApplicantIncomeCoapplicantIncomeLoanAmountLoan_Amount_TermCredit_History
count614.000000614.000000592.000000600.00000564.000000
mean5403.4592831621.245798146.412162342.000000.842199
std6109.0416732926.24836985.58732565.120410.364878
min150.0000000.0000009.00000012.000000.000000
25%2877.5000000.000000100.000000360.000001.000000
50%3812.5000001188.500000128.000000360.000001.000000

… 2 more rows

ExamplePython
# number of missing values in each column
loan_dataset.isnull().sum()

Output

Output
Loan_ID               0
Gender               13
Married               3
Dependents           15
Education             0
Self_Employed        32
ApplicantIncome       0
CoapplicantIncome     0
LoanAmount           22
Loan_Amount_Term     14
Credit_History       50
Property_Area         0
Loan_Status           0
dtype: int64
ExamplePython
# dropping the missing values
loan_dataset = loan_dataset.dropna()
ExamplePython
# number of missing values in each column
loan_dataset.isnull().sum()

Output

Output
Loan_ID              0
Gender               0
Married              0
Dependents           0
Education            0
Self_Employed        0
ApplicantIncome      0
CoapplicantIncome    0
LoanAmount           0
Loan_Amount_Term     0
Credit_History       0
Property_Area        0
Loan_Status          0
dtype: int64
ExamplePython
# label encoding
loan_dataset.replace({"Loan_Status":{'N':0,'Y':1}},inplace=True)
ExamplePython
# printing the first 5 rows of the dataframe
loan_dataset.head()
Loan_IDGenderMarriedDependentsEducationSelf_EmployedApplicantIncome…
1LP001003MaleYes1GraduateNo4583…
2LP001005MaleYes0GraduateYes3000…
3LP001006MaleYes0Not GraduateNo2583…
4LP001008MaleNo0GraduateNo6000…
5LP001011MaleYes2GraduateYes5417…
ExamplePython
# Dependent column values
loan_dataset['Dependents'].value_counts()

Output

Output
0     274
2      85
1      80
3+     41
Name: Dependents, dtype: int64
ExamplePython
# replacing the value of 3+ to 4
loan_dataset = loan_dataset.replace(to_replace='3+', value=4)
ExamplePython
# dependent values
loan_dataset['Dependents'].value_counts()

Output

Output
0    274
2     85
1     80
4     41
Name: Dependents, dtype: int64

Data Visualization

ExamplePython
# education & Loan Status
sns.countplot(x='Education',hue='Loan_Status',data=loan_dataset)

Output

Output
<matplotlib.axes._subplots.AxesSubplot at 0x7f60020a91d0>

Project 6 Loan Status Prediction — figure 1

ExamplePython
# marital status & Loan Status
sns.countplot(x='Married',hue='Loan_Status',data=loan_dataset)

Output

Output
<matplotlib.axes._subplots.AxesSubplot at 0x7f6000f5a650>

Project 6 Loan Status Prediction — figure 2

ExamplePython
# convert categorical columns to numerical values
loan_dataset.replace({'Married':{'No':0,'Yes':1},'Gender':{'Male':1,'Female':0},'Self_Employed':{'No':0,'Yes':1},
                      'Property_Area':{'Rural':0,'Semiurban':1,'Urban':2},'Education':{'Graduate':1,'Not Graduate':0}},inplace=True)
ExamplePython
loan_dataset.head()
Loan_IDGenderMarriedDependentsEducationSelf_EmployedApplicantIncome…
1LP001003111104583…
2LP001005110113000…
3LP001006110002583…
4LP001008100106000…
5LP001011112115417…
ExamplePython
# separating the data and label
X = loan_dataset.drop(columns=['Loan_ID','Loan_Status'],axis=1)
Y = loan_dataset['Loan_Status']
ExamplePython
print(X)
print(Y)

Output

Output
     Gender  Married  ... Credit_History  Property_Area
1         1        1  ...            1.0              0
2         1        1  ...            1.0              2
3         1        1  ...            1.0              2
4         1        0  ...            1.0              2
5         1        1  ...            1.0              2
..      ...      ...  ...            ...            ...
609       0        0  ...            1.0              0
610       1        1  ...            1.0              0
611       1        1  ...            1.0              2
612       1        1  ...            1.0              2
613       0        0  ...            0.0              1

[480 rows x 11 columns]
1      0
2      1
3      1
4      1
… (8 more lines)

Train Test Split

ExamplePython
X_train, X_test,Y_train,Y_test = train_test_split(X,Y,test_size=0.1,stratify=Y,random_state=2)
ExamplePython
print(X.shape, X_train.shape, X_test.shape)

Output

Output
(480, 11) (432, 11) (48, 11)

Training the model:

Support Vector Machine Model

ExamplePython
classifier = svm.SVC(kernel='linear')
ExamplePython
#training the support Vector Macine model
classifier.fit(X_train,Y_train)

Output

Output
SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma='scale', kernel='linear',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)

Model Evaluation

ExamplePython
# accuracy score on training data
X_train_prediction = classifier.predict(X_train)
training_data_accuray = accuracy_score(X_train_prediction,Y_train)
ExamplePython
print('Accuracy on training data : ', training_data_accuray)

Output

Output
Accuracy on training data :  0.7986111111111112
ExamplePython
# accuracy score on training data
X_test_prediction = classifier.predict(X_test)
test_data_accuray = accuracy_score(X_test_prediction,Y_test)
ExamplePython
print('Accuracy on test data : ', test_data_accuray)

Output

Output
Accuracy on test data :  0.8333333333333334

Making a predictive system


Keep going: ← Lesson 30 · Lesson 32 → · Track it in the Self Guide