Spaces:
Sleeping
Sleeping
Roberta2024
commited on
Commit
•
8fe277d
1
Parent(s):
9ae4286
Update app.py
Browse files
app.py
CHANGED
@@ -8,8 +8,6 @@ import io
|
|
8 |
import time
|
9 |
from sklearn.cluster import KMeans
|
10 |
from sklearn.preprocessing import StandardScaler
|
11 |
-
import base64
|
12 |
-
from io import BytesIO
|
13 |
|
14 |
# 確保正確的中文字符編碼
|
15 |
st.set_page_config(page_title="🌳台灣中小企業ESG數據分析與揭露儀表板🌲", page_icon=":chart_with_upwards_trend:", layout="wide")
|
@@ -30,15 +28,6 @@ def load_data(url):
|
|
30 |
df = df.fillna(0)
|
31 |
return df
|
32 |
|
33 |
-
# 函數:將圖表轉換為可下載的連結
|
34 |
-
def get_image_download_link(fig, filename, text):
|
35 |
-
buf = BytesIO()
|
36 |
-
fig.write_image(buf, format="png")
|
37 |
-
buf.seek(0)
|
38 |
-
b64 = base64.b64encode(buf.read()).decode()
|
39 |
-
href = f'<a href="data:image/png;base64,{b64}" download="{filename}">{text}</a>'
|
40 |
-
return href
|
41 |
-
|
42 |
# Streamlit 應用程式
|
43 |
st.title("台灣企業ESG數據分析與揭露")
|
44 |
st.subheader("以溫室氣體 X 再生能源 X 董事會資訊: https://www.tejwin.com/insight/carbon-footprint-verification/")
|
@@ -95,9 +84,6 @@ if st.button("生成圖表"):
|
|
95 |
margin=dict(t=50, b=50, l=50, r=50)
|
96 |
)
|
97 |
st.plotly_chart(fig_pie, use_container_width=True)
|
98 |
-
|
99 |
-
# 添加下載按鈕
|
100 |
-
st.markdown(get_image_download_link(fig_pie, "pie_chart.png", "下載圓餅圖"), unsafe_allow_html=True)
|
101 |
|
102 |
with tab2:
|
103 |
# 使用 plotly 創建長條圖
|
@@ -118,22 +104,15 @@ if st.button("生成圖表"):
|
|
118 |
height=600
|
119 |
)
|
120 |
st.plotly_chart(fig_bar, use_container_width=True)
|
121 |
-
|
122 |
-
# 添加下載按鈕
|
123 |
-
st.markdown(get_image_download_link(fig_bar, "bar_chart.png", "下載長條圖"), unsafe_allow_html=True)
|
124 |
|
125 |
with tab3:
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
# 選擇用於聚類的特徵
|
130 |
-
cluster_features = st.multiselect("選擇用於聚類的特徵", numeric_columns, default=numeric_columns[:2])
|
131 |
|
132 |
-
|
133 |
-
|
134 |
|
135 |
-
# 添加一個執行K-means分析的按鈕
|
136 |
-
if st.button("執行K-means分析"):
|
137 |
if len(cluster_features) >= 2:
|
138 |
# 準備數據
|
139 |
X = selected_df[cluster_features]
|
@@ -141,6 +120,7 @@ if st.button("生成圖表"):
|
|
141 |
X_scaled = scaler.fit_transform(X)
|
142 |
|
143 |
# 執行K-means聚類
|
|
|
144 |
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
|
145 |
clusters = kmeans.fit_predict(X_scaled)
|
146 |
|
@@ -154,12 +134,9 @@ if st.button("生成圖表"):
|
|
154 |
y=cluster_features[1],
|
155 |
color='Cluster',
|
156 |
hover_data=['公司名稱'],
|
157 |
-
title=f"
|
158 |
)
|
159 |
st.plotly_chart(fig_scatter, use_container_width=True)
|
160 |
-
|
161 |
-
# 添加下載按鈕
|
162 |
-
st.markdown(get_image_download_link(fig_scatter, "kmeans_scatter.png", "下載K-means散點圖"), unsafe_allow_html=True)
|
163 |
|
164 |
# 顯示每個聚類的特徵
|
165 |
st.subheader("聚類特徵")
|
@@ -168,8 +145,10 @@ if st.button("生成圖表"):
|
|
168 |
|
169 |
else:
|
170 |
st.warning("請至少選擇兩個特徵進行聚類分析。")
|
|
|
|
|
171 |
|
172 |
-
st.success("
|
173 |
|
174 |
# 下載並設置自定義字體以顯示中文字符
|
175 |
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
|
|
|
8 |
import time
|
9 |
from sklearn.cluster import KMeans
|
10 |
from sklearn.preprocessing import StandardScaler
|
|
|
|
|
11 |
|
12 |
# 確保正確的中文字符編碼
|
13 |
st.set_page_config(page_title="🌳台灣中小企業ESG數據分析與揭露儀表板🌲", page_icon=":chart_with_upwards_trend:", layout="wide")
|
|
|
28 |
df = df.fillna(0)
|
29 |
return df
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# Streamlit 應用程式
|
32 |
st.title("台灣企業ESG數據分析與揭露")
|
33 |
st.subheader("以溫室氣體 X 再生能源 X 董事會資訊: https://www.tejwin.com/insight/carbon-footprint-verification/")
|
|
|
84 |
margin=dict(t=50, b=50, l=50, r=50)
|
85 |
)
|
86 |
st.plotly_chart(fig_pie, use_container_width=True)
|
|
|
|
|
|
|
87 |
|
88 |
with tab2:
|
89 |
# 使用 plotly 創建長條圖
|
|
|
104 |
height=600
|
105 |
)
|
106 |
st.plotly_chart(fig_bar, use_container_width=True)
|
|
|
|
|
|
|
107 |
|
108 |
with tab3:
|
109 |
+
if dataset_choice == "溫室氣體":
|
110 |
+
# 對溫室氣體數據進行K-means分析
|
111 |
+
st.subheader("溫室氣體數據的K-means分析")
|
|
|
|
|
112 |
|
113 |
+
# 選擇用於聚類的特徵
|
114 |
+
cluster_features = st.multiselect("選擇用於聚類的特徵", numeric_columns, default=numeric_columns[:2])
|
115 |
|
|
|
|
|
116 |
if len(cluster_features) >= 2:
|
117 |
# 準備數據
|
118 |
X = selected_df[cluster_features]
|
|
|
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 |
y=cluster_features[1],
|
135 |
color='Cluster',
|
136 |
hover_data=['公司名稱'],
|
137 |
+
title=f"溫室氣體數據的K-means聚類 ({cluster_features[0]} vs {cluster_features[1]})"
|
138 |
)
|
139 |
st.plotly_chart(fig_scatter, use_container_width=True)
|
|
|
|
|
|
|
140 |
|
141 |
# 顯示每個聚類的特徵
|
142 |
st.subheader("聚類特徵")
|
|
|
145 |
|
146 |
else:
|
147 |
st.warning("請至少選擇兩個特徵進行聚類分析。")
|
148 |
+
else:
|
149 |
+
st.info("K-means分析僅適用於溫室氣體數據集。")
|
150 |
|
151 |
+
st.success("圖表生成完成!")
|
152 |
|
153 |
# 下載並設置自定義字體以顯示中文字符
|
154 |
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
|