<jupyter_start><jupyter_text>Loan Eligibility Prediction  Importing useful libraries<jupyter_code>import numpy as np
import pandas as pd
data = pd.read_csv("E:\\Datasets\\loan_data_set.csv") #loading dataset
data.head()
data.Loan_Status = data.Loan_Status.map({'Y':1,'N':0})
data.head()
data.shape
data.describe()
data.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    int64  
dtypes: float64(4), int64(2), object(7)
memory usage: 62.5+ KB<jupyter_text>Preprocessing<jupyter_code>data.isnull().sum()
data.Gender=data.Gender.map({"Male":1,"Female":0})
data.head()
data.Gender.value_counts()
data.Married=data.Married.map({"Yes":1,"No":0})

data.head()
data.Married.value_counts()
data.Dependents=data.Dependents.map({'0':0,'1':1,'2':2,'3+':3})
data.head()
data.Dependents.value_counts()
data.Education=data.Education.map({'Graduate':1,'Not Graduate':0})
data.head()
data.Education.value_counts()
data.Self_Employed=data.Self_Employed.map({'Yes':1,'No':0})
data.head()
data.Self_Employed.value_counts()
data.Property_Area=data.Property_Area.map({'Urban':2,'Rural':0,'Semiurban':1})
data.head()
data.Property_Area.value_counts()<jupyter_output><empty_output><jupyter_text>Filling Missing Values<jupyter_code>data.isnull().sum()
data.Credit_History.fillna(np.random.randint(0,2),inplace=True)
data.isnull().sum()
data.Married.fillna(np.random.randint(0,2),inplace=True)
data.isnull().sum()
data.LoanAmount.fillna(data.LoanAmount.median(),inplace=True)
data.isnull().sum()
data.Loan_Amount_Term.fillna(data.Loan_Amount_Term.mean(),inplace=True)
data.isnull().sum()
data.Gender.fillna(np.random.randint(0,2),inplace=True)
data.isnull().sum()
data.Dependents.fillna(data.Dependents.median(),inplace=True)
data.isnull().sum()
data.Self_Employed.fillna(np.random.randint(0,2),inplace=True)
data.isnull().sum()
data.head()
data.drop("Loan_ID",axis = 1, inplace = True)
data.to_csv("Cleaned_data.csv")<jupyter_output><empty_output><jupyter_text>Data Visualization<jupyter_code>from matplotlib import pyplot as plt
import seaborn as sns
plt.plot(data["ApplicantIncome"])
plt.plot(data["CoapplicantIncome"])
sns.set(style = "whitegrid")
sns.barplot(x= 'Loan_Status', y = 'LoanAmount', data = data)
sns.set(style = "whitegrid")
sns.barplot(x= 'Loan_Status', y = 'Loan_Amount_Term', data = data)
sns.set(style = "whitegrid")
sns.barplot(x= 'Loan_Status', y = 'Loan_Amount_Term', hue = 'Property_Area' ,data = data)
sns.set(style = "whitegrid")
sns.barplot(x= 'Loan_Status', y = 'Loan_Amount_Term', hue = 'Dependents' ,data = data)
sns.set(style = "whitegrid")
sns.barplot(x= 'Loan_Status', y = 'LoanAmount', hue = 'Married' ,data = data)
plt.plot(data["Loan_Status"].unique(),data["Credit_History"].unique())
plt.title("Credit History vs Loan Status")
plt.xlabel("Loan_Status")
plt.ylabel("Credit_History")
plt.show()<jupyter_output><empty_output><jupyter_text>MODELLING  Splitting data<jupyter_code>Input = data.drop('Loan_Status', axis =1)
Input.head()
Output = data["Loan_Status"]
Output.head()
from sklearn.model_selection import train_test_split
X_train,X_test,Y_train,Y_test = train_test_split(Input,Output,test_size = 0.2, random_state = 123)
X_train.shape
X_test.shape
Y_train.shape
Y_test.shape<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix,accuracy_score,classification_report
from sklearn.metrics import roc_curve,auc

LR = LogisticRegression()
LR.fit(X_train,Y_train)
model_prediction = LR.predict(X_test)
accuracy_score(Y_test,model_prediction)
print(classification_report(Y_test,model_prediction))
pred = pd.DataFrame({"Actual Value":Y_test,"Predicted Value":model_prediction})
pred.tail()
conf_matrix = confusion_matrix(Y_test,model_prediction)
conf_matrix
sns.heatmap(conf_matrix,annot = True,fmt = ".3g")
plt.ylabel("True Label")
plt.xlabel("Predicted Label")
plt.title("Confusion Matrix")
plt.plot()
fpr,tpr, threshold = roc_curve(Y_test,model_prediction)
auc_model = auc(fpr,tpr)
plt.plot(fpr,tpr,linestyle = "-")
plt.xlabel("False Positive")
plt.ylabel("True Positive")
plt.title("Roc Curve")
plt.show()
print(pred)
prediction = LR.predict(X_train)
print(prediction)
import pickle
pickle_out = open('LR.pkl','wb')
pickle.dump(LR,pickle_out)
pickle_out.close()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup