Rooobert commited on
Commit
65e4f78
·
verified ·
1 Parent(s): 7e5e2d9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +168 -0
app.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import plotly.express as px
3
+ import plotly.graph_objects as go
4
+ import numpy as np
5
+ from plotly.subplots import make_subplots
6
+ import streamlit as st
7
+
8
+ # 讀取數據
9
+ @st.cache_data
10
+ def load_data():
11
+ df = pd.read_csv('booking_hotels_tainan.csv')
12
+
13
+ # 數據預處理
14
+ def clean_rating(x):
15
+ if pd.isna(x) or x == '無評分':
16
+ return 0
17
+ return float(str(x).replace('分數', '').replace('分', ''))
18
+
19
+ # 清理評分數據
20
+ df['評分'] = df['評分'].apply(clean_rating)
21
+
22
+ # 創建價格區間
23
+ df['價格區間'] = pd.qcut(df['價格'],
24
+ q=3,
25
+ labels=['經濟型', '中價位', '高價位'])
26
+ return df
27
+
28
+ df = load_data()
29
+
30
+ def create_price_rating_scatter():
31
+ """價格vs評分散點圖"""
32
+ fig = px.scatter(
33
+ df,
34
+ x='價格',
35
+ y='評分',
36
+ text='飯店名稱',
37
+ size='價格', # 點的大小根據價格變化
38
+ color='評分', # 顏色根據評分變化
39
+ title='台南飯店價格與評分關係圖',
40
+ labels={'價格': '房價 (TWD)', '評分': '評分 (0-10)'}
41
+ )
42
+
43
+ fig.update_traces(
44
+ textposition='top center',
45
+ marker=dict(sizeref=2.*max(df['價格'])/(40.**2))
46
+ )
47
+
48
+ fig.update_layout(
49
+ height=600,
50
+ showlegend=True,
51
+ title_x=0.5,
52
+ title_font_size=20
53
+ )
54
+
55
+ return fig
56
+
57
+ def create_price_distribution():
58
+ """價格分布圖"""
59
+ fig = go.Figure()
60
+
61
+ # 添加直方圖
62
+ fig.add_trace(go.Histogram(
63
+ x=df['價格'],
64
+ name='價格分布',
65
+ nbinsx=10,
66
+ marker_color='rgb(55, 83, 109)'
67
+ ))
68
+
69
+ # 添加箱型圖
70
+ fig.add_trace(go.Box(
71
+ x=df['價格'],
72
+ name='價格箱型圖',
73
+ marker_color='rgb(26, 118, 255)'
74
+ ))
75
+
76
+ fig.update_layout(
77
+ title_text='台南飯店價格分布',
78
+ title_x=0.5,
79
+ title_font_size=20,
80
+ xaxis_title='價格 (TWD)',
81
+ yaxis_title='數量',
82
+ height=500,
83
+ bargap=0.2,
84
+ showlegend=True
85
+ )
86
+
87
+ return fig
88
+
89
+ def create_rating_box_by_price_range():
90
+ """不同價格區間的評分箱型圖"""
91
+ fig = px.box(
92
+ df,
93
+ x='價格區間',
94
+ y='評分',
95
+ title='不同價格區間的評分分布',
96
+ labels={'價格區間': '價格類型', '評分': '評分 (0-10)'},
97
+ color='價格區間'
98
+ )
99
+
100
+ fig.update_layout(
101
+ title_x=0.5,
102
+ title_font_size=20,
103
+ height=500,
104
+ showlegend=False
105
+ )
106
+
107
+ return fig
108
+
109
+ def create_hotel_comparison():
110
+ """飯店評分與價格比較圖"""
111
+ fig = make_subplots(specs=[[{"secondary_y": True}]])
112
+
113
+ # 排序數據
114
+ df_sorted = df.sort_values('評分', ascending=True)
115
+
116
+ # 添加評分柱狀圖
117
+ fig.add_trace(
118
+ go.Bar(
119
+ x=df_sorted['飯店名稱'],
120
+ y=df_sorted['評分'],
121
+ name="評分",
122
+ marker_color='rgb(55, 83, 109)'
123
+ )
124
+ )
125
+
126
+ # 添加價格線圖
127
+ fig.add_trace(
128
+ go.Scatter(
129
+ x=df_sorted['飯店名稱'],
130
+ y=df_sorted['價格'],
131
+ name="價格",
132
+ marker_color='rgb(26, 118, 255)'
133
+ ),
134
+ secondary_y=True
135
+ )
136
+
137
+ fig.update_layout(
138
+ title_text='台南飯店評分與價格比較',
139
+ title_x=0.5,
140
+ title_font_size=20,
141
+ height=700,
142
+ showlegend=True,
143
+ xaxis_tickangle=45
144
+ )
145
+
146
+ fig.update_yaxes(title_text="評分", secondary_y=False)
147
+ fig.update_yaxes(title_text="價格 (TWD)", secondary_y=True)
148
+
149
+ return fig
150
+
151
+ def main():
152
+ st.set_page_config(page_title="台南飯店分析", layout="wide")
153
+ st.title("台南飯店分析")
154
+
155
+ scatter_fig = create_price_rating_scatter()
156
+ dist_fig = create_price_distribution()
157
+ box_fig = create_rating_box_by_price_range()
158
+ comparison_fig = create_hotel_comparison()
159
+
160
+ st.plotly_chart(scatter_fig, use_container_width=True)
161
+ st.plotly_chart(dist_fig, use_container_width=True)
162
+ st.plotly_chart(box_fig, use_container_width=True)
163
+ st.plotly_chart(comparison_fig, use_container_width=True)
164
+
165
+ st.write("分析完成!請查看上方產生的互動式視覺化圖表。")
166
+
167
+ if __name__ == "__main__":
168
+ main()