24/04/2025, 09:39 Untitled22.
ipynb - Colab
import zipfile
import os
# Define the path to the uploaded zip file
zip_path = "/content/archive (5).zip"
extract_path = "/mnt/data/loan_data_extracted"
# Extract the contents of the zip file
with [Link](zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_path)
# List extracted files
extracted_files = [Link](extract_path)
extracted_files
['test_Y3wMUE5_7gLdaTN.csv', 'train_u6lujuX_CVtuZ9i.csv']
# Load the specific dataset CSV file
file_path = "/mnt/data/loan_data_extracted/train_u6lujuX_CVtuZ9i.csv"
df_loan = pd.read_csv(file_path)
# Display the first few rows of the dataset
df_loan.head(), df_loan.info()
<class '[Link]'>
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
( Loan_ID Gender Married Dependents Education Self_Employed \
0 LP001002 Male No 0 Graduate No
1 LP001003 Male Yes 1 Graduate No
2 LP001005 Male Yes 0 Graduate Yes
3 LP001006 Male Yes 0 Not Graduate No
4 LP001008 Male No 0 Graduate No
ApplicantIncome CoapplicantIncome LoanAmount Loan_Amount_Term \
0 5849 0.0 NaN 360.0
1 4583 1508.0 128.0 360.0
2 3000 0.0 66.0 360.0
3 2583 2358.0 120.0 360.0
4 6000 0.0 141.0 360.0
Credit_History Property_Area Loan_Status
0 1.0 Urban Y
1 1.0 Rural N
2 1.0 Urban Y
3 1.0 Urban Y
4 1.0 Urban Y ,
None)
import pandas as pd
import numpy as np
import seaborn as sns
import [Link] as plt
# Load the dataset
file_path = "/mnt/data/loan_data_extracted/train_u6lujuX_CVtuZ9i.csv" # Replace with the correct file path
df = pd.read_csv(file_path)
# Display first few rows and info about the dataset
print([Link]())
[Link] 1/6
24/04/2025, 09:39 [Link] - Colab
print([Link]())
Loan_ID Gender Married Dependents Education Self_Employed \
0 LP001002 Male No 0 Graduate No
1 LP001003 Male Yes 1 Graduate No
2 LP001005 Male Yes 0 Graduate Yes
3 LP001006 Male Yes 0 Not Graduate No
4 LP001008 Male No 0 Graduate No
ApplicantIncome CoapplicantIncome LoanAmount Loan_Amount_Term \
0 5849 0.0 NaN 360.0
1 4583 1508.0 128.0 360.0
2 3000 0.0 66.0 360.0
3 2583 2358.0 120.0 360.0
4 6000 0.0 141.0 360.0
Credit_History Property_Area Loan_Status
0 1.0 Urban Y
1 1.0 Rural N
2 1.0 Urban Y
3 1.0 Urban Y
4 1.0 Urban Y
<class '[Link]'>
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
None
# 1. Handle missing values
# Check missing values
print("\nMissing Values:\n", [Link]().sum())
Missing Values:
Loan_ID 0
Gender 13
Married 3
Dependents 15
Education 0
Self_Employed 32
ApplicantIncome 0
CoapplicantIncome 0
LoanAmount 22
Loan_Amount_Term 14
Credit_History 50
Property_Area 0
Loan_Status 0
dtype: int64
# 1. Fill missing Gender with the mode (most frequent value)
df['Gender'] = df['Gender'].fillna(df['Gender'].mode()[0])
# 2. Fill missing Married with the mode (most frequent value)
df['Married'] = df['Married'].fillna(df['Married'].mode()[0])
# 3. Fill missing Dependents with 0 (assuming no dependents)
df['Dependents'] = df['Dependents'].fillna('0')
# 4. Fill missing Self_Employed with 'No'
df['Self_Employed'] = df['Self_Employed'].fillna('no')
# 5. Fill missing LoanAmount with the median value
df['LoanAmount'] = df['LoanAmount'].fillna(df['LoanAmount'].median())
[Link] 2/6
24/04/2025, 09:39 [Link] - Colab
# 6. Fill missing Loan_Amount_Term with the mode (most frequent value)
df['Loan_Amount_Term'] = df['Loan_Amount_Term'].fillna(df['Loan_Amount_Term'].mode()[0])
# 7. Fill missing Credit_History with 1 (assuming good credit history)
df['Credit_History'] = df['Credit_History'].fillna(1)
# Check again for any missing values
print("\nMissing Values After Imputation:\n", [Link]().sum())
Missing Values After Imputation:
Loan_ID 0
Gender 0
Married 0
Dependents 0
Education 0
Self_Employed 0
ApplicantIncome 0
CoapplicantIncome 0
LoanAmount 0
Loan_Amount_Term 0
Credit_History 0
Property_Area 0
Loan_Status 0
TotalIncome 0
dtype: int64
# Let's categorize total income into bins for better analysis
bins = [0, 2500, 4000, 6000, 81000] # Custom bins for income categories
labels = ['Low', 'Medium', 'High', 'Very High']
df['IncomeCategory'] = [Link](df['TotalIncome'], bins=bins, labels=labels)
# Apply to all relevant columns safely
df['Gender'] = df['Gender'].astype(str).[Link]().[Link]()
df['Married'] = df['Married'].astype(str).[Link]().[Link]()
df['Education'] = df['Education'].astype(str).[Link]().[Link]()
df['Self_Employed'] = df['Self_Employed'].astype(str).[Link]().[Link]()
df['Property_Area'] = df['Property_Area'].astype(str).[Link]().[Link]()
df['Dependents'] = df['Dependents'].astype(str).[Link]()
# 2. Convert '3+' to numeric in the 'Dependents' column
df['Dependents'] = df['Dependents'].replace('3+', 3).astype(float)
# Import the StandardScaler class from sklearn for feature scaling
from [Link] import StandardScaler
# Create an instance of the StandardScaler
scaler = StandardScaler()
# Define the list of numerical columns we want to scale
scaled_cols = ['LoanAmount', 'ApplicantIncome', 'CoapplicantIncome', 'TotalIncome']
# Fit the scaler to the selected columns and transform the data
# This will standardize the values in each column to have mean 0 and standard deviation 1
df[scaled_cols] = scaler.fit_transform(df[scaled_cols])
# 3. Feature Engineering
df['TotalIncome'] = df['ApplicantIncome'] + df['CoapplicantIncome']
# 4. Data Visualization
# Distribution of Loan Amounts
[Link](figsize=(10,6))
[Link](df['LoanAmount'], kde=True, bins=20)
[Link]('Distribution of Loan Amounts')
[Link]()
[Link] 3/6
24/04/2025, 09:39 [Link] - Colab
# Correlation Heatmap to analyze numeric features
[Link](figsize=(8,6))
[Link](df[['ApplicantIncome', 'CoapplicantIncome', 'LoanAmount', 'TotalIncome']].corr(), annot=True, cmap='coolw
[Link]('Correlation Heatmap')
[Link]()
import seaborn as sns
import [Link] as plt
# Plotting histograms for numerical features
numerical cols = ['ApplicantIncome' 'CoapplicantIncome' 'LoanAmount' 'Loan Amount Term' 'TotalIncome']
[Link] 4/6
24/04/2025, 09:39 [Link] - Colab
numerical_cols = [ ApplicantIncome , CoapplicantIncome , LoanAmount , Loan_Amount_Term , TotalIncome ]
df[numerical_cols].hist(figsize=(10, 8), bins=20, edgecolor='black')
# Adding labels to the axes
[Link]('Value') # X-axis label
[Link]('Frequency') # Y-axis label
# Show the plot
plt.tight_layout()
[Link]()
import seaborn as sns
import [Link] as plt
# Create a count plot for Loan_Status
[Link](figsize=(6, 4))
[Link](data=df, x='Loan_Status', palette='Set2')
[Link]('Distribution of Loan Status', fontsize=14)
[Link]('Loan Status', fontsize=12)
[Link]('Count', fontsize=12)
[Link]()
[Link] 5/6
24/04/2025, 09:39 [Link] - Colab
<ipython-input-20-6dda5b1e4e87>:6: FutureWarning:
Passing `palette` without assigning `hue` is deprecated and will be removed in v0.14.0. Assign the `x` variable t
[Link](data=df, x='Loan_Status', palette='Set2')
[Link] 6/6