Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -106,13 +106,17 @@ if st.button("生成圖表"):
|
|
106 |
st.plotly_chart(fig_bar, use_container_width=True)
|
107 |
|
108 |
with tab3:
|
109 |
-
|
110 |
-
|
111 |
-
st.subheader("溫室氣體數據的K-means分析")
|
112 |
|
113 |
-
|
114 |
-
|
115 |
|
|
|
|
|
|
|
|
|
|
|
116 |
if len(cluster_features) >= 2:
|
117 |
# 準備數據
|
118 |
X = selected_df[cluster_features]
|
@@ -120,7 +124,6 @@ if st.button("生成圖表"):
|
|
120 |
X_scaled = scaler.fit_transform(X)
|
121 |
|
122 |
# 執行K-means聚類
|
123 |
-
n_clusters = st.slider("選擇聚類數量", min_value=2, max_value=10, value=3)
|
124 |
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
|
125 |
clusters = kmeans.fit_predict(X_scaled)
|
126 |
|
@@ -134,7 +137,7 @@ if st.button("生成圖表"):
|
|
134 |
y=cluster_features[1],
|
135 |
color='Cluster',
|
136 |
hover_data=['公司名稱'],
|
137 |
-
title=f"
|
138 |
)
|
139 |
st.plotly_chart(fig_scatter, use_container_width=True)
|
140 |
|
@@ -145,8 +148,6 @@ if st.button("生成圖表"):
|
|
145 |
|
146 |
else:
|
147 |
st.warning("請至少選擇兩個特徵進行聚類分析。")
|
148 |
-
else:
|
149 |
-
st.info("K-means分析僅適用於溫室氣體數據集。")
|
150 |
|
151 |
st.success("圖表生成完成!")
|
152 |
|
|
|
106 |
st.plotly_chart(fig_bar, use_container_width=True)
|
107 |
|
108 |
with tab3:
|
109 |
+
# 對所有數據集執行K-means分析
|
110 |
+
st.subheader(f"{dataset_choice}數據的K-means分析")
|
|
|
111 |
|
112 |
+
# 選擇用於聚類的特徵
|
113 |
+
cluster_features = st.multiselect("選擇用於聚類的特徵", numeric_columns, default=numeric_columns[:2])
|
114 |
|
115 |
+
# 選擇聚類數量
|
116 |
+
n_clusters = st.slider("選擇聚類數量", min_value=2, max_value=10, value=3)
|
117 |
+
|
118 |
+
# 添加一個執行K-means分析的按鈕
|
119 |
+
if st.button("執行K-means分析"):
|
120 |
if len(cluster_features) >= 2:
|
121 |
# 準備數據
|
122 |
X = selected_df[cluster_features]
|
|
|
124 |
X_scaled = scaler.fit_transform(X)
|
125 |
|
126 |
# 執行K-means聚類
|
|
|
127 |
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
|
128 |
clusters = kmeans.fit_predict(X_scaled)
|
129 |
|
|
|
137 |
y=cluster_features[1],
|
138 |
color='Cluster',
|
139 |
hover_data=['公司名稱'],
|
140 |
+
title=f"{dataset_choice}數據的K-means聚類 ({cluster_features[0]} vs {cluster_features[1]})"
|
141 |
)
|
142 |
st.plotly_chart(fig_scatter, use_container_width=True)
|
143 |
|
|
|
148 |
|
149 |
else:
|
150 |
st.warning("請至少選擇兩個特徵進行聚類分析。")
|
|
|
|
|
151 |
|
152 |
st.success("圖表生成完成!")
|
153 |
|