P1G5_Set_1_devi_nirfana / prediction.py
dnirfana's picture
update model path
318466b verified
# Contents of `app2.py`
import streamlit as st
import pandas as pd
import joblib
import json
def app():
st.title('Make Predictions')
# load the model
with open('model.pkl', 'rb') as file_1:
model = joblib.load(file_1)
with open('column.txt', 'r') as file_2:
selected_columns = json.load(file_2)
with open('scaler.pkl', 'rb') as file_3:
scaler = joblib.load(file_3)
st.title("Data Input Options")
option = st.selectbox(
"How would you like to input the data?",
("Upload a CSV file", "Manually input data")
)
if option == "Upload a CSV file":
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
st.write("Data Preview:")
st.write(df)
# Select only the columns for modeling
data_inference_num = df[selected_columns]
# Scaling the data
data_inference_scaled = scaler.transform(data_inference_num)
# Create new dataframe
data_inference_final = pd.DataFrame(data_inference_scaled, columns=selected_columns)
# Predict the defaulter
predicted_defaulter = model.predict(data_inference_final)
# Show result
st.write("Predicted Defaulter Status:")
st.write(predicted_defaulter)
elif option == "Manually input data":
st.write("Enter your data:")
# Define empty lists for each attribute
limit_balance = []
sex = []
education_level = []
marital_status = []
age = []
pay_0 = []
pay_2 = []
pay_3 = []
pay_4 = []
pay_5 = []
pay_6 = []
bill_amt_1 = []
bill_amt_2 = []
bill_amt_3 = []
bill_amt_4 = []
bill_amt_5 = []
bill_amt_6 = []
pay_amt_1 = []
pay_amt_2 = []
pay_amt_3 = []
pay_amt_4 = []
pay_amt_5 = []
pay_amt_6 = []
limit_balance.append(st.number_input("Limit Balance", min_value=0))
sex.append(st.selectbox("Sex", [1, 2]))
education_level.append(st.selectbox("Education Level", [0, 1, 2, 3]))
marital_status.append(st.selectbox("Marital Status", [0, 1, 2 , 3]))
age.append(st.number_input("Age", min_value=0))
pay_0.append(st.number_input("Pay 0", min_value=-2, max_value=12))
pay_2.append(st.number_input("Pay 2", min_value=-2, max_value=12))
pay_3.append(st.number_input("Pay 3", min_value=-2, max_value=12))
pay_4.append(st.number_input("Pay 4", min_value=-2, max_value=12))
pay_5.append(st.number_input("Pay 5", min_value=-2, max_value=12))
pay_6.append(st.number_input("Pay 6", min_value=-2, max_value=12))
bill_amt_1.append(st.number_input("Bill Amount 1", min_value=0))
bill_amt_2.append(st.number_input("Bill Amount 2", min_value=0))
bill_amt_3.append(st.number_input("Bill Amount 3", min_value=0))
bill_amt_4.append(st.number_input("Bill Amount 4", min_value=0))
bill_amt_5.append(st.number_input("Bill Amount 5", min_value=0))
bill_amt_6.append(st.number_input("Bill Amount 6", min_value=0))
pay_amt_1.append(st.number_input("Pay Amount 1", min_value=0))
pay_amt_2.append(st.number_input("Pay Amount 2", min_value=0))
pay_amt_3.append(st.number_input("Pay Amount 3", min_value=0))
pay_amt_4.append(st.number_input("Pay Amount 4", min_value=0))
pay_amt_5.append(st.number_input("Pay Amount 5", min_value=0))
pay_amt_6.append(st.number_input("Pay Amount 6", min_value=0))
if st.button("Submit"):
data = {
'limit_balance': limit_balance,
'sex': sex,
'education_level': education_level,
'marital_status': marital_status,
'age': age,
'pay_0': pay_0,
'pay_2': pay_2,
'pay_3': pay_3,
'pay_4': pay_4,
'pay_5': pay_5,
'pay_6': pay_6,
'bill_amt_1': bill_amt_1,
'bill_amt_2': bill_amt_2,
'bill_amt_3': bill_amt_3,
'bill_amt_4': bill_amt_4,
'bill_amt_5': bill_amt_5,
'bill_amt_6': bill_amt_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
}
df = pd.DataFrame(data)
st.write("Data Preview:")
st.write(df)
# Select only the columns for modeling
data_inference_num = df[selected_columns]
# Scaling the data
data_inference_scaled = scaler.transform(data_inference_num)
# Create new dataframe
data_inference_final = pd.DataFrame(data_inference_scaled, columns=selected_columns)
# Predict the defaulter
predicted_defaulter = model.predict(data_inference_final)
# Show result
st.write("Predicted Defaulter Status:")
st.write(predicted_defaulter)