JERNGOC commited on
Commit
d9c3a39
·
verified ·
1 Parent(s): ab22dbd

Delete app_V1.py.txt

Browse files
Files changed (1) hide show
  1. app_V1.py.txt +0 -74
app_V1.py.txt DELETED
@@ -1,74 +0,0 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import matplotlib.pyplot as plt
4
- import joblib
5
- from sklearn.decomposition import PCA
6
- from sklearn.preprocessing import StandardScaler
7
- from sklearn.cluster import KMeans, DBSCAN
8
- from scipy.cluster.hierarchy import fcluster, linkage
9
-
10
- # 讀取保存的模型
11
- scaler = joblib.load('scaler.sav')
12
- pca = joblib.load('pca_model.sav')
13
- kmeans = joblib.load('kmeans_model.sav')
14
- linked = joblib.load('hierarchical_model.sav')
15
- dbscan = joblib.load('dbscan_model.sav')
16
-
17
- # 標題和簡介
18
- st.title("聚類分析 - KMeans, Hierarchical Clustering 和 DBSCAN")
19
- st.write("上傳 CSV 文件並查看聚類結果")
20
-
21
- # 上傳文件
22
- uploaded_file = st.file_uploader("上傳 CSV 文件", type=["csv"])
23
-
24
- if uploaded_file is not None:
25
- # 讀取上傳的 CSV 文件
26
- data = pd.read_csv(uploaded_file)
27
-
28
- # 移除 'Time' 欄位
29
- numerical_data = data.drop(columns=['Time'])
30
-
31
- # 標準化數據
32
- scaled_data = scaler.transform(numerical_data)
33
-
34
- # 使用 PCA 進行降維
35
- pca_data = pca.transform(scaled_data)
36
-
37
- # 創建包含主成分的 DataFrame
38
- pca_df = pd.DataFrame(pca_data, columns=['PC1', 'PC2'])
39
-
40
- # 使用保存的 K-means 模型進行聚類
41
- kmeans_labels = kmeans.predict(pca_df)
42
-
43
- # 使用保存的階層式聚類結果
44
- hclust_labels = fcluster(linked, 3, criterion='maxclust')
45
-
46
- # 使用保存的 DBSCAN 模型進行聚類
47
- dbscan_labels = dbscan.fit_predict(pca_df)
48
-
49
- # ================== K-means 聚類圖表 ==================
50
- st.subheader("K-means 聚類結果")
51
- fig_kmeans, ax_kmeans = plt.subplots()
52
- ax_kmeans.scatter(pca_df['PC1'], pca_df['PC2'], c=kmeans_labels, cmap='viridis')
53
- ax_kmeans.set_title('K-means Clustering')
54
- ax_kmeans.set_xlabel('PC1')
55
- ax_kmeans.set_ylabel('PC2')
56
- st.pyplot(fig_kmeans)
57
-
58
- # ================== 階層式聚類圖表 ==================
59
- st.subheader("階層式聚類結果")
60
- fig_hclust, ax_hclust = plt.subplots()
61
- ax_hclust.scatter(pca_df['PC1'], pca_df['PC2'], c=hclust_labels, cmap='viridis')
62
- ax_hclust.set_title('Hierarchical Clustering')
63
- ax_hclust.set_xlabel('PC1')
64
- ax_hclust.set_ylabel('PC2')
65
- st.pyplot(fig_hclust)
66
-
67
- # ================== DBSCAN 聚類圖表 ==================
68
- st.subheader("DBSCAN 聚類結果")
69
- fig_dbscan, ax_dbscan = plt.subplots()
70
- ax_dbscan.scatter(pca_df['PC1'], pca_df['PC2'], c=dbscan_labels, cmap='viridis')
71
- ax_dbscan.set_title('DBSCAN Clustering')
72
- ax_dbscan.set_xlabel('PC1')
73
- ax_dbscan.set_ylabel('PC2')
74
- st.pyplot(fig_dbscan)