<jupyter_start><jupyter_text><jupyter_code>import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set(style="white", color_codes=True)
path = '/content/train_loanprediction1.csv'
train = pd.read_csv(path)
train.head()
train.describe()
train.info()
train.shape
train.isnull().any()
train.isnull().sum()
train[['Gender']].info()
train.head(10)
train['Property_Area'].unique()
train['Property_Area'].value_counts()
train_loan = train.dropna()
train_loan.info()
train.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 614 entries, 0 to 613
Data columns (total 13 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   Loan_ID            614 non-null    object 
 1   Gender             601 non-null    object 
 2   Married            611 non-null    object 
 3   Dependents         599 non-null    object 
 4   Education          614 non-null    object 
 5   Self_Employed      582 non-null    object 
 6   ApplicantIncome    614 non-null    int64  
 7   CoapplicantIncome  614 non-null    float64
 8   LoanAmount         592 non-null    float64
 9   Loan_Amount_Term   600 non-null    float64
 10  Credit_History     564 non-null    float64
 11  Property_Area      614 non-null    object 
 12  Loan_Status        614 non-null    object 
dtypes: float64(4), int64(1), object(8)
memory usage: 62.5+ KB<jupyter_text>Data Preprocessing<jupyter_code>train['Dependents'].fillna(1,inplace=True)
train.info()
train['LoanAmount'].fillna(train.LoanAmount.mean(),inplace=True)
train.info()
train.head(10)
ValueMapping = {'Yes': 1, 'No': 0}
train['Married_Section'] = train['Married'].map(ValueMapping)
train.head()
ValueMapping1 = {'Male': 1, 'Female': 0}
train['Gender_Section'] = train['Gender'].map(ValueMapping1)
train.head()
train['Education'].unique()
ValueMapping2 = {'Graduate': 1, 'Not Graduate': 0}
train['Edu_Section'] = train['Education'].map(ValueMapping2)
train.head()
train.info()
train['Married_Section'].fillna(train.Married_Section.mean(), inplace=True)
train['Gender_Section'].fillna(train.Gender_Section.mean(), inplace=True)
train['Loan_Amount_Term'].fillna(train.Loan_Amount_Term.mean(), inplace=True)
train['Credit_History'].fillna(train.Credit_History.mean(), inplace=True)

train.info()
ValueMapping3 = {'Yes': 1, 'No': 0}
train['Employed_Section'] = train['Self_Employed'].map(ValueMapping3)

train.head()
train.info()
from sklearn.preprocessing import LabelEncoder

lb = LabelEncoder()

train['Property_Section'] = lb.fit_transform(train['Property_Area'])

train.head()
ValueMapping4 = {'Y':1, 'N':0}
train['Loan_Section'] = train['Loan_Status'].map(ValueMapping4)

train.head()
sns.FacetGrid(train,hue="Gender_Section",size=4) \
.map(plt.scatter,"Loan_Status","LoanAmount") \
.add_legend()
plt.show()
sns.FacetGrid(train,hue="Property_Section",size=4) \
.map(plt.scatter,"ApplicantIncome","CoapplicantIncome") \
.add_legend()
plt.show()
plt.figure(figsize = (10,7)) 
x = train["LoanAmount"] 
plt.hist(x, bins = 30, color = "pink") 
plt.title("Loan taken by Customers") 
plt.xlabel("Loan Figures") 
plt.ylabel("Count")
sns.boxplot(x="Property_Area", y="Gender_Section", data=train)
sns.boxplot(x="Married_Section", y="ApplicantIncome", data=train)
train_temp=train[train["Education"]== "Graduate"]
train_temp["Self_Employed"].hist()
sns.FacetGrid(train, hue="Credit_History", size=6).map(sns.kdeplot, "CoapplicantIncome").add_legend()
cols = ['ApplicantIncome','CoapplicantIncome','LoanAmount','Loan_Amount_Term','Credit_History','Married_Section',
        'Gender_Section','Edu_Section','Employed_Section','Property_Section']
f, ax = plt.subplots(figsize=(10, 7))
cm = np.corrcoef(train[cols].values.T)
sns.set(font_scale=1.5)
hm = sns.heatmap(cm,
                 cbar=True,
                 annot=True,
                 square=True,
                 fmt='.2f',
                 annot_kws={'size': 15},
                 yticklabels=cols,
                 xticklabels=cols)

plt.show()
train['Employed_Section'].unique()
train['Employed_Section'].fillna(1,inplace=True)
train.head()
train['Employed_Section'].unique()
train['Gender_Section'].unique()
train['Gender_Section'].fillna(1,inplace=True)
train.head()
train['Gender_Section'].unique()
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
X=train[['ApplicantIncome','CoapplicantIncome','LoanAmount','Loan_Amount_Term','Credit_History','Married_Section',
        'Gender_Section','Edu_Section','Employed_Section','Property_Section']].values
y=train[["Loan_Section"]].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
train.isna().any()
model.fit(X_train, y_train)
model.score(X_train,y_train)
model.score(X_test,y_test)
expected = y_test
predicted = model.predict(X_test)
from sklearn import metrics
print(metrics.classification_report(expected, predicted))
metrics.confusion_matrix(expected, predicted)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15245327102803738, 'fraction_numerical': 0.02511682242990654, 'mean_word_length': 4.645054945054945, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '146188', 'license': 'MIT', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1757, 'n_words': 296, 'path': 'Logistic_Regression.ipynb', 'repo_name': 'vdnew/Loan-Prediction', 'size': '5136'}