<jupyter_start><jupyter_text>*Now, we have to study the relationship between the status of loan and the person's credit history. We will explore to find out how a person's credit history plays a part in getting his/her loan approved if at all it acts as an important factor!*<jupyter_code>print('Credit History')
print(train.Credit_History.value_counts())
print('Loan Status')
print(train.Loan_Status.value_counts())<jupyter_output>Credit History
1.0    475
0.0     89
Name: Credit_History, dtype: int64
Loan Status
Y    422
N    192
Name: Loan_Status, dtype: int64<jupyter_text>*The following table shows the percentage (/100) of Approved Loans segregated on the basis of credit history. This can also be considered as a probability of getting the loan aproved.*<jupyter_code>train.pivot_table(values='Loan_Status',index=['Credit_History'],aggfunc=lambda x: x.map({'Y':1,'N':0}).mean())
var1=pd.crosstab(index=train['Credit_History'],columns=train['Loan_Status'])
print(var1)
var1.plot(kind='bar',stacked='true',color=['orange','green'],grid=False)
var2=pd.crosstab(index=[train['Credit_History'],train['Gender']],columns=train['Loan_Status'])
var2.plot(kind='bar',stacked='true',color=['orange','green'],grid=False)<jupyter_output><empty_output><jupyter_text>Now, we have to handle the missing values.<jupyter_code>train.apply(lambda x: sum(x.isnull()),axis=0)
train.Gender.fillna(train['Gender'].mode()[0],inplace=True)
train.Married.fillna(train['Married'].mode()[0],inplace=True)
train.Dependents.fillna(train['Dependents'].mode()[0],inplace=True)
train.Self_Employed.fillna(train['Self_Employed'].mode()[0],inplace=True)
train.Credit_History.fillna(train['Credit_History'].mode()[0],inplace=True)
train[train['Loan_Amount_Term']==360].count()
train.Loan_Amount_Term.fillna(train['Loan_Amount_Term'].mode()[0],inplace=True)<jupyter_output><empty_output><jupyter_text>Filling of missing Loan Amount values require some extra effort that just filling the most frequent values.<jupyter_code>la_table=train.pivot_table(values='LoanAmount', index='Self_Employed' ,columns='Education', aggfunc=np.median)
print(la_table)
def la_fill(f):
    return la_table.loc[f['Self_Employed'],f['Education']]

train['LoanAmount'].fillna(train[train['LoanAmount'].isnull()].apply(la_fill, axis=1), inplace=True)
train.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 614 entries, 0 to 613
Data columns (total 13 columns):
Loan_ID              614 non-null object
Gender               614 non-null object
Married              614 non-null object
Dependents           614 non-null object
Education            614 non-null object
Self_Employed        614 non-null object
ApplicantIncome      614 non-null int64
CoapplicantIncome    614 non-null float64
LoanAmount           614 non-null float64
Loan_Amount_Term     614 non-null float64
Credit_History       614 non-null float64
Property_Area        614 non-null object
Loan_Status          614 non-null object
dtypes: float64(4), int64(1), object(8)
memory usage: 62.4+ KB<jupyter_text>In the next part, we will be focusing on 2 things,1. Reducing the skewness of some features2. Sincle the applicant's income is supported by the coapplicant's income for loan approval, we will calculate total income as a sum of both.<jupyter_code>plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
train['LoanAmount'].hist(bins=20,edgecolor='black')
plt.subplot(1,2,2)
train['ApplicantIncome'].hist(bins=20,edgecolor='black')
train['TotalIncome']=train['ApplicantIncome']+train['CoapplicantIncome']
train['TotalIncome_log']=np.log(train['TotalIncome'])
train['LoanAmount_log']=np.log(train['LoanAmount'])
plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
train['LoanAmount_log'].hist(bins=20,edgecolor='black')
plt.subplot(1,2,2)
train['TotalIncome_log'].hist(bins=20,edgecolor='black')<jupyter_output><empty_output><jupyter_text>Since, now we have all non-null values, some of those values are non numeric. In order to use them as a feature in training our machine learning models, we have to convert them into some sort of numeric data. This can be done with the help of LabelEncoder.<jupyter_code>from sklearn.preprocessing import LabelEncoder
# will create a list of column names with non numeric data
col_names=['Gender','Married','Dependents','Education','Self_Employed','Property_Area','Loan_Status']
le=LabelEncoder()
for name in col_names:
    train[name]=le.fit_transform(train[name])
train.dtypes
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn import metrics<jupyter_output><empty_output><jupyter_text>We will create a common funclion in which we can pass objects of various models to test the performances.<jupyter_code>def model_fun(model,df,features,target):
    model.fit(df[features],df[target])
    predictions=model.predict(df[features])
    accuracy=metrics.accuracy_score(predictions,df[target])
    print ("Accuracy : %s" % "{0:.3%}".format(accuracy))
    cv_score=cross_val_score(model,df[features],df[target],cv=5)
    print ("Cross validation score : %s" % "{0:.3%}".format(np.mean(cv_score)))
    model.fit(df[features],df[target])
#Logistic Regression
target_var='Loan_Status'
model=LogisticRegression()
features_var=['Credit_History']
model_fun(model,train,features_var,target_var)
features_var=['Gender', 'Married', 'Dependents', 'Education','Self_Employed','Loan_Amount_Term', 'Credit_History', 'Property_Area','TotalIncome_log', 'LoanAmount_log']
model_fun(model,train,features_var,target_var)<jupyter_output>Accuracy : 80.945%
Cross validation score : 80.957%
{'fraction_non_alphanumeric': 0.12979506043089858, 'fraction_numerical': 0.026624627780697144, 'mean_word_length': 7.311499272197962, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '435946', 'license': 'Apache-2.0', 'n_tokens_mistral': 1975, 'n_tokens_neox': 1837, 'n_words': 415, 'path': 'loan-approval-prediction.ipynb', 'repo_name': 'Sanyam8055/Data-Set-Analysis', 'size': '5709'}