ML Project – Loan Approval Classifier using Random Forest

Machine Learning courses with 100+ Real-time projects Start Now!!

Program 1

Loan Dataset

Loan Approval Classifier using Random Forest


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

df_loan=pd.read_csv("D://scikit_data/loanData/loan_data.csv")

df_loan.head()

df_loan.shape

df_loan.info()

df_loan.isnull().sum()

# Label Encoding
le=LabelEncoder()
df_loan['Gender']=le.fit_transform(df_loan['Gender'])
df_loan['Married']=le.fit_transform(df_loan['Married'])
df_loan['Education']=le.fit_transform(df_loan['Education'])
df_loan['Loan_Status']=le.fit_transform(df_loan['Loan_Status'])

df_loan.info()

df_loan.head()

df_loan=df_loan.drop(['Unnamed: 6'],axis='columns')

df_loan

X = df_loan.drop("Loan_Status", axis=1) # Independed variables
y = df_loan["Loan_Status"] # Depended variables

X

y

# Split data into train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

len(X_train)

len(X_test)

# Train Random Forest model
model = RandomForestClassifier(n_estimators=100, random_state=42)

model

model.fit(X_train,y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate
print("Accuracy:", accuracy_score(y_test, y_pred))

df_loan

model.predict([[1,1,0,5000,200]])

model.predict([[0,0,1,3200,115]])

gen=int(input("Enter Gender(Male-1 ,Female-0) :"))
mar=int(input("Enter Married Status (1-Yes 0-No) :"))
edu=int(input("Enter Your Education:(Graduate-0,Not Graduate 1) :"))
tc=int(input("Enter ApplicantIncome:"))
amt=float(input("Enter LoanAmount :"))

result=model.predict([[gen,mar,edu,tc,amt]])
if(result==1):
    print("********Loan Approved*******")
else:
    print(".......Loan is Cancel .....")

 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *