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|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import matplotlib as mpl
|
| 6 |
+
import pycaret
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from streamlit_option_menu import option_menu
|
| 9 |
+
import PIL
|
| 10 |
+
from PIL import Image
|
| 11 |
+
from PIL import ImageColor
|
| 12 |
+
from PIL import ImageDraw
|
| 13 |
+
from PIL import ImageFont
|
| 14 |
+
|
| 15 |
+
hide_streamlit_style = """
|
| 16 |
+
<style>
|
| 17 |
+
#MainMenu {visibility: hidden;}
|
| 18 |
+
footer {visibility: hidden;}
|
| 19 |
+
</style>
|
| 20 |
+
"""
|
| 21 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
| 22 |
+
|
| 23 |
+
with st.sidebar:
|
| 24 |
+
image = Image.open('./itaca_logo.png')
|
| 25 |
+
st.image(image,use_column_width=True)
|
| 26 |
+
page = option_menu(menu_title='Menu',
|
| 27 |
+
menu_icon="robot",
|
| 28 |
+
options=["Clustering Analysis",
|
| 29 |
+
"Anomaly Detection"],
|
| 30 |
+
icons=["chat-dots",
|
| 31 |
+
"key"],
|
| 32 |
+
default_index=0
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
st.title('ITACA Insurance Core AI Module')
|
| 36 |
+
|
| 37 |
+
if page == "Clustering Analysis":
|
| 38 |
+
st.header('Clustering Analysis')
|
| 39 |
+
|
| 40 |
+
st.write(
|
| 41 |
+
"""
|
| 42 |
+
"""
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# import pycaret unsupervised models
|
| 46 |
+
from pycaret.clustering import *
|
| 47 |
+
# import ClusteringExperiment
|
| 48 |
+
from pycaret.clustering import ClusteringExperiment
|
| 49 |
+
|
| 50 |
+
# Upload the CSV file
|
| 51 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
| 52 |
+
|
| 53 |
+
# Define the unsupervised model
|
| 54 |
+
clusteringmodel = ['kmeans', 'ap', 'meanshift', 'sc', 'hclust', 'dbscan', 'optics', 'birch']
|
| 55 |
+
selected_model = st.selectbox("Choose a clustering model", clusteringmodel)
|
| 56 |
+
|
| 57 |
+
# Define the options for the dropdown list
|
| 58 |
+
numclusters = [2, 3, 4, 5, 6]
|
| 59 |
+
# selected_clusters = st.selectbox("Choose a number of clusters", numclusters)
|
| 60 |
+
selected_clusters = st.slider("Choose a number of clusters", min_value=2, max_value=10, value=4)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# Read and display the CSV file
|
| 64 |
+
if uploaded_file is not None:
|
| 65 |
+
try:
|
| 66 |
+
delimiter = ','
|
| 67 |
+
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter)
|
| 68 |
+
except ValueError:
|
| 69 |
+
delimiter = '|'
|
| 70 |
+
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter, encoding='latin-1')
|
| 71 |
+
|
| 72 |
+
s = setup(insurance_claims, session_id = 123, log_experiment='mlflow', experiment_name='fraud_detection')
|
| 73 |
+
|
| 74 |
+
exp_clustering = ClusteringExperiment()
|
| 75 |
+
|
| 76 |
+
# init setup on exp
|
| 77 |
+
exp_clustering.setup(insurance_claims, session_id = 123)
|
| 78 |
+
|
| 79 |
+
if st.button("Prediction"):
|
| 80 |
+
with st.spinner("Analyzing..."):
|
| 81 |
+
# train kmeans model
|
| 82 |
+
cluster_model = create_model(selected_model, num_clusters = selected_clusters)
|
| 83 |
+
|
| 84 |
+
cluster_model_2 = assign_model(cluster_model)
|
| 85 |
+
cluster_model_2
|
| 86 |
+
|
| 87 |
+
all_metrics = get_metrics()
|
| 88 |
+
all_metrics
|
| 89 |
+
|
| 90 |
+
cluster_results = pull()
|
| 91 |
+
cluster_results
|
| 92 |
+
|
| 93 |
+
# plot pca cluster plot
|
| 94 |
+
plot_model(cluster_model, plot = 'cluster', display_format = 'streamlit')
|
| 95 |
+
|
| 96 |
+
if selected_model != 'ap':
|
| 97 |
+
plot_model(cluster_model, plot = 'tsne', display_format = 'streamlit')
|
| 98 |
+
|
| 99 |
+
if selected_model not in ('ap', 'meanshift', 'dbscan', 'optics'):
|
| 100 |
+
plot_model(cluster_model, plot = 'elbow', display_format = 'streamlit')
|
| 101 |
+
|
| 102 |
+
if selected_model not in ('ap', 'meanshift', 'sc', 'hclust', 'dbscan', 'optics'):
|
| 103 |
+
plot_model(cluster_model, plot = 'silhouette', display_format = 'streamlit')
|
| 104 |
+
|
| 105 |
+
if selected_model not in ('ap', 'sc', 'hclust', 'dbscan', 'optics', 'birch'):
|
| 106 |
+
plot_model(cluster_model, plot = 'distance', display_format = 'streamlit')
|
| 107 |
+
|
| 108 |
+
if selected_model != 'ap':
|
| 109 |
+
plot_model(cluster_model, plot = 'distribution', display_format = 'streamlit')
|
| 110 |
+
|
| 111 |
+
elif page == "Anomaly Detection":
|
| 112 |
+
st.header('Anomaly Detection')
|
| 113 |
+
|
| 114 |
+
st.write(
|
| 115 |
+
"""
|
| 116 |
+
"""
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# import pycaret anomaly
|
| 120 |
+
from pycaret.anomaly import *
|
| 121 |
+
# import AnomalyExperiment
|
| 122 |
+
from pycaret.anomaly import AnomalyExperiment
|
| 123 |
+
|
| 124 |
+
# Upload the CSV file
|
| 125 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
| 126 |
+
|
| 127 |
+
# Define the unsupervised model
|
| 128 |
+
anomalymodel = ['abod', 'cluster', 'cof', 'iforest', 'histogram', 'knn', 'lof', 'svm', 'pca', 'mcd', 'sod', 'sos']
|
| 129 |
+
selected_model = st.selectbox("Choose an anomaly model", anomalymodel)
|
| 130 |
+
|
| 131 |
+
# Read and display the CSV file
|
| 132 |
+
if uploaded_file is not None:
|
| 133 |
+
try:
|
| 134 |
+
delimiter = ','
|
| 135 |
+
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter)
|
| 136 |
+
except ValueError:
|
| 137 |
+
delimiter = '|'
|
| 138 |
+
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter, encoding='latin-1')
|
| 139 |
+
|
| 140 |
+
s = setup(insurance_claims, session_id = 123)
|
| 141 |
+
|
| 142 |
+
exp_anomaly = AnomalyExperiment()
|
| 143 |
+
|
| 144 |
+
# init setup on exp
|
| 145 |
+
exp_anomaly.setup(insurance_claims, session_id = 123)
|
| 146 |
+
|
| 147 |
+
if st.button("Prediction"):
|
| 148 |
+
with st.spinner("Analyzing..."):
|
| 149 |
+
# train model
|
| 150 |
+
anomaly_model = create_model(selected_model)
|
| 151 |
+
|
| 152 |
+
anomaly_model_2 = assign_model(anomaly_model)
|
| 153 |
+
anomaly_model_2
|
| 154 |
+
|
| 155 |
+
anomaly_results = pull()
|
| 156 |
+
anomaly_results
|
| 157 |
+
|
| 158 |
+
# plot
|
| 159 |
+
plot_model(anomaly_model, plot = 'tsne', display_format = 'streamlit')
|
| 160 |
+
plot_model(anomaly_model, plot = 'umap', display_format = 'streamlit')
|