<jupyter_start><jupyter_text>Imports<jupyter_code>import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
import seaborn as sns 
import os 
import re 

from sklearn import preprocessing, metrics 
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier 
from sklearn.neighbors import KNeighborsClassifier 

from sklearn.metrics import accuracy_score 
from sklearn.metrics import average_precision_score 
from sklearn.metrics import precision_recall_curve<jupyter_output><empty_output><jupyter_text>**Data Analysis**We neesd to predict the probability of a loanee/borrower defaulting on a vehicle loan in the first EMI on the due date. Following information regarding the loan and loanee are provided in the datasets:1.   LOANEE INFORMATION : Demographic data like age, identity proof etc 2.   LOAN INFORMATION : Disbursal details, loan to value ratio etc 3. BUREAU DATA AND HISTORY : Bureau score, number of active accounts, the status of other loans, credit history etcDoing so will ensure that clients capable of repayment are not rejected and important determinants can be identified which can be further used for minimising the default rates.<jupyter_code>train_df = pd.read_csv('train.csv')<jupyter_output><empty_output><jupyter_text>**Loan Default class Distribution**<jupyter_code># Given Dataset 
ax = train_df['loan_default'].value_counts().plot(kind = 'bar', figsize = (10, 7), color = 'slateblue', fontsize = 13)
ax.set_alpha(0.8)
ax.set_xlabel('Class', color = 'gray', fontsize = 18 )
ax.set_ylabel('No of Obeservations', fontsize = 18, color = 'gray')
ax.set_title("Loan Default Class Distribution", fontsize = 18)

totals = []

for i in ax.patches:
  totals.append(i.get_height())
total = sum(totals)

print(totals)

# set individual bar lables using above list
for i in ax.patches:
    # get_x pulls left or right; get_height pushes up or down
    
    ax.text(i.get_x() + 0.1, i.get_height()- 13000, \
            str(round((i.get_height()/total)*100, 2))+'%', fontsize=22,
                color='white')
  
plt.show()
train_df['loan_default'] = train_df['loan_default'].astype('category')
train_df.head(5)
# No. of Vehicle Manufacturer 
len(train_df['manufacturer_id'].unique())
# Count of Loan defaults grouped by Employee Type
df = train_df.groupby(['Employment.Type', 'loan_default']).agg({'loan_default': 'count'})
df

train_df.groupby(['State_ID', 'loan_default']).agg({'loan_default': 'count'})
train_df.head(5)
train_df.columns 
len(train_df['PERFORM_CNS.SCORE.DESCRIPTION'].unique())
train_df['PERFORM_CNS.SCORE'].unique()
df = train_df[['PERFORM_CNS.SCORE', 'PERFORM_CNS.SCORE.DESCRIPTION', 'loan_default']]
df.head(50)
# Quantitative variables : [disbursed_amount, asset_cost, ltv, 
#                           Perform_CNS_Score, Perform_CNS_Score_DESC, 
#                            pri_NO_accts, active_acct, 
# 
# 
# 
# 
#                           NEW.ACCTS.IN.LAST.SIX.MONTHS, DELINQUENT.ACCTS.IN.LAST.SIX.MONTHS, NO.OF_INQUIRIES]

# Categoorical Variables : [branch_id, supplier_id, manufacturer_id, current_pincode, Employement.Type, 
#                           State_ID, MobileNo_AVL_Flag, Aadhar_Flag, PAN_flag, VoterID_flag, Driving_flag, Passport_flag,
#                            ]

# preprocess : [date.of.Birth, disbursalDate, CREDIT.HISTORY.LENGTH, AVERAGE.ACCT.AGE]
#
# Delete features : [Unique_ID, Employee_code_ID, ]
# preparing Quantitative variables


train_df.dtypes
train_df.iloc[0]
train_df.describe()
train_df.columns
drop_features = ['UniqueID', 'branch_id', 'supplier_id', 'manufacturer_id', 'Current_pincode_ID', 'Employee_code_ID',
                'MobileNo_Avl_Flag', 'Aadhar_flag', 'PAN_flag', 'VoterID_flag',
       'Driving_flag', 'Passport_flag',]

train_df = train_df.drop(drop_features, axis = 1)
train_df.shape
train_df.isnull().sum()
train_df.dropna(inplace = True)<jupyter_output><empty_output><jupyter_text>**Non Numneric data**<jupyter_code>train_df.dtypes.value_counts()
object_cols_df = train_df.select_dtypes(include = ['object'])
print(object_cols_df.iloc[0])
# Date.of.Birth
from datetime import datetime 
from datetime import date 

def calAge(dob):
  age =  pd.to_datetime('today').year - pd.to_datetime(dob).year
  if age < 0:
    age = age + 100 
  return age 
    

train_df['Age'] = train_df['Date.of.Birth'].apply(calAge)
train_df = train_df.drop('Date.of.Birth', axis =  1)
# Employement.Type
dummy_df = pd.get_dummies(train_df['Employment.Type'])
train_df = pd.concat([train_df, dummy_df], axis = 1)
train_df = train_df.drop('Employment.Type', axis = 1)
# PERFORM_CNS.SCORE.DESCRIPTION 
dummy_df = pd.get_dummies(train_df['PERFORM_CNS.SCORE.DESCRIPTION'])
train_df = pd.concat([train_df, dummy_df], axis = 1)
train_df = train_df.drop('PERFORM_CNS.SCORE.DESCRIPTION', axis = 1)
# AVERAGE.ACCT.AGE & CREDIT.HISTORY.LENGTH 
import re 

def preprocess(str1):
  no = re.findall('\d+', str1)
  mylist = [12, 1]
  res = 0 
  for i,j in zip(no, mylist):
    i = int(i)
    res = res + (i*j)
  return res 

train_df['AVG_ACCT_AGE_Mon'] = train_df['AVERAGE.ACCT.AGE'].apply(preprocess)
train_df['CREDIT.HISTORY.LENGTH_Mon'] = train_df['CREDIT.HISTORY.LENGTH'].apply(preprocess)

train_df = train_df.drop(['AVERAGE.ACCT.AGE', 'CREDIT.HISTORY.LENGTH'], axis = 1)
train_df = train_df.drop('DisbursalDate', axis = 1)
train_df.info()
train_df.head(10)<jupyter_output><empty_output><jupyter_text>Modelling<jupyter_code># Logistic regression classifier 

from sklearn.linear_model import LogisticRegression

lr = LogisticRegression()
cols = train_df.columns 
train_cols = cols.drop('loan_default')
features = train_df[train_cols]
target = train_df['loan_default']

lr.fit(features, target)
predictions = lr.predict(features)
from sklearn.model_selection import 
lr = LogisticRegression()
kf = KFold(features.shape[0], random_state=42)
predictions = cross_val_predict(lr, features, target, cv=kf)
predictions = pd.Series(predictions)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12110091743119267, 'fraction_numerical': 0.009674728940783986, 'mean_word_length': 5.056565656565657, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '595775', 'license': 'MIT', 'n_tokens_mistral': 2219, 'n_tokens_neox': 2119, 'n_words': 453, 'path': 'LTFS Data Science FinHack/LTFS_Data_Science_FinHack.ipynb', 'repo_name': 'leomcp/AV_Black_Friday', 'size': '5995'}