<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dhruv Tuteja on Medium]]></title>
        <description><![CDATA[Stories by Dhruv Tuteja on Medium]]></description>
        <link>https://medium.com/@dhruvtuteja?source=rss-efef1d3c36b0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*EQQdj589ckWWqs-07Ah1eA@2x.jpeg</url>
            <title>Stories by Dhruv Tuteja on Medium</title>
            <link>https://medium.com/@dhruvtuteja?source=rss-efef1d3c36b0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 20:21:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhruvtuteja/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Loan Status Predictor using Neural Networks and Random Forest Classifier]]></title>
            <link>https://dhruvtuteja.medium.com/loan-status-predictor-using-neural-networks-and-random-forest-classifier-2065d1b4fa4d?source=rss-efef1d3c36b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/2065d1b4fa4d</guid>
            <category><![CDATA[recall]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[random-forest-classifiers]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Dhruv Tuteja]]></dc:creator>
            <pubDate>Fri, 09 Jul 2021 22:07:13 GMT</pubDate>
            <atom:updated>2021-07-11T06:36:00.659Z</atom:updated>
            <content:encoded><![CDATA[<p>Non-performing loans (NPL) are loans for which the borrower has stopped making payments. Non-performing loans lead to losses, and impacts growth and earnings of the bank. In this article, I will be developing a Loan Status Predictor to predict whether the borrower will be able to repay the loan. Two models will be developed and compared using Neural Networks and Random Forest Classifier. Please feel free to check the code using this <a href="https://github.com/tutejadhruv/P2P-Lending-System.git">link</a>.</p><h4><strong>What is a Neural Network?</strong></h4><p>Neural Network functions similar to that of human brain. They are used to model complex relationships between input variables &amp; output variables, or detect patterns within data. Neural Network comprises of three components:<br>1. <strong>Input Layer</strong>: Input Data for Neural Network<br>2. <strong>Hidden Layer</strong>: This layer is located between Input and Output layer. All non linear computations/ conversions take place inside Hidden Layer on the basis of Activation Function. Hidden Layers are indecipherable.<br>3. <strong>Output Layer</strong>:<strong> </strong>This layer produces the final result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*v-qUrbIGURCdxjnG.png" /><figcaption>Neural Network</figcaption></figure><h4>What is Random Forest Classifier?</h4><p>Random Forests Classifier is an ensemble learning method to perform classification by creating multiple decision trees. Random Forest is a flexible model, which provides a good predicition accuracy but are difficult to interpret.</p><h4><strong>Dataset</strong></h4><p>The dataset used to train models is Online P2P Lending. The dataset can be pulled from the Git link shared above, or can also be downloaded from below link.<br>Link: <a href="https://www.kaggle.com/skihikingkevin/online-p2p-lending">https://www.kaggle.com/skihikingkevin/online-p2p-lending</a></p><h4>Loading Data on Google Colab from Google Drive</h4><p>The model can be run on local machine through Jupyter, PyCharm, etc. I will be using Google Colab, an online IDE to develop Mahine Learning/ Deep Learning Model. Please use the <a href="https://colab.research.google.com/notebooks/intro.ipynb?utm_source=scs-index">link </a>to try Google Colab.</p><pre>import numpy as np<br>import pandas as pd</pre><pre>from pydrive.auth import GoogleAuth<br>from pydrive.drive import GoogleDrive<br>from google.colab import auth<br>from oauth2client.client import GoogleCredentials</pre><pre># Authenticate and create the PyDrive client.<br>auth.authenticate_user()<br>gauth = GoogleAuth()<br>gauth.credentials = GoogleCredentials.get_application_default()<br>drive = GoogleDrive(gauth)</pre><pre># Link of the file on Google Drive<br>link = &quot;https://drive.google.com/file/d/1aGf9sDBY0-7UAsO2l7w9lxKBtO954bv4/view?usp=sharing&quot;</pre><pre>id = &quot;1aGf9sDBY0-7UAsO2l7w9lxKBtO954bv4&quot;</pre><pre>downloaded = drive.CreateFile({&#39;id&#39;:id}<br>downloaded.GetContentFile(&#39;Master_Loan_Summary.csv&#39;)<br>df = pd.read_csv(&#39;Master_Loan_Summary.csv&#39;)<br># Dataset is now stored in a Pandas Dataframe</pre><h4>Exploratory Data Analysis (EDA)</h4><pre># Importing matplotlib and seaborn for graphs<br>import matplotlib.pyplot as plt<br>import seaborn as sns</pre><pre>%matplotlib inline</pre><p><strong>Countplot of Target Variable loan_status_description</strong></p><pre>plt.figure(figsize=(10,6))<br>sns.countplot(x=&#39;loan_status_description&#39;, data=df, palette=&#39;Set1&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/955/1*OCYCqBp0QCaRrcv-VzQzCw.png" /><figcaption>Loan Status Description Count Plot</figcaption></figure><p>Majority of loans belong to Completed and Current Class. Cancelled loans seems to be lower than 1% in the dataset.</p><p><strong>Correlation Heatmap</strong></p><pre>plt.figure(figsize=(12,8))<br>sns.heatmap(df.corr(), annot=True, linewidths=0.5)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OXN4IOIK-u0_Oai8aUtoYg.png" /><figcaption>Pearson Correlation Heatmap</figcaption></figure><p>Through the heatmap, it can be infered that installment and amount_borrowed are highly correlated. Other continous variables in the dataset are mildly or moderately correlated.</p><p><strong>Data Source Count Plot</strong></p><pre>sns.countplot(x = &#39;data_source&#39;, data=df, hue = &#39;loan_status_description&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/861/1*I5nfunoH_ve1JvAV52K_lQ.png" /><figcaption>Data Source Count Plot</figcaption></figure><p>Majority of loans are being lent by ‘Lending Club’ in the dataset. Lending club have no ‘Defaulted’ and ‘Cancelled’ loans.</p><p><strong>Grade Count Plot</strong></p><pre>grade_order = sorted(df[&#39;grade&#39;].unique())</pre><pre>sns.countplot(x=&#39;grade&#39;, data=df, hue=&#39;loan_status_description&#39;, order=grade_order)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NqolKCiUMT4KfHZpgF6Ajg.png" /><figcaption>Grade Count Plot</figcaption></figure><p>Majority of loans belong to Grade B or Grade C. Among loans granted in all grades, Defaulted and Charged Off loans tend to increase among number of loans alloted in each grade as loans grant move from ‘A’ to ‘G’.</p><h4>Data Preprocessing</h4><p><strong>Dropping Rows with Cancelled and Current loan_status_description<br></strong>Here, the loans with Cancelled and Current status will be removed, as our model is built to predict whether the user will be able to repay, the loan. We can not know whether the ongoing loan, will be repaid or not.</p><pre>df = df[((df[&#39;loan_status_description&#39;] != &#39;CURRENT&#39;) &amp; (df[&#39;loan_status_description&#39;] != &#39;CANCELLED&#39;))]</pre><p><strong>Checking Null Values</strong></p><pre>df.isnull().sum()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*6pJb32EoJeQUWXTiIacl2Q.png" /></figure><p>In our dataset, only listing_title has null values.</p><p><strong>Handling Null Values of listing_title feature<br></strong>listing_title contains reason for loans, and has a value ‘others’. I will be setting all the Null values of the column to ‘others’ value, as we do not the reason of the loan.</p><pre>df[&#39;listing_title&#39;].fillna(value = &#39;other&#39;, inplace = True)</pre><p><strong>Converting loan_status_description (target variable) into numeric</strong></p><pre>df[&#39;loan_status_description&#39;] = df[&#39;loan_status_description&#39;].map({&#39;COMPLETED&#39;:0, &#39;CHARGEOFF&#39;:1, &#39;DEFAULTED&#39;:2})</pre><p><strong>Removing Irrelevant Columns</strong></p><pre>df.drop(columns=[&#39;origination_date&#39;, &#39;last_payment_date&#39;, &#39;next_payment_due_date&#39;, &#39;loan_number&#39;], inplace=True)</pre><p>Here we will drop loan origination_date, as we do not know yet if the loan is approved or not.</p><p>Also, we will be removing last_payment_date because every loan can have same or different dates, and it will not help in categorizing loan_status_description.</p><p>next_payment_due_date will be no use if loan_status is completed, defaulted or charged_off.</p><p>Every loan has a unique loan_number, and can be removed.</p><p><strong>Converting All Categorical Variables into Dummy Variables</strong></p><pre>df = pd.get_dummies(df, columns = [&#39;grade&#39;, &#39;listing_title&#39;, &#39;data_source&#39;, &#39;term&#39;], drop_first = True)</pre><p><strong>Multicollinearity Check with VIF (Variance Inflation Factor)<br></strong>Variance Inflation Factor (VIF) helps in identifying multicollinearity among the independent variables. Multicollinearity detection is a vital process, as it reduces the statistical significance of the independent variables. A large VIF number indicates, high correlation of a variable to other independent variables.<br>Generally VIF numbers interpreted as following:<br>a. 1 = Not Correlated<br>b. 1 to 5 = Moderately Correlated<br>c. More than 5 = Highly Correlated</p><p>VIF numbers are debatable and I will be using a general value of VIF = 10, i.e. all variables with VIF value of 10 will be analyzed and the variable with highest VIF will be removed from the dataset.</p><pre># Importing VIF Library<br>from statsmodels.stats.outliers_influence import variance_inflation_factor</pre><pre>def calc_vif(X):</pre><pre>     vif = pd.DataFrame()<br>     vif[&quot;variables&quot;] = X.columns<br>     vif[&quot;VIF&quot;] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]</pre><pre>     return(vif)</pre><pre># VIF Multicollinearity check<br>X = df[[&#39;amount_borrowed&#39;, &#39;borrower_rate&#39;, &#39;installment&#39;, &#39;principal_balance&#39;, &#39;principal_paid&#39;, &#39;interest_paid&#39;, &#39;late_fees_paid&#39;, &#39;debt_sale_proceeds_received&#39;, &#39;days_past_due&#39;]]</pre><pre>vif = calc_vif(X)<br>vif[vif[&#39;VIF&#39;]&gt;10]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*uXXhNGsYoVEiuzh0Bp0kQg.png" /><figcaption>VIF Result</figcaption></figure><p>Given, the above result, amount_borrowed will be removed as it has the highest VIF value. <br>Note: VIF is only run on continous variables. For categorical variables Generalized VIF test, Chi Square Test, etc. can be performed.</p><pre># Dropping amount_borrowed<br>X.drop(&#39;amount_borrowed&#39;, axis = 1, inplace = True)</pre><pre># Calculating VIF <br>vif = calc_vif(X)<br>vif[vif[&#39;VIF&#39;]&gt;10]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/243/1*2cY_m8mJjneRyEFCpW17Qw.png" /><figcaption>VIF Result</figcaption></figure><p>Now installment and principal_paid have VIF values greater than 10. installment will be removed as it has highest VIF value.</p><pre># Dropping installment <br>X.drop(&#39;installment&#39;, axis = 1, inplace= True)</pre><pre># Calculating VIF<br>vif = calc_vif(X)<br>vif[vif[&#39;VIF&#39;]&gt;10]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/157/1*_GMK4e-WtimJFOQTl1U1kQ.png" /><figcaption>VIF Result</figcaption></figure><p>Now no independent variables of the dataframe have VIF greater than 10.</p><h4>Model Building</h4><pre># Removing amount_borrowed, installment from df<br>df.drop([&#39;installment&#39;, &#39;amount_borrowed&#39;], axis = 1, inplace= True)</pre><p><strong>Splitting the Dataset</strong></p><pre># Importing train_test_split library<br>from sklearn.model_selection import train_test_split</pre><pre># Independent Variables<br>X = df.drop(&#39;loan_status_description&#39;, axis = 1)</pre><pre># Target Variable<br>y = df[&#39;loan_status_description&#39;]</pre><pre># Train-Test Split<br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=101)</pre><p><strong>Neural Network Model</strong></p><p>Step 1: Normalising the dataset<br>This step will help in changing all numeric values to a common scale. This is only required when features have different ranges.</p><pre># Normalising Dataset</pre><pre># Importing MinMaxScaler<br>from sklearn.preprocessing import MinMaxScaler</pre><pre>scaler = MinMaxScaler()<br>X_train = scaler.fit_transform(X_train)<br>X_test = scaler.transform(X_test)</pre><p>Step 2: Building a Neural Network Model</p><pre># Importing Tensorflow <br>import tensorflow as tf<br>from tensorflow.keras.models import Sequential<br>from tensorflow.keras.layers import Dense,Dropout<br>from tensorflow.keras.callbacks import EarlyStopping</pre><pre># Building Neural Network Model</pre><pre>model = Sequential()</pre><pre># Adding Input Layer<br>model.add(Dense(29, activation=&#39;relu&#39;))<br>model.add(Dropout(0.2)) # This will help in regularization</pre><pre># Hidden Layer<br>model.add(Dense(14, activation=&#39;relu&#39;))<br>model.add(Dropout(0.2))</pre><pre># Hidden Layer<br>model.add(Dense(7, activation=&#39;relu&#39;))<br>model.add(Dropout(0.2))</pre><pre># Output Layer<br>model.add(Dense(3, activation=&#39;softmax&#39;))</pre><pre># Compile Model<br>model.compile(loss=&#39;categorical_crossentropy&#39;, optimizer=&#39;adam&#39;)</pre><pre># Creating dummies of target variable for model fitting<br>y_train_model = pd.get_dummies(y_train)<br>y_test_model = pd.get_dummies(y_test)</pre><p>TensorFlow is an open source library to build to train and infer Deep Neural Networks. TensorFlow allows to create large-scale Neural Networks with multiple layers which can be used for classification, prediction, inference, etc.</p><p>Here, Sequential Model is built, which allows to build model layer by layer. Input layer has 29 Neurons and has activation function ‘Rectified Linear Unit’. The model has two hidden layers with 14 and 7 layers respectively with activation function ‘Rectified Linear Unit’. The output layer has 3 neurons, as there are 3 classes (Completed, Charged Off and Defaulted) and has activation function ‘softmax’.</p><p>Dropout is a regularization technique used to prevent overfitting of Neural Network Model. Dropout technique turns of percentage of randomly selected neurons of a particular layer, i.e. there won’t be any influence of dropped neurons on the next layer, weights and biases. General value of dropout layer is between 0.2 and 0.5.</p><p>While compiling the model, loss parameter is set to ‘categorical_crossentropy’ and optimizer is ‘adam’.</p><p>Step 3: Fitting the model</p><pre># Fitting Model on Training Dataset<br>model.fit(x=X_train, y=y_train_model, epochs=50, validation_data=(X_test, y_test_model), batch_size = 256)</pre><p>Epoch is number of times our model will go over our training data. We can also pass validation set/ test set, while fitting our model, to mesure the test error as well. The model will not be fit on this data.</p><p>Batch size is the number of samples processed before any updates in the model.</p><p>Step 4: Loss Comparison Graph</p><pre># Loss Comparison between training and test<br>losses = pd.DataFrame(model.history.history)<br>losses.plot()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*s-wpaV2EvUc5nlliDe74jQ.png" /><figcaption>Training Loss vs Test Loss Graph</figcaption></figure><p>Step 5: Measure of Accuracy</p><pre># Predictions<br>predictions = pd.DataFrame(model.predict_classes(X_test))</pre><pre># Confusion Matrix<br>print(confusion_matrix(y_test, predictions))</pre><pre># Classification Matrix<br>print(classification_report(y_test, predictions))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/201/1*qEhoIWggYDwajdn7E5Bkyw.png" /><figcaption>Confusion Matrix of Neural Network Model</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/447/1*ShA8NsGyYyfGQM5VWP0uXg.png" /><figcaption>Classification Report of Neural Network Model</figcaption></figure><p><strong>Random Forest Classifier</strong></p><p>We have already split and normalised the dataset into training, hence we will use that to train our RFC Model.</p><p>Step 1: Building RFC Model</p><pre># Importing RFC<br>from sklearn.ensemble import RandomForestClassifier</pre><pre># Building RFC Model<br>rfc = RandomForestClassifier(n_estimators=100, max_depth = None)<br>rfc.fit(X_train, y_train)</pre><p>Here, n_estimators is number of trees to be built before taking the maximum voting or averages of predictions. As n_estimators increases, model’s performance also increases.<br>max_depth is the maximum depth of the tree. Default value of max_depth is None, i.e. the nodes are expanded until leaves are pure. We can set the longest path between root and leaf node by using this parameter.</p><p>To find the optimum values of these hyperparameters techniques like Grid Search, Random Search can be used.</p><p>Step 2: Measure of Accuracy</p><pre># Predictions<br>rfc_pred = rfc.predict(X_test)</pre><pre># Confusion Matrix<br>print(confusion_matrix(y_test,rfc_pred))</pre><pre># Classification Matrix<br>print(classification_report(y_test,rfc_pred))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/207/1*hJA_35LC31b1t8XGfrSk3Q.png" /><figcaption>Confusion Matrix of Random Forest Classifier</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*N5xcadzyf4fiBkxtPYvKNA.png" /><figcaption>Classification Report of Random Forest Classifier</figcaption></figure><h4>Conclusion</h4><p>Classification Report gives insights about how good our predictions are. Now, we have to check the values of Recall-Precision values. Precision is the number of True Positives among all the predicted Positive instances, i.e. the number of people model correctly identified would be able to repay the loan out of all the people actually paying back the loan.</p><p>Recall is the number of True Positive among all the actual Positive instances, i.e. the number of people who actually repaid the loan were correctly identified.</p><p>Both Neural Network Model and Random Forest Classifier have accuracy of 99%. We can see a slight difference between Recall of Neural Network Model and Random Forest Classifier. Our agenda is to predict if a customer can pay back the loan, hence we will choose the model on the basis of Recall value. As the difference between Recall values of both the models is small we can choose any of the model.</p><h4>References</h4><ol><li><a href="https://deepai.org/">https://deepai.org/</a></li><li><a href="https://www.investopedia.com/">https://www.investopedia.com/</a></li><li><a href="https://www.analyticsvidhya.com/blog/2018/10/introduction-neural-networks-deep-learning/">https://www.analyticsvidhya.com/blog/2018/10/introduction-neural-networks-deep-learning/</a></li><li><a href="https://machinelearningmastery.com/random-forest-ensemble-in-python/">https://machinelearningmastery.com/random-forest-ensemble-in-python/</a></li><li><a href="https://www.exastax.com/deep-learning/top-five-use-cases-of-tensorflow/">https://www.exastax.com/deep-learning/top-five-use-cases-of-tensorflow/</a></li><li><a href="https://en.wikipedia.org/wiki/TensorFlow">https://en.wikipedia.org/wiki/TensorFlow</a></li><li><a href="https://www.analyticsvidhya.com/blog/2020/09/precision-recall-machine-learning/">https://www.analyticsvidhya.com/blog/2020/09/precision-recall-machine-learning/</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2065d1b4fa4d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>