Spaces:
Runtime error
Runtime error
File size: 9,860 Bytes
12459eb 1b23081 12459eb 1715696 12459eb 1715696 12459eb 1715696 12459eb 9c773d7 441577a 1715696 2c1612c 9c773d7 12459eb 760e447 1715696 12459eb 1715696 12459eb 1715696 12459eb 1715696 12459eb 1715696 12459eb 1715696 12459eb 3f3d080 12459eb d6b7dee 12459eb 3f3d080 d6b7dee 1715696 e0787af 12459eb 1715696 12459eb d6b7dee 12459eb c34d8f1 12459eb b86c3d4 12459eb 1715696 |
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
import os
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
import pycaret
import streamlit as st
from streamlit_option_menu import option_menu
import PIL
from PIL import Image
from PIL import ImageColor
from PIL import ImageDraw
from PIL import ImageFont
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
with st.sidebar:
image = Image.open('itaca_logo.png')
st.image(image, width=150) #,use_column_width=True)
page = option_menu(menu_title='Menu',
menu_icon="robot",
options=["Clustering Analysis",
"Anomaly Detection"],
icons=["chat-dots",
"key"],
default_index=0
)
# Additional section below the option menu
# st.markdown("---") # Add a separator line
st.header("Settings")
# Define the options for the dropdown list
numclusters = [2, 3, 4, 5, 6]
# selected_clusters = st.selectbox("Choose a number of clusters", numclusters)
selected_clusters = st.slider("Choose a number of clusters", min_value=2, max_value=10, value=4)
p_remove_multicollinearity = st.checkbox("Remove Multicollinearity", value=False)
p_multicollinearity_threshold = st.slider("Choose multicollinearity thresholds", min_value=0.0, max_value=1.0, value=0.9)
# p_remove_outliers = st.checkbox("Remove Outliers", value=False)
# p_outliers_method = st.selectbox ("Choose an Outlier Method", ["iforest", "ee", "lof"])
p_transformation = st.checkbox("Choose Power Transform", value = False)
p_normalize = st.checkbox("Choose Normalize", value = False)
p_pca = st.checkbox("Choose PCA", value = False)
p_pca_method = st.selectbox ("Choose a PCA Method", ["linear", "kernel", "incremental"])
st.title('ITACA Insurance Core AI Module')
if page == "Clustering Analysis":
st.header('Clustering Analysis')
st.write(
"""
"""
)
# import pycaret unsupervised models
from pycaret.clustering import *
# import ClusteringExperiment
from pycaret.clustering import ClusteringExperiment
# Display the list of CSV files
directory = "./"
all_files = os.listdir(directory)
# Filter files to only include CSV files
csv_files = [file for file in all_files if file.endswith(".csv")]
# Select a CSV file from the list
selected_csv = st.selectbox("Select a CSV file from the list", ["None"] + csv_files)
# Upload the CSV file
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
# Define the unsupervised model
clusteringmodel = ['kmeans', 'ap', 'meanshift', 'sc', 'hclust', 'dbscan', 'optics', 'birch']
selected_model = st.selectbox("Choose a clustering model", clusteringmodel)
# Read and display the CSV file
if selected_csv != "None" or uploaded_file is not None:
if uploaded_file:
try:
delimiter = ','
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter)
except ValueError:
delimiter = '|'
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter, encoding='latin-1')
else:
insurance_claims = pd.read_csv(selected_csv)
insurance_claims.describe().T
cat_col = insurance_claims.select_dtypes(include=['object']).columns
num_col = insurance_claims.select_dtypes(exclude=['object']).columns
# insurance_claims[num_col].hist(bins=15, figsize=(20, 15), layout=(5, 4))
# Calculate the correlation matrix
corr_matrix = insurance_claims[num_col].corr()
# Create a Matplotlib figure
fig, ax = plt.subplots(figsize=(12, 8))
# Create a heatmap using seaborn
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', fmt='.2f', ax=ax)
# Set the title for the heatmap
ax.set_title('Correlation Heatmap')
# Display the heatmap in Streamlit
st.pyplot(fig)
all_columns = insurance_claims.columns.tolist()
selected_columns = st.multiselect("Choose columns", all_columns, default=all_columns)
if st.button("Prediction"):
insurance_claims = insurance_claims[selected_columns].copy()
s = setup(insurance_claims, session_id = 123, remove_multicollinearity=p_remove_multicollinearity, multicollinearity_threshold=p_multicollinearity_threshold,
# remove_outliers=p_remove_outliers, outliers_method=p_outliers_method,
transformation=p_transformation,
normalize=p_normalize, pca=p_pca, pca_method=p_pca_method)
exp_clustering = ClusteringExperiment()
# init setup on exp
exp_clustering.setup(insurance_claims, session_id = 123)
with st.spinner("Analyzing..."):
# train kmeans model
cluster_model = create_model(selected_model, num_clusters = selected_clusters)
cluster_model_2 = assign_model(cluster_model)
# Calculate summary statistics for each cluster
cluster_summary = cluster_model_2.groupby('Cluster').agg(['count', 'mean', 'median', 'min', 'max',
'std', 'var', 'sum', ('quantile_25', lambda x: x.quantile(0.25)),
('quantile_75', lambda x: x.quantile(0.75)), 'skew'])
cluster_summary
cluster_model_2
# all_metrics = get_metrics()
# all_metrics
cluster_results = pull()
cluster_results
# plot pca cluster plot
plot_model(cluster_model, plot = 'cluster', display_format = 'streamlit')
if selected_model != 'ap':
plot_model(cluster_model, plot = 'tsne', display_format = 'streamlit')
if selected_model not in ('ap', 'meanshift', 'dbscan', 'optics'):
plot_model(cluster_model, plot = 'elbow', display_format = 'streamlit')
if selected_model not in ('ap', 'meanshift', 'sc', 'hclust', 'dbscan', 'optics'):
plot_model(cluster_model, plot = 'silhouette', display_format = 'streamlit')
if selected_model not in ('ap', 'sc', 'hclust', 'dbscan', 'optics', 'birch'):
plot_model(cluster_model, plot = 'distance', display_format = 'streamlit')
if selected_model != 'ap':
plot_model(cluster_model, plot = 'distribution', display_format = 'streamlit')
elif page == "Anomaly Detection":
st.header('Anomaly Detection')
st.write(
"""
"""
)
# import pycaret anomaly
from pycaret.anomaly import *
# import AnomalyExperiment
from pycaret.anomaly import AnomalyExperiment
# Display the list of CSV files
directory = "./"
all_files = os.listdir(directory)
# Filter files to only include CSV files
csv_files = [file for file in all_files if file.endswith(".csv")]
# Select a CSV file from the list
selected_csv = st.selectbox("Select a CSV file from the list", ["None"] + csv_files)
# Upload the CSV file
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
# Define the unsupervised model
anomalymodel = ['abod', 'cluster', 'cof', 'iforest', 'histogram', 'knn', 'lof', 'svm', 'pca', 'mcd', 'sod', 'sos']
selected_model = st.selectbox("Choose an anomaly model", anomalymodel)
# Read and display the CSV file
if selected_csv != "None" or uploaded_file is not None:
if uploaded_file:
try:
delimiter = ','
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter)
except ValueError:
delimiter = '|'
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter, encoding='latin-1')
else:
insurance_claims = pd.read_csv(selected_csv)
all_columns = insurance_claims.columns.tolist()
selected_columns = st.multiselect("Choose columns", all_columns, default=all_columns)
if st.button("Prediction"):
insurance_claims = insurance_claims[selected_columns].copy()
# s = setup(insurance_claims, session_id = 123)
s = setup(insurance_claims, session_id = 123, remove_multicollinearity=p_remove_multicollinearity, multicollinearity_threshold=p_multicollinearity_threshold,
# remove_outliers=p_remove_outliers, outliers_method=p_outliers_method,
transformation=p_transformation,
normalize=p_normalize, pca=p_pca, pca_method=p_pca_method)
exp_anomaly = AnomalyExperiment()
# init setup on exp
exp_anomaly.setup(insurance_claims, session_id = 123)
with st.spinner("Analyzing..."):
# train model
anomaly_model = create_model(selected_model)
anomaly_model_2 = assign_model(anomaly_model)
anomaly_model_2
anomaly_results = pull()
anomaly_results
# plot
plot_model(anomaly_model, plot = 'tsne', display_format = 'streamlit')
plot_model(anomaly_model, plot = 'umap', display_format = 'streamlit') |