Skip to content

Instantly share code, notes, and snippets.

Last active Feb 21, 2021
What would you like to do?
import pickle
import streamlit as st
# loading the trained model
pickle_in = open('classifier.pkl', 'rb')
classifier = pickle.load(pickle_in)
# defining the function which will make the prediction using the data which the user inputs
def prediction(Gender, Married, ApplicantIncome, LoanAmount, Credit_History):
# Pre-processing user input
if Gender == "Male":
Gender = 0
Gender = 1
if Married == "Unmarried":
Married = 0
Married = 1
if Credit_History == "Unclear Debts":
Credit_History = 0
Credit_History = 1
LoanAmount = LoanAmount / 1000
# Making predictions
prediction = classifier.predict(
[[Gender, Married, ApplicantIncome, LoanAmount, Credit_History]])
if prediction == 0:
pred = 'Rejected'
pred = 'Approved'
return pred
# this is the main function in which we define our webpage
def main():
# front end elements of the web page
html_temp = """
<div style ="background-color:yellow;padding:13px">
<h1 style ="color:black;text-align:center;">Streamlit Loan Prediction ML App</h1>
# display the front end aspect
st.markdown(html_temp, unsafe_allow_html = True)
# following lines create boxes in which user can enter data required to make prediction
Gender = st.selectbox('Gender',("Male","Female"))
Married = st.selectbox('Marital Status',("Unmarried","Married"))
ApplicantIncome = st.number_input("Applicants monthly income")
LoanAmount = st.number_input("Total loan amount")
Credit_History = st.selectbox('Credit_History',("Unclear Debts","No Unclear Debts"))
result =""
# when 'Predict' is clicked, make the prediction and store it
if st.button("Predict"):
result = prediction(Gender, Married, ApplicantIncome, LoanAmount, Credit_History)
st.success('Your loan is {}'.format(result))
if __name__=='__main__':
X = train[['Gender', 'Married', 'ApplicantIncome', 'LoanAmount', 'Credit_History']]
y = train.Loan_Status
X.shape, y.shape
!pip install -q pyngrok
!pip install -q streamlit
!pip install -q streamlit_ace
import pandas as pd
train = pd.read_csv('train_ctrUa4K.csv')
train = train.dropna()
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(max_depth=4, random_state = 10), y_train)
# saving the model
import pickle
pickle_out = open("classifier.pkl", mode = "wb")
pickle.dump(model, pickle_out)
train['Gender']= train['Gender'].map({'Male':0, 'Female':1})
train['Married']= train['Married'].map({'No':0, 'Yes':1})
train['Loan_Status']= train['Loan_Status'].map({'N':0, 'Y':1})
from pyngrok import ngrok
public_url = ngrok.connect('8501')
!streamlit run &>/dev/null&
pred_train = model.predict(x_train)
from sklearn.model_selection import train_test_split
x_train, x_cv, y_train, y_cv = train_test_split(X,y, test_size = 0.2, random_state = 10)
from sklearn.metrics import accuracy_score
pred_cv = model.predict(x_cv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment