VIF_0918 / app_V1.py
JERNGOC's picture
Create app_V1.py
ab22dbd verified
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import joblib
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans, DBSCAN
from scipy.cluster.hierarchy import fcluster, linkage
# 讀取保存的模型
scaler = joblib.load('scaler.sav')
pca = joblib.load('pca_model.sav')
kmeans = joblib.load('kmeans_model.sav')
linked = joblib.load('hierarchical_model.sav')
dbscan = joblib.load('dbscan_model.sav')
# 標題和簡介
st.title("聚類分析 - KMeans, Hierarchical Clustering 和 DBSCAN")
st.write("上傳 CSV 文件並查看聚類結果")
# 上傳文件
uploaded_file = st.file_uploader("上傳 CSV 文件", type=["csv"])
if uploaded_file is not None:
# 讀取上傳的 CSV 文件
data = pd.read_csv(uploaded_file)
# 移除 'Time' 欄位
numerical_data = data.drop(columns=['Time'])
# 標準化數據
scaled_data = scaler.transform(numerical_data)
# 使用 PCA 進行降維
pca_data = pca.transform(scaled_data)
# 創建包含主成分的 DataFrame
pca_df = pd.DataFrame(pca_data, columns=['PC1', 'PC2'])
# 使用保存的 K-means 模型進行聚類
kmeans_labels = kmeans.predict(pca_df)
# 使用保存的階層式聚類結果
hclust_labels = fcluster(linked, 3, criterion='maxclust')
# 使用保存的 DBSCAN 模型進行聚類
dbscan_labels = dbscan.fit_predict(pca_df)
# ================== K-means 聚類圖表 ==================
st.subheader("K-means 聚類結果")
fig_kmeans, ax_kmeans = plt.subplots()
ax_kmeans.scatter(pca_df['PC1'], pca_df['PC2'], c=kmeans_labels, cmap='viridis')
ax_kmeans.set_title('K-means Clustering')
ax_kmeans.set_xlabel('PC1')
ax_kmeans.set_ylabel('PC2')
st.pyplot(fig_kmeans)
# ================== 階層式聚類圖表 ==================
st.subheader("階層式聚類結果")
fig_hclust, ax_hclust = plt.subplots()
ax_hclust.scatter(pca_df['PC1'], pca_df['PC2'], c=hclust_labels, cmap='viridis')
ax_hclust.set_title('Hierarchical Clustering')
ax_hclust.set_xlabel('PC1')
ax_hclust.set_ylabel('PC2')
st.pyplot(fig_hclust)
# ================== DBSCAN 聚類圖表 ==================
st.subheader("DBSCAN 聚類結果")
fig_dbscan, ax_dbscan = plt.subplots()
ax_dbscan.scatter(pca_df['PC1'], pca_df['PC2'], c=dbscan_labels, cmap='viridis')
ax_dbscan.set_title('DBSCAN Clustering')
ax_dbscan.set_xlabel('PC1')
ax_dbscan.set_ylabel('PC2')
st.pyplot(fig_dbscan)