<jupyter_start><jupyter_text>Logistic Regression  On<jupyter_code>import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt 
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix
from sklearn import metrics
 loan = pd.read_csv("D:\Logistic Regression Dummy Data v3.csv")
loan.head()
loan.shape
loan.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 1006 entries, 0 to 1005
Data columns (total 14 columns):
 #   Column                 Non-Null Count  Dtype  
---  ------                 --------------  -----  
 0   Type of Account        1004 non-null   object 
 1   Account History        1004 non-null   object 
 2   Reason for the Loan    1003 non-null   object 
 3   Loan Amount            1003 non-null   float64
 4   Account Savings        1002 non-null   object 
 5   Employment History     1001 non-null   object 
 6   Individual Stauts      1001 non-null   object 
 7   Other Loans            1001 non-null   object 
 8   Security / Collateral  1001 non-null   object 
 9   Age                    1002 non-null   float64
 10  Residence Status       1002 non-null   object 
 11  Job                    1002 non-null   object 
 12  Completed Other loan?  1002 non-null   object 
 13  Good Loan              1001 non-null   object 
dtypes: float64(2), object(12)
memory usage: 110.2+ KB<jupyter_text>Object Type:   Handling Null Values<jupyter_code>loan.isnull().sum()
loan['Type of Account'].dropna(inplace=True)
loan.dropna(subset=['Type of Account'], how='all',inplace =True)
#dropping rows which have null values in account type
loan.isnull().sum()
loan.dropna(subset=['Loan Amount','Other Loans'], how='all',inplace =True)
#dropping rows which have null values in voth loan amount and other loans
loan.dropna(subset=['Good Loan'], how='all',inplace =True)
#dropping rows which have null values in good loan
loan['Good Loan'].value_counts()
loan.isnull().sum()
loan['Type of Account'].value_counts()
loan['Type of Account']=loan['Type of Account'].replace(['No Data'], 'Type D')
loan[loan['Age'] >100]
loan = loan[loan['Age'] <100]
loan.shape
#loan['Account Savings'].replace('<1',random.uniform(0.1,0.9))
display(loan['Reason for the Loan'].value_counts())
display(loan['Individual Stauts'].value_counts())
display(loan['Security / Collateral'].value_counts())
display(loan['Residence Status'].value_counts())
display(loan['Job'].value_counts())
loan.info()
loan['Individual Stauts'] = loan['Individual Stauts'].replace(['Female_'],'Female')
#replacing value'female_' with female
display(loan['Individual Stauts'].value_counts())
lst =  ['Other Loans','Completed Other loan?','Good Loan']

def binary_map(x):
    return x.map({'Yes': 1, "No": 0})
loan[lst] = loan[lst].apply(binary_map)
loan['Other Loans'] = pd.to_numeric(loan['Other Loans'])
loan['Completed Other loan?'] = pd.to_numeric(loan['Completed Other loan?'])
loan['Good Loan'] = pd.to_numeric(loan['Good Loan'])
loan.info()
loan.head()
display(loan['Employment History'].value_counts())
loan['Employment History']= loan['Employment History'].replace({"Unemployed": 0, "0-2 Years": 2,
                                                                "2-5 Years": 4,"5-7 Years": 6,
                                                                "7+ Years":8})
display(loan['Employment History'].value_counts())<jupyter_output><empty_output><jupyter_text>Statistics and Visualisation:<jupyter_code>loan.describe()
sns.countplot(x = 'Type of Account', data = loan, hue = 'Good Loan')
plt.show()<jupyter_output><empty_output><jupyter_text>Creating Dummy Variables for categorical data types:<jupyter_code>act= pd.get_dummies(loan['Type of Account'], prefix= 'Acc')
loan = pd.concat([loan,act], axis=1)

ach= pd.get_dummies(loan['Account History'], prefix= 'Acc_Hist')
loan = pd.concat([loan,ach], axis=1)

res = pd.get_dummies(loan['Reason for the Loan'], prefix='Reason')
loan = pd.concat([loan,res], axis=1)


acs= pd.get_dummies(loan['Account Savings'], prefix= 'Acc_Savings')
loan = pd.concat([loan,acs], axis=1)


indst = pd.get_dummies(loan['Individual Stauts'], prefix='Individual Status')
loan = pd.concat([loan,indst], axis=1)


sc = pd.get_dummies(loan['Security / Collateral'], prefix='Collateral')
loan = pd.concat([loan,sc], axis=1)

rs = pd.get_dummies(loan['Residence Status'], prefix='Residence')
loan = pd.concat([loan,rs], axis=1)

jb = pd.get_dummies(loan['Job'], prefix='Job')
loan = pd.concat([loan,jb], axis=1)
#pd.set_option("display.max_columns", None)
loan.head()
ln= loan.drop(['Type of Account','Account History','Reason for the Loan','Account Savings',
               'Individual Stauts','Security / Collateral','Residence Status','Job'],1)

ln.head()
ln.info()
cor= ln.corr()
#cor

plt.figure(figsize = (30,15))       
sns.heatmap(cor,annot = True)
plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Model Buliding:<jupyter_code>##
x= ln.drop(['Good Loan'], axis=1)
x.head()
y = loan['Good Loan']
y.head()
#train -test split:
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
"""
scaler = StandardScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)
"""

log_reg = LogisticRegression()
log_reg.fit(x_train, y_train)
print(log_reg.coef_)
print(log_reg.intercept_)
y_pred = log_reg.predict(x_test)
prob = log_reg.predict_proba(x_test)
prob = prob.reshape(-1,1)
conf=confusion_matrix(y_test, y_pred)
conf
print(metrics.accuracy_score(y_test, y_pred))
TP = conf[1,1] # true positive 
TN = conf[0,0] # true negatives
FP = conf[0,1] # false positives
FN = conf[1,0] # false negatives
#Precision:
Precision=TP/(TP+FP)
Precision
#Recall:
Recall = TP/(TP+FN)
Recall
#fpr, tpr, thresholds = metrics.roc_curve( y_pred, prob, drop_intermediate = False )

roc<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1471030222073802, 'fraction_numerical': 0.025045917515444983, 'mean_word_length': 4.43064369900272, 'pattern_counts': {'":': 6, '<': 23, '<?xml version=': 0, '>': 22, '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': '637564', 'license': 'Apache-2.0', 'n_tokens_mistral': 2206, 'n_tokens_neox': 1965, 'n_words': 486, 'path': 'GAN Model/Log_Reg_Dipesh_Team 4.ipynb', 'repo_name': 'MrTONYCHAN/xyz', 'size': '5989'}