<jupyter_start><jupyter_text>DecisionTree and RandomForest using LendingClub Data setThis project handles a LendingClub set and contains some features of people(borrowers) and whether the borrower paid back the money. Based on this data using a DecisionTree model and a RandomForest model we predict whether the person will pay back the money. The columns represent following featuresHere are what the columns represent:* credit.policy: 1 if the customer meets the credit underwriting criteria of LendingClub.com, and 0 otherwise.* purpose: The purpose of the loan (takes values "credit_card", "debt_consolidation", "educational", "major_purchase", "small_business", and "all_other").* int.rate: The interest rate of the loan, as a proportion (a rate of 11% would be stored as 0.11). Borrowers judged by LendingClub.com to be more risky are assigned higher interest rates.* installment: The monthly installments owed by the borrower if the loan is funded.* log.annual.inc: The natural log of the self-reported annual income of the borrower.* dti: The debt-to-income ratio of the borrower (amount of debt divided by annual income).* fico: The FICO credit score of the borrower.* days.with.cr.line: The number of days the borrower has had a credit line.* revol.bal: The borrower's revolving balance (amount unpaid at the end of the credit card billing cycle).* revol.util: The borrower's revolving line utilization rate (the amount of the credit line used relative to total credit available).* inq.last.6mths: The borrower's number of inquiries by creditors in the last 6 months.* delinq.2yrs: The number of times the borrower had been 30+ days past due on a payment in the past 2 years.* pub.rec: The borrower's number of derogatory public records (bankruptcy filings, tax liens, or judgments).<jupyter_code>#initially import essential libraries for data exploration
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set_style("whitegrid") #sets the background style for seaborn plots
#loading the data from csv file
loans = pd.read_csv('loan_data.csv')
#check the head(first few rows) of the data
loans.head()
#use info to get a good understanding of the data like number of columns
# rows and the data types in each columns
loans.info()
#use describe method to get analytical information like mean, standard deviation, minimum, maximum etc. of each column
loans.describe()<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis<jupyter_code>#check a histogram distribution of FICO score with different credit policy
plt.figure(figsize=(10,6))
loans[loans['credit.policy']==1]['fico'].hist(bins=30,alpha=0.5,label='Credit.policy=1')
loans[loans['credit.policy']==0]['fico'].hist(bins=30,alpha=0.5,label='Credit.policy=0')
plt.legend()
plt.xlabel('FICO')
#check a similar relation but with the target column - not fully paid
plt.figure(figsize=(10,6))
loans[loans['not.fully.paid']==1]['fico'].hist(bins=30,alpha=0.5,label='not.fully.paid=1')
loans[loans['not.fully.paid']==0]['fico'].hist(bins=30,alpha=0.5,label='not.fully.paid=0')
plt.legend()
plt.xlabel('FICO')
#check relation between the purpose of loan and loan repayment
plt.figure(figsize=(12,8))
sns.countplot(x='purpose',hue='not.fully.paid',data=loans)
#check the trend between FICO score and interest rate
sns.jointplot(x='fico', y='int.rate', data=loans)
#seem to be having a negative correlation
#create an lm plot to see trend between not fully paid and credit poicy
#in the context of plot above
sns.lmplot(x='fico',y='int.rate',data=loans,
           hue='credit.policy',col='not.fully.paid',
           palette='Set1')<jupyter_output><empty_output><jupyter_text>Setting up the data<jupyter_code>loans.info()
#purpose is a non neumerical data but it is important so cannot be droped
#so replace this categorical data into neumerical by using dummies
mod_loans=pd.get_dummies(loans,columns=['purpose'],drop_first=True) 
mod_loans.head()<jupyter_output><empty_output><jupyter_text>Training a DecisionTree model<jupyter_code>#initially import essential libraries for logistic regression and analysis
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report, confusion_matrix
x = mod_loans.drop('not.fully.paid',axis=1) #training input features
y = mod_loans['not.fully.paid'] #target labels
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.30)
dtree = DecisionTreeClassifier() #create an instance of the decision tree model
dtree.fit(X_train,y_train)       #fir the model with input training features and the target labels
#check how good the model is by predicting on the test set
pred = dtree.predict(X_test)
#evaluating the predictions using classification report
print(classification_report(y_test,pred))
#evaluating the predictions using confusion_matrix
print(confusion_matrix(y_test,pred))<jupyter_output>[[1985  439]
 [ 315  135]]<jupyter_text>Training a DecisionTree model<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(n_estimators=100)
rfc.fit(X_train, y_train)
pred =rfc.predict(X_test)
#we can observe an improved performance with random forest classifier
print(confusion_matrix(y_test,pred))
print(classification_report(y_test,pred))<jupyter_output>[[2405   19]
 [ 440   10]]
              precision    recall  f1-score   support

           0       0.85      0.99      0.91      2424
           1       0.34      0.02      0.04       450

    accuracy                           0.84      2874
   macro avg       0.60      0.51      0.48      2874
weighted avg       0.77      0.84      0.78      2874
{'fraction_non_alphanumeric': 0.09001922740779585, 'fraction_numerical': 0.024296451669288587, 'mean_word_length': 5.061440677966102, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '220231', 'license': 'MIT', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1697, 'n_words': 618, 'path': 'MachineLearning/03-DecisionTrees&RandomForest/DecisionTrees&RandomForest-LendingClub Data.ipynb', 'repo_name': 'anish-pratheepkumar/AI-Machine-Learning-and-Deep-Learning', 'size': '5721'}