P1G5_deployment_wilson / prediction.py
weewoo2636's picture
add scaler before prediction
5d59558 verified
# import libraries
import streamlit as st
import numpy as np
import pandas as pd
import joblib
def app():
st.title('Prediction for Default Payment on the Next Month')
df = pd.read_csv('eda_data.csv')
st.subheader('Dataset Preview')
st.write(df)
st.subheader('Input Data')
input = user_input(df)
st.subheader('User Input')
st.write(input)
load_scaler = joblib.load('scaler.pkl')
input_scaled = load_scaler.transform(input)
st.subheader('Scaled User Input')
st.write(input_scaled)
load_model = joblib.load("model.pkl")
prediction = load_model.predict(input_scaled)
if prediction == 1:
prediction = 'Payment Defaulted (1)'
else:
prediction = 'Payment Not Defaulted (0)'
st.subheader('Prediction Result:')
st.write('Based on user input, the model predicted: ')
st.write(prediction)
def user_input(df):
limit_balance = st.number_input('limit_balance', value=0.0)
education_level = st.selectbox('education_level', df['education_level'].unique())
pay_1 = st.selectbox('pay_1', [i for i in range(-1, 13)])
pay_2 = st.selectbox('pay_2', [i for i in range(-1, 13)])
pay_3 = st.selectbox('pay_3', [i for i in range(-1, 13)])
pay_4 = st.selectbox('pay_4', [i for i in range(-1, 13)])
pay_5 = st.selectbox('pay_5', [i for i in range(-1, 13)])
pay_6 = st.selectbox('pay_6', [i for i in range(-1, 13)])
pay_amt_1 = st.number_input('pay_amt_1', value=0.0)
pay_amt_2 = st.number_input('pay_amt_2', value=0.0)
pay_amt_3 = st.number_input('pay_amt_3', value=0.0)
pay_amt_4 = st.number_input('pay_amt_4', value=0.0)
pay_amt_5 = st.number_input('pay_amt_5', value=0.0)
pay_amt_6 = st.number_input('pay_amt_6', value=0.0)
data = {
'limit_balance' : limit_balance,
'education_level' : education_level,
'pay_1' : pay_1,
'pay_2' : pay_2,
'pay_3': pay_3,
'pay_4' : pay_4,
'pay_5' : pay_5,
'pay_6' : pay_6,
'pay_amt_1' : pay_amt_1,
'pay_amt_2' : pay_amt_2,
'pay_amt_3' : pay_amt_3,
'pay_amt_4' : pay_amt_4,
'pay_amt_5' : pay_amt_5,
'pay_amt_6' : pay_amt_6
}
features = pd.DataFrame(data, index=[0])
return features