# Python for LendingClub: Predictive Analytics

Is Python better for Machine Learning? How would you use python to analyze a major financial institution like LendingClub's dataset to train models to predict the interest rates of the loans? Case Analysis of Predictive Analytics.

**Background - Problem Definition**

LendingClub, a pioneer in the peer-to-peer lending sector, has revolutionized the lending landscape by enabling individuals and small businesses to secure loans directly from investors. LendingClub uses analytics to assess borrowers' creditworthiness, which plays a pivotal role in determining the interest rate on each loan. Investors, in turn, can browse the platform's diverse loan offerings and make investment decisions based on factors such as credit rating, loan term, and loan purpose.

The challenge that LendingClub faces, and the problem this analysis seeks to address, is the need to assign interest rates to loans in a manner that accurately reflects the applicants' risk level to the company. Using information from loan applications, this analysis aims to identify significant features that influence the value of a specific loan's interest rate.

The primary objective is to predict the interest rate of a loan using applicant information and credit metrics. The goals are twofold: to minimize the prediction error of interest rates and to maximize the prediction accuracy of interest rates.

To achieve this, I conducted an in-depth analysis of a modified version of the LendingClub dataset. I performed the analysis using Python in a Jupyter notebook environment, Colab, encompassing data exploration, visualization, and modeling. The project's main stages included data preprocessing, model training, model evaluation, and result presentation.

The dataset used in this project is a representative sample of loans from the LendingClub platform. The training dataset comprises information on a sample of 100,000 loans, while the test dataset contains a sample of 10,000 loans, with the interest rate information omitted. These datasets encompass various features related to the loans and borrowers, such as loan amount, borrower's annual income, and loan purpose, among others.

I trained two distinct machine learning models, namely, Support Vector Machine (SVM) and Random Forest, and compared their performance. The model demonstrating superior performance, Random Forest, was then utilized to predict the interest rates for the loans in the test dataset. This paper presents the methodology, results, and insights derived from the project.

**Methods & Diagnostics**

My endeavor to predict Lending Club interest rates commenced with data preprocessing, a task that proved to be more intricate and time-consuming than initially anticipated. This phase was integral to my analysis as it involved addressing missing data, outliers, and inconsistencies within the dataset. Furthermore, I transformed categorical variables into numerical ones using methods such as one-hot encoding, and standardized numerical variables to ensure uniformity across the dataset.

Before delving into the specifics of my preprocessing and feature selection, there were a number of assumptions that I made to proceed:

I used only data collected at loan origination to predict interest rates, assuming that the loan status is current and that data measuring current information was removed.

I assumed that all loans have a fixed interest rate for the term of the loan, with no variable or floating interest rates.

I presumed that no external factors influenced the interest rate, such as economic conditions or time of year.

I did not consider personal demographics (i.e., gender, race, age, etc.) as influencing the loan interest rate and these were not provided either.

In my initial dataset exploration, I identified numerous missing values and high correlation between certain features. To refine the dataset, I merged high and low FICO scores into one variable, removed non-contributing variables, and filled missing data with column means. 'Emp_length' was recoded into a binary variable, with '10+ years' as 1. I identified and removed 'int_rate' outliers using the IQR method and used scatter plots to visually inspect variable relationships. Lastly, I standardized the data to ensure uniformity, a crucial step for machine learning algorithms.

**Evaluation and Optimization of the Support Vector Regression Model **

I first used the Support Vector Regression (SVR) model, known for its accuracy and resilience. The initial model parameters were chosen to balance error minimization and complexity reduction to avoid overfitting.

After training the SVR model on the standardized data, I evaluated its performance using the Root Mean Squared Error (RMSE) and Mean Absolute Error (MAE) metrics. The RMSE was 4.189, indicating the average deviation of the predictions from the actual values, and the MAE was 3.130, providing a direct measure of average error.

I attempted to improve the model through hyperparameter tuning, but the best parameters only marginally improved the RMSE to 4.408 and the R-squared value to 0.293, at a significant computational cost.

Despite the reasonable performance of the SVR model in predicting Lending Club interest rates, the computational demands of hyperparameter tuning outweighed the minor improvements, leading me to abandon this approach. This experience highlighted the importance of considering computational resources when choosing machine learning algorithms and libraries.

**Evaluation and Optimization of the Random Forest Model**

Following the SVR, I utilized the Random Forest Regression model, known for handling complex datasets with numerous variables and outliers. After training on standardized data, the model's predictions were evaluated using RMSE and MAE metrics, yielding an RMSE of 4.102 and an MAE of 3.176.

Hyperparameter tuning improved the model slightly, with optimal 'n_estimators' and 'max_features' values of 623 and 5, respectively. This resulted in an RMSE of 4.374 and an MAE of 3.138, indicating reasonable predictive accuracy.

I considered both Grid Search and Randomized Search for hyperparameter tuning. Given computational constraints, Randomized Search proved more efficient, though it still took over an hour to complete.

In summary, the Random Forest model provided reasonable performance in predicting Lending Club interest rates, with slight improvements from hyperparameter tuning. Future work could explore different parameters or algorithms to enhance prediction accuracy, balancing computational efficiency and model performance.

**Insights **

The analysis of the LendingClub dataset yielded several key insights:

Dataset Size: Large datasets can increase computational demands and processing time, highlighting the need to balance dataset comprehensiveness with computational capacity.

Redundancy and Collinearity: Numerous independent variables can lead to redundancy and high collinearity, complicating model interpretation and potentially reducing predictive power. Careful feature selection is crucial.

Model Selection: Model performance can be significantly impacted by hyperparameter tuning and feature selection, emphasizing the need for iterative refinement.

External Factors: While we assumed external factors like market stability and economic conditions didn't influence interest rates, this assumption warrants further investigation.

These insights highlight the complexities of predictive modeling and the importance of data management, feature selection, model refinement, and consideration of external influences.

Contrary to my initial assumption, Python didn't outperform R in speed and efficiency for these models. The computational demand was higher and less efficient than expected. In R, initial model training and tuning took 30 minutes, while in Python, the SVR took 45 minutes initially and 2-3 hours after tuning. The Random Forest model took even longer, leading me to opt for Random Search over Grid Search to expedite computation in Python.

**Conclusion **

In this analysis, my goal was to predict LendingClub loan interest rates using applicant data and credit metrics. Two machine learning models, Support Vector Machine (SVM) and Random Forest, were put to the test on a representative sample of LendingClub loans. Despite the robustness and precision of the SVM model, the computational burden of hyperparameter tuning overshadowed the minor performance enhancements, leading to the selection of the Random Forest model.

The Random Forest model showed decent performance, with a slight uptick following hyperparameter tuning. However, the process underscored the intricacies of predictive modeling and the importance of meticulous data management, thoughtful feature selection, iterative model refinement, and the consideration of external influences.