Skip to main content

Machine Learning: Loan Approval Prediction

Published 4 min readMarkdown

Loan approval prediction is a financial modeling and machine learning task that involves using historical data and predictive algorithms to assess the likelihood of an individual or business being approved for a loan. The goal is to build a predictive model that can accurately predict whether a loan application will be approved or denied by a financial institution, such as a bank or credit union.

Here's a general description of the loan approval prediction process:

  1. Data Collection: The first step is to gather historical data related to loan applications. This data typically includes information about applicants, such as their income, credit score, employment history, loan amount requested, and other relevant factors. Additionally, the dataset should include information on whether each application was approved or denied.

    Reading the data set through pandas

  2. Data Preprocessing: Once the data is collected, it needs to be cleaned and prepared for analysis. This involves handling missing values, encoding categorical variables, and scaling or normalizing numerical features.

  3. Feature Selection/Engineering: Feature selection and engineering are important steps to identify the most relevant variables that influence loan approval decisions. This may involve creating new features or transforming existing ones to better represent the data.

  4. Model Building: Machine learning algorithms are applied to the preprocessed data to build predictive models. Common algorithms used for loan approval prediction include logistic regression, decision trees, random forests, support vector machines, and neural networks.

  5. Model Training: The historical data is split into a training set and a testing set. The model is trained on the training set, and its performance is evaluated using the testing set. The model learns to identify patterns and relationships in the data that are indicative of loan approval outcomes.

  6. Model Evaluation: Various evaluation metrics are used to assess the performance of the predictive model. Common metrics include accuracy, precision, recall, F1 score, and ROC-AUC. The choice of metrics depends on the specific goals of the loan approval process.

Code:

Importing the libraries

Reading the dataset we need to read the dataset through the pandas read command.

Now, getting five rows and column through the head command

Now, getting the bottom five rows and columns through tail command

Dropping the Loan_Id column because it is not in use

Getting more information about the dataset which gives the column, non - null count and the dtype of the dataset

Now, describe the mean, standard deviation, minimum and maximum of the data.

Finding the missing values in the dataset through the isnull().sum() method. which is used in programming to check whether a variable or object is set to a null value or contains no value.

Value_counts gives the used to count the unique values in a categorical column of your dataset.

Now, replacing the zeros with the mode of that column where we need to give inplace is equals to True.

Now, checking again with the isnull method where we can see that the null values is replaced with the mode values.

As you can see that the Loan_Amount, Loan_Amount_Term and Credit_History are still having non-zeros values we need to replace it the other values.

Finally checking it again with the method isnull().sum() as you can see that there are no null values present in the dataset.

Now, plotting the graph using the pie chart for Loan Approval Status.

Plotting the Histogram for getting the ApplicantIncome distribution.

Now, converting the categorical columns to numerical using the one-hot encoding.

Now, spliting the dataset into the X and y where X is the features and the y is the target.

Now, spliting the dataset into the train_test_split then we will be using the sklearn model selection for the train_test_split. In that we will be using the X, y, the test_size and random_state.

Now, for all we need to do the StandardScaler for scaling the numerical columns.

Here we will be using the RandomForestClassifier alogrithm with the sklearn ensembel method.

finally we are building the model using the SVC and then we are taking the random_state = 42.

Finally fiting the model with the X_train and y_train.

We need to predict the model we need to take the X_test for it. we can see that the model is predicting for the X_test and giving the accurate assumptions for it.

Thanks for reading,

Mohammed Muqafamuddin.

No comments yet. Be the first to comment.

More from this blog