#Core pkgs
import streamlit as st
import streamlit.components.v1 as stc
from streamlit_pandas_profiling import st_profile_report
from pandas_profiling import ProfileReport
#EDA pkgs
import pandas as pd
import numpy as np
import codecs
#Utils
import os
import joblib
# Images
from PIL import Image
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from catboost import CatBoostClassifier
from gmpy2 import nan
def main():
"""
docstring
"""
menu = ["Home","EDA","Predition","About"]
choice = st.sidebar.selectbox("Menu",menu)
if choice =="Home":
st.subheader("UmojaHack")
elif choice == "EDA":
st.subheader("Automated EDA with pandas_profiling")
data_file=st.file_uploader("upload your dataset")
if data_file is not None:
df=pd.read_csv(data_file)
st.dataframe(df.head())
profile=ProfileReport(df)
st_profile_report(profile)
elif choice == "Predition":
st.subheader("Predition")
stc.html("""
<div style="background-color:tomato;padding:10px;border-radius:10px">
<h1 style="color:white;text-align:center;"> Prediction</h1>
</div> """)
def get_value(val,my_dict):
for key ,value in my_dict.items():
if val == key:
return value
col1,col2= st.beta_columns(2)
with col1:
policy_start_date_day= st.number_input("Policy Start Data by Day",1,30)
policy_start_date_month= st.number_input("Policy Start Data by month",1,12)
policy_start_date_quarter= st.number_input("Policy Start Data by quarter",1,5)
policy_end_date_day= st.number_input("Policy End Data by Day",1,30)
policy_end_date_month= st.number_input("Policy End Data by month",1,12)
policy_end_date_quarter= st.number_input("Policy End Data by quarter",1,5)
first_transaction_date_day= st.number_input("First Transaction by Day",1,30)
first_transaction_date_month= st.number_input("First Transaction by month",1,12)
with col2:
age = st.slider("Select Age",1,300,30)
gender_dict = {"Male":2,"Female":4,"others":6}
gender= st.selectbox("Please Select your gender",tuple(gender_dict.keys()))
result_gender = get_value(gender,gender_dict)
# st.write(result_gender)
product_name_dict = {'Car Classic':1, 'CarSafe':5, 'Muuve':8, 'CVTP':0, 'Car Plus':2,'Motor Cycle':7,
'Customized Motor':6, 'CarFlex':4, 'Car Vintage':3}
product_name= st.selectbox("Please Select the ProductName",tuple(product_name_dict.keys()))
result_product_name = get_value(product_name,product_name_dict)
# st.text(result_productname)
Car_Category_dict = { "Saloon":9, "JEEP":2, nan:6, "Motorcycle":5, "Truck":14, "Bus":0, "Mini Bus":3,
"Pick Up":7, "Mini Van":4, "Van":15, "Pick Up ":8, "CAMRY CAR HIRE":1,
"Wagon":16, "Shape Of Vehicle Chasis":11, "Sedan":10, "Station 4 Wheel":12,
"Tipper Truck":13}
Car_Category= st.selectbox("Please Select Car Category ",tuple(Car_Category_dict.keys()))
result_Car_Category= get_value(Car_Category,Car_Category_dict)
# st.text(result_Car_Category)
no_pol_prod_name =st.number_input("Number of Policy/ Product Name",1,1000)
Date_diff =st.number_input("Data Difference",1,400)
no_pol=st.number_input("Number of Policy",1,400,30)
first_transaction_date_quarter= st.number_input("First Transaction by quarter",1,5)
# st.write(result_gender)
# Result and in json format
results = [gender, age, no_pol, Car_Category, product_name, Date_diff, policy_start_date_day, policy_start_date_month, policy_start_date_quarter,
policy_end_date_day, policy_end_date_month, policy_end_date_quarter, first_transaction_date_day, first_transaction_date_month,
first_transaction_date_quarter, no_pol_prod_name]
displayed_results = [gender, age, no_pol, Car_Category, product_name, Date_diff, policy_start_date_day, policy_start_date_month, policy_start_date_quarter,
policy_end_date_day, policy_end_date_month, policy_end_date_quarter, first_transaction_date_day, first_transaction_date_month,
first_transaction_date_quarter, no_pol_prod_name]
prettified_result = {
"gender" :gender,
"Age":age,
"no_pol":no_pol,
"Car_Category":Car_Category,
"product_name":product_name,
"Date_diff":Date_diff,
"policy_start_date_day":policy_start_date_day,
"policy_start_date_month":policy_start_date_month,
"policy_start_date_quarter":policy_start_date_quarter,
"policy_end_date_day":policy_end_date_day,
"policy_end_date_month":policy_end_date_month,
"policy_end_date_quarter":policy_end_date_quarter,
"first_transaction_date_day":first_transaction_date_day,
"first_transaction_date_month":first_transaction_date_month,
"first_transaction_date_quarter":first_transaction_date_quarter,
"no_pol_prod_name":no_pol_prod_name
}
sample_data = np.array(results).reshape(1, -1)
if st.checkbox("Your Inputs Summary"):
st.json(prettified_result)
st.text("Vectorized as ::{}".format(results))
st.subheader("Prediction")
if st.checkbox("Select Model for Prediction"):
all_ml_dict= {
"xgboost":XGBClassifier(),
"Catboost": CatBoostClassifier(),
"LGBM": LGBMClassifier()}
# Find the Key From Dictionary
def get_key(val,my_dict):
for key ,value in my_dict.items():
if val == value:
return key
# Load Models
def load_model_n_predict(model_file):
loaded_model = joblib.load(open(os.path.join(model_file),"rb"))
return loaded_model
# Model Selection
model_choice = st.selectbox('Model Choice',list(all_ml_dict.keys()))
prediction_label = {"Would not Claim Insurance": 0,"Would Claim Insurance": 1}
if st.button("Predict"):
# if model_choice == 'Catboost':
# loaded_model = joblib.load(open("models/catboost3_model.pickle","rb"))
# prediction = loaded_model.predict(sample_data)
# # final_result = get_key(prediction,prediction_label)
# # # st.info(final_result)
if model_choice == 'LGBM':
model_predictor = load_model_n_predict("models/lgbm_model4.pickle")
prediction = model_predictor.predict(sample_data)
# st.text(prediction)
elif model_choice == 'xgboost':
model_predictor = load_model_n_predict("models/xgboost_model4.pickle")
prediction = model_predictor.predict(sample_data)
# st.text(prediction)
final_result = get_key(prediction,prediction_label)
st.success(final_result)
else:
st.subheader("About Me")
if __name__ == "__main__":
main()
Here is the code
Here is the errror, please help where i went wrong