-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
74 lines (63 loc) · 3.04 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
from sklearn import preprocessing
import streamlit as st
import pandas as pd
import plotly.figure_factory as ff
import matplotlib.pyplot as plt
import seaborn as sns
import pickle
# Load the model
filename = 'gb_model.sav'
loaded_model = pickle.load(open(filename, 'rb'))
# Construct the path to the CSV file dynamically
current_dir = os.path.dirname(__file__)
file_path = os.path.join(current_dir, 'Clustered_Customer_Data.csv')
# Load the CSV file
try:
df = pd.read_csv(file_path)
except FileNotFoundError:
st.error(f"File not found: {file_path}")
st.stop()
# Set Streamlit options
st.set_option('deprecation.showPyplotGlobalUse', False)
# CSS for background color
st.markdown(
"""
<style>
.stApp {
background-color: lightblue !important;
}
</style>
""",
unsafe_allow_html=True
)
st.title("Market Segment Prediction")
with st.form("my_form"):
balance = st.number_input(label='Balance', step=0.001, format="%.6f")
balance_frequency = st.number_input(label='Balance Frequency', step=0.001, format="%.6f")
purchases = st.number_input(label='Purchases', step=0.01, format="%.2f")
oneoff_purchases = st.number_input(label='OneOff Purchases', step=0.01, format="%.2f")
installments_purchases = st.number_input(label='Installments Purchases', step=0.01, format="%.2f")
cash_advance = st.number_input(label='Cash Advance', step=0.01, format="%.6f")
purchases_frequency = st.number_input(label='Purchases Frequency', step=0.01, format="%.6f")
oneoff_purchases_frequency = st.number_input(label='OneOff Purchases Frequency', step=0.1, format="%.6f")
purchases_installment_frequency = st.number_input(label='Purchases Installments Frequency', step=0.1, format="%.6f")
cash_advance_frequency = st.number_input(label='Cash Advance Frequency', step=0.1, format="%.6f")
cash_advance_trx = st.number_input(label='Cash Advance Trx', step=1)
purchases_trx = st.number_input(label='Purchases TRX', step=1)
credit_limit = st.number_input(label='Credit Limit', step=0.1, format="%.1f")
payments = st.number_input(label='Payments', step=0.01, format="%.6f")
minimum_payments = st.number_input(label='Minimum Payments', step=0.01, format="%.6f")
prc_full_payment = st.number_input(label='PRC Full Payment', step=0.01, format="%.6f")
tenure = st.number_input(label='Tenure', step=1)
data = [[balance, balance_frequency, purchases, oneoff_purchases, installments_purchases, cash_advance, purchases_frequency, oneoff_purchases_frequency, purchases_installment_frequency, cash_advance_frequency, cash_advance_trx, purchases_trx, credit_limit, payments, minimum_payments, prc_full_payment, tenure]]
submitted = st.form_submit_button("Submit")
if submitted:
clust = loaded_model.predict(data)[0]
st.write(f'Data Belongs to Cluster {clust}')
cluster_df1 = df[df['Cluster'] == clust]
plt.rcParams["figure.figsize"] = (20, 3)
for c in cluster_df1.drop(['Cluster'], axis=1):
fig, ax = plt.subplots()
sns.histplot(cluster_df1, x=c, ax=ax)
st.pyplot(fig)