pybanking_churn / app.py
nikhil0nk's picture
clean
b337966
import streamlit as st
import streamlit.components.v1 as components
import pandas as pd
import pickle
import matplotlib.pyplot as plt
from pybanking.churn_prediction import model_churn
from pybanking.EDA import data_analysis
import sklearn.metrics as metrics
from mlxtend.plotting import plot_confusion_matrix
import streamlit.components.v1 as components
from PIL import Image
st.set_page_config(page_title="Customer Churn Prediction Model", layout="wide")
col1,col2 = st.columns([1,2])
with col1:
image = Image.open('Shorthills.png')
st.image(image)
with col2:
st.title('Customer Churn Prediction Model')
df = model_churn.get_data()
option2 = st.selectbox(
'Which dataset would you like to use for prediction?',
['Sample Dataset', 'Upload Custom']
)
if option2 == 'Upload Custom':
file = st.file_uploader("Choose a file")
if file is not None:
#read csv
df = pd.read_csv(file)
else:
st.warning("you need to upload a csv file.")
st.subheader('This is the Selected Data')
st.dataframe(df.head(5))
analysis_class = data_analysis.Analysis()
option3 = st.selectbox(
'Select Exploratory Data Analysis type',
['None', 'DataPrep', 'SweetViz', 'PandasProfiling']
)
if option3 == 'SweetViz':
res = analysis_class.sweetviz_analysis(df)
res.show_html(filepath='SweetViz.html', open_browser=True, layout='widescreen', scale=None)
HtmlFile = open('SweetViz.html', 'r', encoding='utf-8')
source_code = HtmlFile.read()
with st.expander("See Report"):
components.html(source_code, height=600, scrolling=True)
elif option3 == 'DataPrep':
res = analysis_class.dataprep_analysis(df)
# res.show_browser()
res.save('DataPrep.html')
HtmlFile = open('DataPrep.html', 'r', encoding='utf-8')
source_code = HtmlFile.read()
with st.expander("See Report"):
components.html(source_code, height=600, scrolling=True)
elif option3 == 'PandasProfiling':
res = analysis_class.pandas_analysis(df)
res.to_file("PandasProfiling.html")
HtmlFile = open('PandasProfiling.html', 'r', encoding='utf-8')
source_code = HtmlFile.read()
with st.expander("See Report"):
components.html(source_code, height=600, scrolling=True)
model_names = [
"Logistic_Regression",
"Support_Vector_Machine",
"Support_Vector_Machine_Optimized",
"Decision_Tree",
"Neural_Network",
"Random_Forest",
"Pycaret_Best"
]
option = st.selectbox(
'Select a model to be used',
model_names
)
model = pickle.load(open(option+'.pkl', 'rb'))
st.write("Model Loaded : ", option)
X, y = model_churn.preprocess_inputs(df, option)
if option2 == 'Upload custom':
model = model_churn.train(df, model)
y_pred = model.predict(X)
st.write("Accuracy:",metrics.accuracy_score(y, y_pred))
st.write("Precision:",metrics.precision_score(y, y_pred))
st.write("Recall:",metrics.recall_score(y, y_pred))
fig, ax = plot_confusion_matrix(conf_mat=metrics.confusion_matrix(y, y_pred), figsize=(6, 6), cmap=plt.cm.Reds, colorbar=True)
plt.xlabel('Predictions', fontsize=18)
plt.ylabel('Actuals', fontsize=18)
plt.title('Confusion Matrix', fontsize=18)
st.pyplot(fig)