# Machine Learning: Loan Approval Prediction

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694674060371/74472f44-5c90-4bf8-be9a-d4d88b0ca648.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694674098397/4e9b076f-ef00-4a1e-8690-e5f08cb7be49.png align="center")

Now, getting five rows and column through the head command

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694674200514/fe68233c-6074-4a38-b616-03a49b1e78c8.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694674250101/c3a09966-3145-4626-b47f-c1ba8f9ba1ca.png align="center")

Dropping the Loan\_Id column because it is not in use

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694674306492/4188772f-2c9a-447e-8da3-86c1e2eacc95.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694674341437/6528693b-18bf-4d37-9579-e129dfa7cdd8.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694674473841/4fa7f4e0-ac63-4b57-8df8-72fbec59d133.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695980695899/51208a5c-1a47-4fb0-b036-4413940767f5.png align="center")

Value\_counts gives the used to count the unique values in a categorical column of your dataset.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695980727842/61a82628-5d33-48dd-b010-224f9bbdbf90.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695980987199/9a833621-778f-426d-bed8-4a5a49372aa6.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981013929/228b774f-892c-4d09-81d3-7967ae470b1e.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981040813/b1ead855-9813-4fac-9357-1e66d40cde4f.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981064323/83d1edcb-c49d-4ac9-bb52-d30f1d033a9a.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981100669/64008965-56f2-4c51-ad44-26536d8bfea3.png align="center")

Plotting the Histogram for getting the ApplicantIncome distribution.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981895162/afeccdb6-0ac3-44d2-8298-fae038dc5242.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981863645/1bacc287-9baf-4bf6-bb6d-1aea379a16b1.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981835227/cfa57934-f298-4302-b875-345b430cab04.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981805637/8f8d9de1-f538-4d7b-a527-30b753e34d4d.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981778761/dfcdfa37-0c31-4f6a-9e8a-ba1eb959c3e8.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981675507/b2062e4c-ee66-449c-8745-186a146abf64.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981637200/8044051b-53eb-45d4-8996-6c1640269c5a.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695981600740/d03b68d3-550e-4833-b10e-a6f46cb0fbfd.png align="center")

Thanks for reading,

Mohammed Muqafamuddin.
