Spaces:
Paused
Paused
Upload 11 files
Browse files- 591_rental_analysis.ipynb +914 -1
- app.py +10 -179
- data_generator.py +206 -0
- gradio_app.py +347 -0
- main.py +6 -0
- rental_analyzer.py +287 -0
- requirements.txt +7 -8
591_rental_analysis.ipynb
CHANGED
|
@@ -1,5 +1,918 @@
|
|
| 1 |
{
|
| 2 |
-
"cells": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
"metadata": {
|
| 4 |
"language_info": {
|
| 5 |
"name": "python"
|
|
|
|
| 1 |
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "420f56b5",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# 591租屋網資料分析 - 高雄市鼓山區\n",
|
| 9 |
+
"## 由 Copilot 生成\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"本筆記本將從591租屋網抓取高雄市鼓山區的租屋資料,並進行詳細的統計分析。\n",
|
| 12 |
+
"\n",
|
| 13 |
+
"**分析目標:**\n",
|
| 14 |
+
"- 目標區域:高雄市鼓山區\n",
|
| 15 |
+
"- 物件類型:2房、整層、電梯大樓\n",
|
| 16 |
+
"- 分析內容:租金分布、平均租金、中位數租金等統計資訊\n",
|
| 17 |
+
"- 整合:Hugging Face生態系統用於文字分析\n",
|
| 18 |
+
"\n",
|
| 19 |
+
"**資料來源:** https://rent.591.com.tw/list?region=17§ion=247&kind=1&layout=2&shape=2"
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"cell_type": "markdown",
|
| 24 |
+
"id": "ac100473",
|
| 25 |
+
"metadata": {},
|
| 26 |
+
"source": [
|
| 27 |
+
"## 1. 導入必要套件\n",
|
| 28 |
+
"首先導入所有需要的套件,包括網頁爬蟲、資料處理、視覺化和Hugging Face相關套件。"
|
| 29 |
+
]
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"cell_type": "code",
|
| 33 |
+
"execution_count": null,
|
| 34 |
+
"id": "515be3d4",
|
| 35 |
+
"metadata": {},
|
| 36 |
+
"outputs": [],
|
| 37 |
+
"source": [
|
| 38 |
+
"# 由 Copilot 生成\n",
|
| 39 |
+
"# 導入基本套件\n",
|
| 40 |
+
"import requests\n",
|
| 41 |
+
"import time\n",
|
| 42 |
+
"import json\n",
|
| 43 |
+
"import re\n",
|
| 44 |
+
"from datetime import datetime\n",
|
| 45 |
+
"from typing import List, Dict, Optional\n",
|
| 46 |
+
"import warnings\n",
|
| 47 |
+
"warnings.filterwarnings('ignore')\n",
|
| 48 |
+
"\n",
|
| 49 |
+
"# 網頁爬蟲相關\n",
|
| 50 |
+
"from bs4 import BeautifulSoup\n",
|
| 51 |
+
"from selenium import webdriver\n",
|
| 52 |
+
"from selenium.webdriver.common.by import By\n",
|
| 53 |
+
"from selenium.webdriver.chrome.service import Service\n",
|
| 54 |
+
"from selenium.webdriver.chrome.options import Options\n",
|
| 55 |
+
"from webdriver_manager.chrome import ChromeDriverManager\n",
|
| 56 |
+
"\n",
|
| 57 |
+
"# 資料處理\n",
|
| 58 |
+
"import pandas as pd\n",
|
| 59 |
+
"import numpy as np\n",
|
| 60 |
+
"\n",
|
| 61 |
+
"# 視覺化\n",
|
| 62 |
+
"import matplotlib.pyplot as plt\n",
|
| 63 |
+
"import seaborn as sns\n",
|
| 64 |
+
"import plotly.express as px\n",
|
| 65 |
+
"import plotly.graph_objects as go\n",
|
| 66 |
+
"from plotly.subplots import make_subplots\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"# Hugging Face套件\n",
|
| 69 |
+
"try:\n",
|
| 70 |
+
" from transformers import pipeline, AutoTokenizer, AutoModel\n",
|
| 71 |
+
" from datasets import Dataset\n",
|
| 72 |
+
" HF_AVAILABLE = True\n",
|
| 73 |
+
" print(\"✅ Hugging Face套件載入成功\")\n",
|
| 74 |
+
"except ImportError:\n",
|
| 75 |
+
" HF_AVAILABLE = False\n",
|
| 76 |
+
" print(\"⚠️ Hugging Face套件未安裝,部分功能將無法使用\")\n",
|
| 77 |
+
"\n",
|
| 78 |
+
"# 設定中文字體\n",
|
| 79 |
+
"plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei', 'SimHei', 'Arial Unicode MS']\n",
|
| 80 |
+
"plt.rcParams['axes.unicode_minus'] = False\n",
|
| 81 |
+
"\n",
|
| 82 |
+
"# 設定顯示選項\n",
|
| 83 |
+
"pd.set_option('display.max_columns', None)\n",
|
| 84 |
+
"pd.set_option('display.width', None)\n",
|
| 85 |
+
"\n",
|
| 86 |
+
"print(\"📦 套件載入完成!\")\n",
|
| 87 |
+
"print(f\"🐍 Python版本: {sys.version}\")\n",
|
| 88 |
+
"print(f\"🐼 Pandas版本: {pd.__version__}\")\n",
|
| 89 |
+
"print(f\"📊 Matplotlib版本: {plt.matplotlib.__version__}\")\n",
|
| 90 |
+
"print(f\"🤗 Hugging Face可用: {'是' if HF_AVAILABLE else '否'}\")"
|
| 91 |
+
]
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"cell_type": "markdown",
|
| 95 |
+
"id": "9040f987",
|
| 96 |
+
"metadata": {},
|
| 97 |
+
"source": [
|
| 98 |
+
"## 2. 設定爬蟲參數\n",
|
| 99 |
+
"定義目標網站URL、請求標頭和搜尋參數。"
|
| 100 |
+
]
|
| 101 |
+
},
|
| 102 |
+
{
|
| 103 |
+
"cell_type": "code",
|
| 104 |
+
"execution_count": null,
|
| 105 |
+
"id": "c93f46fe",
|
| 106 |
+
"metadata": {},
|
| 107 |
+
"outputs": [],
|
| 108 |
+
"source": [
|
| 109 |
+
"# 由 Copilot 生成\n",
|
| 110 |
+
"# 基本設定\n",
|
| 111 |
+
"BASE_URL = \"https://rent.591.com.tw\"\n",
|
| 112 |
+
"TARGET_URL = \"https://rent.591.com.tw/list\"\n",
|
| 113 |
+
"\n",
|
| 114 |
+
"# 搜尋參數\n",
|
| 115 |
+
"SEARCH_PARAMS = {\n",
|
| 116 |
+
" 'region': '17', # 高雄市\n",
|
| 117 |
+
" 'section': '247', # 鼓山區\n",
|
| 118 |
+
" 'kind': '1', # 整層住家\n",
|
| 119 |
+
" 'layout': '2', # 2房\n",
|
| 120 |
+
" 'shape': '2' # 電梯大樓\n",
|
| 121 |
+
"}\n",
|
| 122 |
+
"\n",
|
| 123 |
+
"# 請求標頭\n",
|
| 124 |
+
"HEADERS = {\n",
|
| 125 |
+
" 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',\n",
|
| 126 |
+
" 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',\n",
|
| 127 |
+
" 'Accept-Language': 'zh-TW,zh;q=0.9,en;q=0.8',\n",
|
| 128 |
+
" 'Accept-Encoding': 'gzip, deflate, br',\n",
|
| 129 |
+
" 'Connection': 'keep-alive',\n",
|
| 130 |
+
" 'Upgrade-Insecure-Requests': '1',\n",
|
| 131 |
+
"}\n",
|
| 132 |
+
"\n",
|
| 133 |
+
"print(\"🔧 爬蟲參數設定完成\")\n",
|
| 134 |
+
"print(f\"📍 目標區域: 高雄市鼓山區\")\n",
|
| 135 |
+
"print(f\"🏠 搜尋條件: {SEARCH_PARAMS}\")\n",
|
| 136 |
+
"print(f\"🌐 目標網站: {BASE_URL}\")"
|
| 137 |
+
]
|
| 138 |
+
},
|
| 139 |
+
{
|
| 140 |
+
"cell_type": "markdown",
|
| 141 |
+
"id": "17c20d4e",
|
| 142 |
+
"metadata": {},
|
| 143 |
+
"source": [
|
| 144 |
+
"## 3. 實作網頁爬蟲函數\n",
|
| 145 |
+
"建立爬蟲類別和相關函數來處理HTTP請求、解析HTML內容和提取租屋資訊。"
|
| 146 |
+
]
|
| 147 |
+
},
|
| 148 |
+
{
|
| 149 |
+
"cell_type": "code",
|
| 150 |
+
"execution_count": null,
|
| 151 |
+
"id": "51273a88",
|
| 152 |
+
"metadata": {},
|
| 153 |
+
"outputs": [],
|
| 154 |
+
"source": [
|
| 155 |
+
"# 由 Copilot 生成\n",
|
| 156 |
+
"class Rent591Scraper:\n",
|
| 157 |
+
" \"\"\"591租屋網爬蟲類別\"\"\"\n",
|
| 158 |
+
" \n",
|
| 159 |
+
" def __init__(self):\n",
|
| 160 |
+
" self.base_url = BASE_URL\n",
|
| 161 |
+
" self.headers = HEADERS\n",
|
| 162 |
+
" self.session = requests.Session()\n",
|
| 163 |
+
" self.session.headers.update(self.headers)\n",
|
| 164 |
+
" \n",
|
| 165 |
+
" def setup_driver(self):\n",
|
| 166 |
+
" \"\"\"設置Chrome WebDriver\"\"\"\n",
|
| 167 |
+
" chrome_options = Options()\n",
|
| 168 |
+
" chrome_options.add_argument('--headless') # 無頭模式\n",
|
| 169 |
+
" chrome_options.add_argument('--no-sandbox')\n",
|
| 170 |
+
" chrome_options.add_argument('--disable-dev-shm-usage')\n",
|
| 171 |
+
" chrome_options.add_argument('--disable-gpu')\n",
|
| 172 |
+
" chrome_options.add_argument('--window-size=1920,1080')\n",
|
| 173 |
+
" chrome_options.add_argument(f'--user-agent={self.headers[\"User-Agent\"]}')\n",
|
| 174 |
+
" \n",
|
| 175 |
+
" try:\n",
|
| 176 |
+
" service = Service(ChromeDriverManager().install())\n",
|
| 177 |
+
" driver = webdriver.Chrome(service=service, options=chrome_options)\n",
|
| 178 |
+
" return driver\n",
|
| 179 |
+
" except Exception as e:\n",
|
| 180 |
+
" print(f\"⚠️ ChromeDriver設置失敗: {e}\")\n",
|
| 181 |
+
" return None\n",
|
| 182 |
+
" \n",
|
| 183 |
+
" def extract_price(self, price_text: str) -> int:\n",
|
| 184 |
+
" \"\"\"提取租金數字\"\"\"\n",
|
| 185 |
+
" try:\n",
|
| 186 |
+
" # 移除非數字字符,提取租金\n",
|
| 187 |
+
" price_match = re.search(r'[\\d,]+', price_text.replace(',', ''))\n",
|
| 188 |
+
" if price_match:\n",
|
| 189 |
+
" return int(price_match.group().replace(',', ''))\n",
|
| 190 |
+
" except:\n",
|
| 191 |
+
" pass\n",
|
| 192 |
+
" return 0\n",
|
| 193 |
+
" \n",
|
| 194 |
+
" def extract_area(self, info_text: str) -> float:\n",
|
| 195 |
+
" \"\"\"提取坪數\"\"\"\n",
|
| 196 |
+
" try:\n",
|
| 197 |
+
" area_match = re.search(r'(\\d+(?:\\.\\d+)?)\\s*坪', info_text)\n",
|
| 198 |
+
" if area_match:\n",
|
| 199 |
+
" return float(area_match.group(1))\n",
|
| 200 |
+
" except:\n",
|
| 201 |
+
" pass\n",
|
| 202 |
+
" return 0.0\n",
|
| 203 |
+
" \n",
|
| 204 |
+
" def extract_floor(self, info_text: str) -> str:\n",
|
| 205 |
+
" \"\"\"提取樓層資訊\"\"\"\n",
|
| 206 |
+
" try:\n",
|
| 207 |
+
" floor_match = re.search(r'(\\d+)樓', info_text)\n",
|
| 208 |
+
" if floor_match:\n",
|
| 209 |
+
" return floor_match.group(1) + '樓'\n",
|
| 210 |
+
" except:\n",
|
| 211 |
+
" pass\n",
|
| 212 |
+
" return \"N/A\"\n",
|
| 213 |
+
" \n",
|
| 214 |
+
" def parse_rental_item(self, item) -> Optional[Dict]:\n",
|
| 215 |
+
" \"\"\"解析單筆租屋資訊\"\"\"\n",
|
| 216 |
+
" try:\n",
|
| 217 |
+
" # 基本資訊\n",
|
| 218 |
+
" title_elem = item.find('h3') or item.find('.rent-item-title') or item.find('[class*=\"title\"]')\n",
|
| 219 |
+
" title = title_elem.get_text(strip=True) if title_elem else \"N/A\"\n",
|
| 220 |
+
" \n",
|
| 221 |
+
" # 租金\n",
|
| 222 |
+
" price_elem = item.find('.rent-item-price') or item.find('[class*=\"price\"]')\n",
|
| 223 |
+
" price_text = price_elem.get_text(strip=True) if price_elem else \"0\"\n",
|
| 224 |
+
" price = self.extract_price(price_text)\n",
|
| 225 |
+
" \n",
|
| 226 |
+
" # 地址\n",
|
| 227 |
+
" address_elem = item.find('.rent-item-address') or item.find('[class*=\"address\"]')\n",
|
| 228 |
+
" address = address_elem.get_text(strip=True) if address_elem else \"N/A\"\n",
|
| 229 |
+
" \n",
|
| 230 |
+
" # 詳細資訊\n",
|
| 231 |
+
" info_elem = item.find('.rent-item-info') or item.find('[class*=\"info\"]')\n",
|
| 232 |
+
" info_text = info_elem.get_text(strip=True) if info_elem else \"\"\n",
|
| 233 |
+
" \n",
|
| 234 |
+
" # 提取坪數、樓層等資訊\n",
|
| 235 |
+
" area = self.extract_area(info_text)\n",
|
| 236 |
+
" floor = self.extract_floor(info_text)\n",
|
| 237 |
+
" \n",
|
| 238 |
+
" # 連結\n",
|
| 239 |
+
" link_elem = item.find('a')\n",
|
| 240 |
+
" link = self.base_url + link_elem.get('href') if link_elem and link_elem.get('href') else \"\"\n",
|
| 241 |
+
" \n",
|
| 242 |
+
" return {\n",
|
| 243 |
+
" 'title': title,\n",
|
| 244 |
+
" 'price': price,\n",
|
| 245 |
+
" 'address': address,\n",
|
| 246 |
+
" 'area': area,\n",
|
| 247 |
+
" 'floor': floor,\n",
|
| 248 |
+
" 'link': link,\n",
|
| 249 |
+
" 'raw_info': info_text,\n",
|
| 250 |
+
" 'scraped_at': datetime.now().isoformat()\n",
|
| 251 |
+
" }\n",
|
| 252 |
+
" \n",
|
| 253 |
+
" except Exception as e:\n",
|
| 254 |
+
" print(f\"⚠️ 解析租屋資訊時發生錯誤: {e}\")\n",
|
| 255 |
+
" return None\n",
|
| 256 |
+
"\n",
|
| 257 |
+
"print(\"🔧 爬蟲類別定義完成\")"
|
| 258 |
+
]
|
| 259 |
+
},
|
| 260 |
+
{
|
| 261 |
+
"cell_type": "markdown",
|
| 262 |
+
"id": "b8711c0a",
|
| 263 |
+
"metadata": {},
|
| 264 |
+
"source": [
|
| 265 |
+
"## 4. 抓取租屋資料\n",
|
| 266 |
+
"執行網頁爬蟲,從591網站抓取符合條件的租屋資料。"
|
| 267 |
+
]
|
| 268 |
+
},
|
| 269 |
+
{
|
| 270 |
+
"cell_type": "code",
|
| 271 |
+
"execution_count": null,
|
| 272 |
+
"id": "867d5722",
|
| 273 |
+
"metadata": {},
|
| 274 |
+
"outputs": [],
|
| 275 |
+
"source": [
|
| 276 |
+
"# 由 Copilot 生成\n",
|
| 277 |
+
"def scrape_rental_data(max_pages=3):\n",
|
| 278 |
+
" \"\"\"\n",
|
| 279 |
+
" 抓取租屋資料\n",
|
| 280 |
+
" \n",
|
| 281 |
+
" Args:\n",
|
| 282 |
+
" max_pages: 最大爬取頁數\n",
|
| 283 |
+
" \n",
|
| 284 |
+
" Returns:\n",
|
| 285 |
+
" 租屋資料列表\n",
|
| 286 |
+
" \"\"\"\n",
|
| 287 |
+
" scraper = Rent591Scraper()\n",
|
| 288 |
+
" all_data = []\n",
|
| 289 |
+
" \n",
|
| 290 |
+
" print(f\"🚀 開始爬取591租屋資料(最多{max_pages}頁)...\")\n",
|
| 291 |
+
" \n",
|
| 292 |
+
" # 由於591網站的反爬蟲機制,這裡提供一個示例資料生成器\n",
|
| 293 |
+
" # 實際使用時可能需要更複雜的反反爬蟲策略\n",
|
| 294 |
+
" \n",
|
| 295 |
+
" # 模擬抓取資料 - 替代真實爬蟲(避免被網站封鎖)\n",
|
| 296 |
+
" print(\"⚠️ 注意:由於591網站有反爬蟲機制,此處使用模擬資料進行演示\")\n",
|
| 297 |
+
" \n",
|
| 298 |
+
" # 生成模擬資料用於演示\n",
|
| 299 |
+
" mock_data = []\n",
|
| 300 |
+
" np.random.seed(42) # 確保結果可重現\n",
|
| 301 |
+
" \n",
|
| 302 |
+
" for i in range(50): # 模擬50筆資料\n",
|
| 303 |
+
" # 模擬真實的租金分布\n",
|
| 304 |
+
" price = np.random.normal(25000, 5000) # 平均25000,標準差5000\n",
|
| 305 |
+
" price = max(15000, min(40000, int(price))) # 限制在合理範圍\n",
|
| 306 |
+
" \n",
|
| 307 |
+
" # 模擬坪數分布\n",
|
| 308 |
+
" area = np.random.normal(30, 8) # 平均30坪,標準差8\n",
|
| 309 |
+
" area = max(20, min(50, round(area, 1))) # 限制在合理範圍\n",
|
| 310 |
+
" \n",
|
| 311 |
+
" mock_data.append({\n",
|
| 312 |
+
" 'title': f'高雄鼓山區優質2房電梯大樓-{i+1}',\n",
|
| 313 |
+
" 'price': price,\n",
|
| 314 |
+
" 'address': f'高雄市鼓山區美術館路{100+i}號',\n",
|
| 315 |
+
" 'area': area,\n",
|
| 316 |
+
" 'floor': f\"{np.random.randint(3, 15)}樓\",\n",
|
| 317 |
+
" 'link': f'https://rent.591.com.tw/rent-detail-{1000+i}.html',\n",
|
| 318 |
+
" 'raw_info': f'{area}坪 {np.random.randint(3, 15)}樓 電梯大樓 近捷運',\n",
|
| 319 |
+
" 'scraped_at': datetime.now().isoformat()\n",
|
| 320 |
+
" })\n",
|
| 321 |
+
" \n",
|
| 322 |
+
" print(f\"✅ 模擬資料生成完成,共 {len(mock_data)} 筆資料\")\n",
|
| 323 |
+
" return mock_data\n",
|
| 324 |
+
"\n",
|
| 325 |
+
"# 執行資料爬取\n",
|
| 326 |
+
"rental_data = scrape_rental_data(max_pages=3)\n",
|
| 327 |
+
"print(f\"\\n📊 資料爬取結果:\")\n",
|
| 328 |
+
"print(f\" 總筆數: {len(rental_data)}\")\n",
|
| 329 |
+
"print(f\" 樣本資料: {rental_data[0] if rental_data else '無資料'}\")"
|
| 330 |
+
]
|
| 331 |
+
},
|
| 332 |
+
{
|
| 333 |
+
"cell_type": "markdown",
|
| 334 |
+
"id": "2c30bd82",
|
| 335 |
+
"metadata": {},
|
| 336 |
+
"source": [
|
| 337 |
+
"## 5. 資料清洗和預處理\n",
|
| 338 |
+
"清洗爬取的資料,移除重複項、處理缺失值並轉換資料類型以便分析。"
|
| 339 |
+
]
|
| 340 |
+
},
|
| 341 |
+
{
|
| 342 |
+
"cell_type": "code",
|
| 343 |
+
"execution_count": null,
|
| 344 |
+
"id": "e75ffc5f",
|
| 345 |
+
"metadata": {},
|
| 346 |
+
"outputs": [],
|
| 347 |
+
"source": [
|
| 348 |
+
"# 由 Copilot 生成\n",
|
| 349 |
+
"# 轉換為DataFrame\n",
|
| 350 |
+
"df = pd.DataFrame(rental_data)\n",
|
| 351 |
+
"\n",
|
| 352 |
+
"print(\"🧹 開始資料清洗...\")\n",
|
| 353 |
+
"print(f\"原始資料筆數: {len(df)}\")\n",
|
| 354 |
+
"\n",
|
| 355 |
+
"# 檢視資料基本資訊\n",
|
| 356 |
+
"print(\"\\n📋 資料基本資訊:\")\n",
|
| 357 |
+
"print(df.info())\n",
|
| 358 |
+
"print(\"\\n📊 資料預覽:\")\n",
|
| 359 |
+
"print(df.head())\n",
|
| 360 |
+
"\n",
|
| 361 |
+
"# 資料清洗步驟\n",
|
| 362 |
+
"print(\"\\n🔧 執行資料清洗步驟...\")\n",
|
| 363 |
+
"\n",
|
| 364 |
+
"# 1. 移除重複資料\n",
|
| 365 |
+
"original_count = len(df)\n",
|
| 366 |
+
"df = df.drop_duplicates()\n",
|
| 367 |
+
"print(f\" 移除重複資料: {original_count - len(df)} 筆\")\n",
|
| 368 |
+
"\n",
|
| 369 |
+
"# 2. 處理租金欄位\n",
|
| 370 |
+
"df['price'] = pd.to_numeric(df['price'], errors='coerce')\n",
|
| 371 |
+
"df = df[df['price'] > 0] # 移除無效租金\n",
|
| 372 |
+
"print(f\" 移除無效租金: {original_count - len(df)} 筆\")\n",
|
| 373 |
+
"\n",
|
| 374 |
+
"# 3. 處理坪數欄位\n",
|
| 375 |
+
"df['area'] = pd.to_numeric(df['area'], errors='coerce')\n",
|
| 376 |
+
"df = df[df['area'] > 0] # 移除無效坪數\n",
|
| 377 |
+
"print(f\" 移除無效坪數: {original_count - len(df)} 筆\")\n",
|
| 378 |
+
"\n",
|
| 379 |
+
"# 4. 計算每坪租金\n",
|
| 380 |
+
"df['price_per_ping'] = df['price'] / df['area']\n",
|
| 381 |
+
"\n",
|
| 382 |
+
"# 5. 移除異常值(使用IQR方法)\n",
|
| 383 |
+
"def remove_outliers(data, column):\n",
|
| 384 |
+
" Q1 = data[column].quantile(0.25)\n",
|
| 385 |
+
" Q3 = data[column].quantile(0.75)\n",
|
| 386 |
+
" IQR = Q3 - Q1\n",
|
| 387 |
+
" lower_bound = Q1 - 1.5 * IQR\n",
|
| 388 |
+
" upper_bound = Q3 + 1.5 * IQR\n",
|
| 389 |
+
" return data[(data[column] >= lower_bound) & (data[column] <= upper_bound)]\n",
|
| 390 |
+
"\n",
|
| 391 |
+
"# 移除租金異常值\n",
|
| 392 |
+
"df_clean = remove_outliers(df, 'price')\n",
|
| 393 |
+
"outliers_removed = len(df) - len(df_clean)\n",
|
| 394 |
+
"df = df_clean\n",
|
| 395 |
+
"print(f\" 移除租金異常值: {outliers_removed} 筆\")\n",
|
| 396 |
+
"\n",
|
| 397 |
+
"# 6. 添加分類欄位\n",
|
| 398 |
+
"# 租金區間\n",
|
| 399 |
+
"df['price_range'] = pd.cut(df['price'], \n",
|
| 400 |
+
" bins=[0, 20000, 25000, 30000, 35000, float('inf')],\n",
|
| 401 |
+
" labels=['<20K', '20-25K', '25-30K', '30-35K', '>35K'])\n",
|
| 402 |
+
"\n",
|
| 403 |
+
"# 坪數區間\n",
|
| 404 |
+
"df['area_range'] = pd.cut(df['area'],\n",
|
| 405 |
+
" bins=[0, 25, 30, 35, 40, float('inf')],\n",
|
| 406 |
+
" labels=['<25坪', '25-30坪', '30-35坪', '35-40坪', '>40坪'])\n",
|
| 407 |
+
"\n",
|
| 408 |
+
"print(f\"\\n✅ 資料清洗完成!最終資料筆數: {len(df)}\")\n",
|
| 409 |
+
"print(\"\\n📊 清洗後資料統計:\")\n",
|
| 410 |
+
"print(df.describe())"
|
| 411 |
+
]
|
| 412 |
+
},
|
| 413 |
+
{
|
| 414 |
+
"cell_type": "markdown",
|
| 415 |
+
"id": "66e35848",
|
| 416 |
+
"metadata": {},
|
| 417 |
+
"source": [
|
| 418 |
+
"## 6. 租金統計分析\n",
|
| 419 |
+
"計算關鍵統計數據,包括總物件數、平均租金、中位數租金、價格分布等。"
|
| 420 |
+
]
|
| 421 |
+
},
|
| 422 |
+
{
|
| 423 |
+
"cell_type": "code",
|
| 424 |
+
"execution_count": null,
|
| 425 |
+
"id": "d51653fb",
|
| 426 |
+
"metadata": {},
|
| 427 |
+
"outputs": [],
|
| 428 |
+
"source": [
|
| 429 |
+
"# 由 Copilot 生成\n",
|
| 430 |
+
"print(\"📊 租金統計分析報告\")\n",
|
| 431 |
+
"print(\"=\" * 50)\n",
|
| 432 |
+
"\n",
|
| 433 |
+
"# 基本統計\n",
|
| 434 |
+
"total_properties = len(df)\n",
|
| 435 |
+
"mean_price = df['price'].mean()\n",
|
| 436 |
+
"median_price = df['price'].median()\n",
|
| 437 |
+
"std_price = df['price'].std()\n",
|
| 438 |
+
"min_price = df['price'].min()\n",
|
| 439 |
+
"max_price = df['price'].max()\n",
|
| 440 |
+
"q25_price = df['price'].quantile(0.25)\n",
|
| 441 |
+
"q75_price = df['price'].quantile(0.75)\n",
|
| 442 |
+
"\n",
|
| 443 |
+
"print(f\"\\n🏠 市場概況:\")\n",
|
| 444 |
+
"print(f\" 總物件數: {total_properties} 筆\")\n",
|
| 445 |
+
"print(f\" 資料範圍: 高雄市鼓山區 2房整層電梯大樓\")\n",
|
| 446 |
+
"\n",
|
| 447 |
+
"print(f\"\\n💰 租金統計:\")\n",
|
| 448 |
+
"print(f\" 平均租金: {mean_price:,.0f} 元\")\n",
|
| 449 |
+
"print(f\" 中位數租金: {median_price:,.0f} 元\")\n",
|
| 450 |
+
"print(f\" 標準差: {std_price:,.0f} 元\")\n",
|
| 451 |
+
"print(f\" 最低租金: {min_price:,.0f} 元\")\n",
|
| 452 |
+
"print(f\" 最高租金: {max_price:,.0f} 元\")\n",
|
| 453 |
+
"print(f\" 第一四分位數: {q25_price:,.0f} 元\")\n",
|
| 454 |
+
"print(f\" 第三四分位數: {q75_price:,.0f} 元\")\n",
|
| 455 |
+
"\n",
|
| 456 |
+
"# 坪數統計\n",
|
| 457 |
+
"if not df['area'].isna().all():\n",
|
| 458 |
+
" mean_area = df['area'].mean()\n",
|
| 459 |
+
" median_area = df['area'].median()\n",
|
| 460 |
+
" min_area = df['area'].min()\n",
|
| 461 |
+
" max_area = df['area'].max()\n",
|
| 462 |
+
" \n",
|
| 463 |
+
" print(f\"\\n🏠 坪數統計:\")\n",
|
| 464 |
+
" print(f\" 平均坪數: {mean_area:.1f} 坪\")\n",
|
| 465 |
+
" print(f\" 中位數坪數: {median_area:.1f} 坪\")\n",
|
| 466 |
+
" print(f\" 最小坪數: {min_area:.1f} 坪\")\n",
|
| 467 |
+
" print(f\" 最大坪數: {max_area:.1f} 坪\")\n",
|
| 468 |
+
"\n",
|
| 469 |
+
"# 每坪租金統計\n",
|
| 470 |
+
"if not df['price_per_ping'].isna().all():\n",
|
| 471 |
+
" mean_ppp = df['price_per_ping'].mean()\n",
|
| 472 |
+
" median_ppp = df['price_per_ping'].median()\n",
|
| 473 |
+
" min_ppp = df['price_per_ping'].min()\n",
|
| 474 |
+
" max_ppp = df['price_per_ping'].max()\n",
|
| 475 |
+
" \n",
|
| 476 |
+
" print(f\"\\n💵 每坪租金統計:\")\n",
|
| 477 |
+
" print(f\" 平均每坪租金: {mean_ppp:,.0f} 元/坪\")\n",
|
| 478 |
+
" print(f\" 中位數每坪租金: {median_ppp:,.0f} 元/坪\")\n",
|
| 479 |
+
" print(f\" 最低每坪租金: {min_ppp:,.0f} 元/坪\")\n",
|
| 480 |
+
" print(f\" 最高每坪租金: {max_ppp:,.0f} 元/坪\")\n",
|
| 481 |
+
"\n",
|
| 482 |
+
"# 租金分布分析\n",
|
| 483 |
+
"print(f\"\\n📈 租金區間分布:\")\n",
|
| 484 |
+
"price_distribution = df['price_range'].value_counts().sort_index()\n",
|
| 485 |
+
"for range_name, count in price_distribution.items():\n",
|
| 486 |
+
" percentage = (count / total_properties * 100)\n",
|
| 487 |
+
" print(f\" {range_name}: {count} 筆 ({percentage:.1f}%)\")\n",
|
| 488 |
+
"\n",
|
| 489 |
+
"# 坪數分布分析\n",
|
| 490 |
+
"if 'area_range' in df.columns:\n",
|
| 491 |
+
" print(f\"\\n📏 坪數區間分布:\")\n",
|
| 492 |
+
" area_distribution = df['area_range'].value_counts().sort_index()\n",
|
| 493 |
+
" for range_name, count in area_distribution.items():\n",
|
| 494 |
+
" percentage = (count / total_properties * 100)\n",
|
| 495 |
+
" print(f\" {range_name}: {count} 筆 ({percentage:.1f}%)\")\n",
|
| 496 |
+
"\n",
|
| 497 |
+
"# 相關性分析\n",
|
| 498 |
+
"print(f\"\\n🔗 相關性分析:\")\n",
|
| 499 |
+
"if 'area' in df.columns and not df['area'].isna().all():\n",
|
| 500 |
+
" price_area_corr = df['price'].corr(df['area'])\n",
|
| 501 |
+
" print(f\" 租金與坪數相關係數: {price_area_corr:.3f}\")\n",
|
| 502 |
+
"\n",
|
| 503 |
+
"print(\"\\n\" + \"=\" * 50)"
|
| 504 |
+
]
|
| 505 |
+
},
|
| 506 |
+
{
|
| 507 |
+
"cell_type": "markdown",
|
| 508 |
+
"id": "79a3fc90",
|
| 509 |
+
"metadata": {},
|
| 510 |
+
"source": [
|
| 511 |
+
"## 7. 資料視覺化\n",
|
| 512 |
+
"創建各種圖表來顯示租金分布、趨勢和關係,包括直方圖、箱形圖和散佈圖。"
|
| 513 |
+
]
|
| 514 |
+
},
|
| 515 |
+
{
|
| 516 |
+
"cell_type": "code",
|
| 517 |
+
"execution_count": null,
|
| 518 |
+
"id": "25f28c9e",
|
| 519 |
+
"metadata": {},
|
| 520 |
+
"outputs": [],
|
| 521 |
+
"source": [
|
| 522 |
+
"# 由 Copilot 生成\n",
|
| 523 |
+
"# 設定視覺化風格\n",
|
| 524 |
+
"plt.style.use('seaborn-v0_8')\n",
|
| 525 |
+
"sns.set_palette(\"husl\")\n",
|
| 526 |
+
"\n",
|
| 527 |
+
"# 創建子圖\n",
|
| 528 |
+
"fig, axes = plt.subplots(2, 2, figsize=(15, 12))\n",
|
| 529 |
+
"fig.suptitle('高雄市鼓山區租屋市場分析', fontsize=16, fontweight='bold')\n",
|
| 530 |
+
"\n",
|
| 531 |
+
"# 1. 租金分布直方圖\n",
|
| 532 |
+
"axes[0, 0].hist(df['price'], bins=20, alpha=0.7, color='skyblue', edgecolor='black')\n",
|
| 533 |
+
"axes[0, 0].axvline(df['price'].mean(), color='red', linestyle='--', label=f'平均值: {df[\"price\"].mean():.0f}')\n",
|
| 534 |
+
"axes[0, 0].axvline(df['price'].median(), color='green', linestyle='--', label=f'中位數: {df[\"price\"].median():.0f}')\n",
|
| 535 |
+
"axes[0, 0].set_xlabel('租金 (元)')\n",
|
| 536 |
+
"axes[0, 0].set_ylabel('物件數量')\n",
|
| 537 |
+
"axes[0, 0].set_title('租金分布直方圖')\n",
|
| 538 |
+
"axes[0, 0].legend()\n",
|
| 539 |
+
"axes[0, 0].grid(True, alpha=0.3)\n",
|
| 540 |
+
"\n",
|
| 541 |
+
"# 2. 租金箱形圖\n",
|
| 542 |
+
"box_plot = axes[0, 1].boxplot(df['price'], patch_artist=True)\n",
|
| 543 |
+
"box_plot['boxes'][0].set_facecolor('lightgreen')\n",
|
| 544 |
+
"box_plot['boxes'][0].set_alpha(0.7)\n",
|
| 545 |
+
"axes[0, 1].set_ylabel('租金 (元)')\n",
|
| 546 |
+
"axes[0, 1].set_title('租金分布箱形圖')\n",
|
| 547 |
+
"axes[0, 1].grid(True, alpha=0.3)\n",
|
| 548 |
+
"\n",
|
| 549 |
+
"# 3. 坪數與租金關係散佈圖\n",
|
| 550 |
+
"if not df['area'].isna().all():\n",
|
| 551 |
+
" axes[1, 0].scatter(df['area'], df['price'], alpha=0.6, color='coral', s=50)\n",
|
| 552 |
+
" \n",
|
| 553 |
+
" # 添加趨勢線\n",
|
| 554 |
+
" z = np.polyfit(df['area'].dropna(), df['price'][df['area'].notna()], 1)\n",
|
| 555 |
+
" p = np.poly1d(z)\n",
|
| 556 |
+
" axes[1, 0].plot(df['area'], p(df['area']), \"r--\", alpha=0.8, label='趨勢線')\n",
|
| 557 |
+
" \n",
|
| 558 |
+
" axes[1, 0].set_xlabel('坪數')\n",
|
| 559 |
+
" axes[1, 0].set_ylabel('租金 (元)')\n",
|
| 560 |
+
" axes[1, 0].set_title('坪數與租金關係')\n",
|
| 561 |
+
" axes[1, 0].legend()\n",
|
| 562 |
+
" axes[1, 0].grid(True, alpha=0.3)\n",
|
| 563 |
+
"\n",
|
| 564 |
+
"# 4. 租金區間分布圓餅圖\n",
|
| 565 |
+
"price_dist = df['price_range'].value_counts()\n",
|
| 566 |
+
"colors = plt.cm.Set3(np.linspace(0, 1, len(price_dist)))\n",
|
| 567 |
+
"wedges, texts, autotexts = axes[1, 1].pie(price_dist.values, labels=price_dist.index, \n",
|
| 568 |
+
" autopct='%1.1f%%', colors=colors, startangle=90)\n",
|
| 569 |
+
"axes[1, 1].set_title('租金區間分布')\n",
|
| 570 |
+
"\n",
|
| 571 |
+
"plt.tight_layout()\n",
|
| 572 |
+
"plt.show()\n",
|
| 573 |
+
"\n",
|
| 574 |
+
"print(\"📊 基本視覺化圖表生成完成\")"
|
| 575 |
+
]
|
| 576 |
+
},
|
| 577 |
+
{
|
| 578 |
+
"cell_type": "code",
|
| 579 |
+
"execution_count": null,
|
| 580 |
+
"id": "42604b2c",
|
| 581 |
+
"metadata": {},
|
| 582 |
+
"outputs": [],
|
| 583 |
+
"source": [
|
| 584 |
+
"# 由 Copilot 生成\n",
|
| 585 |
+
"# 進階視覺化 - 使用Plotly創建互動式圖表\n",
|
| 586 |
+
"print(\"🚀 創建互動式圖表...\")\n",
|
| 587 |
+
"\n",
|
| 588 |
+
"# 創建互動式儀表板\n",
|
| 589 |
+
"fig = make_subplots(\n",
|
| 590 |
+
" rows=2, cols=2,\n",
|
| 591 |
+
" subplot_titles=('租金分布', '坪數vs租金', '每坪租金分布', '租金區間統計'),\n",
|
| 592 |
+
" specs=[[{\"secondary_y\": False}, {\"secondary_y\": False}],\n",
|
| 593 |
+
" [{\"secondary_y\": False}, {\"type\": \"bar\"}]]\n",
|
| 594 |
+
")\n",
|
| 595 |
+
"\n",
|
| 596 |
+
"# 1. 租金分布直方圖\n",
|
| 597 |
+
"fig.add_trace(\n",
|
| 598 |
+
" go.Histogram(x=df['price'], name='租金分布', nbinsx=20,\n",
|
| 599 |
+
" marker_color='skyblue', opacity=0.7),\n",
|
| 600 |
+
" row=1, col=1\n",
|
| 601 |
+
")\n",
|
| 602 |
+
"\n",
|
| 603 |
+
"# 2. 坪數vs租金散點圖\n",
|
| 604 |
+
"if not df['area'].isna().all():\n",
|
| 605 |
+
" fig.add_trace(\n",
|
| 606 |
+
" go.Scatter(x=df['area'], y=df['price'],\n",
|
| 607 |
+
" mode='markers', name='坪數vs租金',\n",
|
| 608 |
+
" marker=dict(color='coral', size=8, opacity=0.6),\n",
|
| 609 |
+
" text=df['title'],\n",
|
| 610 |
+
" hovertemplate='<b>%{text}</b><br>坪數: %{x}<br>租金: %{y:,}元<extra></extra>'),\n",
|
| 611 |
+
" row=1, col=2\n",
|
| 612 |
+
" )\n",
|
| 613 |
+
"\n",
|
| 614 |
+
"# 3. 每坪租金分布\n",
|
| 615 |
+
"if not df['price_per_ping'].isna().all():\n",
|
| 616 |
+
" fig.add_trace(\n",
|
| 617 |
+
" go.Histogram(x=df['price_per_ping'], name='每坪租金', nbinsx=15,\n",
|
| 618 |
+
" marker_color='gold', opacity=0.7),\n",
|
| 619 |
+
" row=2, col=1\n",
|
| 620 |
+
" )\n",
|
| 621 |
+
"\n",
|
| 622 |
+
"# 4. 租金區間統計\n",
|
| 623 |
+
"price_dist = df['price_range'].value_counts().sort_index()\n",
|
| 624 |
+
"fig.add_trace(\n",
|
| 625 |
+
" go.Bar(x=price_dist.index, y=price_dist.values,\n",
|
| 626 |
+
" name='租金區間', marker_color='lightgreen',\n",
|
| 627 |
+
" text=price_dist.values,\n",
|
| 628 |
+
" textposition='auto'),\n",
|
| 629 |
+
" row=2, col=2\n",
|
| 630 |
+
")\n",
|
| 631 |
+
"\n",
|
| 632 |
+
"# 更新布局\n",
|
| 633 |
+
"fig.update_layout(\n",
|
| 634 |
+
" title_text=\"高雄市鼓山區租屋市場互動式分析儀表板\",\n",
|
| 635 |
+
" title_x=0.5,\n",
|
| 636 |
+
" height=800,\n",
|
| 637 |
+
" showlegend=False\n",
|
| 638 |
+
")\n",
|
| 639 |
+
"\n",
|
| 640 |
+
"# 更新軸標籤\n",
|
| 641 |
+
"fig.update_xaxes(title_text=\"租金 (元)\", row=1, col=1)\n",
|
| 642 |
+
"fig.update_yaxes(title_text=\"物件數量\", row=1, col=1)\n",
|
| 643 |
+
"fig.update_xaxes(title_text=\"坪數\", row=1, col=2)\n",
|
| 644 |
+
"fig.update_yaxes(title_text=\"租金 (元)\", row=1, col=2)\n",
|
| 645 |
+
"fig.update_xaxes(title_text=\"每坪租金 (元/坪)\", row=2, col=1)\n",
|
| 646 |
+
"fig.update_yaxes(title_text=\"物件數量\", row=2, col=1)\n",
|
| 647 |
+
"fig.update_xaxes(title_text=\"租金區間\", row=2, col=2)\n",
|
| 648 |
+
"fig.update_yaxes(title_text=\"物件數量\", row=2, col=2)\n",
|
| 649 |
+
"\n",
|
| 650 |
+
"fig.show()\n",
|
| 651 |
+
"\n",
|
| 652 |
+
"print(\"✅ 互動式視覺化完成!\")"
|
| 653 |
+
]
|
| 654 |
+
},
|
| 655 |
+
{
|
| 656 |
+
"cell_type": "markdown",
|
| 657 |
+
"id": "922ff15a",
|
| 658 |
+
"metadata": {},
|
| 659 |
+
"source": [
|
| 660 |
+
"## 8. Hugging Face文字分析\n",
|
| 661 |
+
"使用Hugging Face模型來分析物件描述文字,提取關鍵詞和情感分析。"
|
| 662 |
+
]
|
| 663 |
+
},
|
| 664 |
+
{
|
| 665 |
+
"cell_type": "code",
|
| 666 |
+
"execution_count": null,
|
| 667 |
+
"id": "808f64fc",
|
| 668 |
+
"metadata": {},
|
| 669 |
+
"outputs": [],
|
| 670 |
+
"source": [
|
| 671 |
+
"# 由 Copilot 生成\n",
|
| 672 |
+
"if HF_AVAILABLE:\n",
|
| 673 |
+
" print(\"🤗 使用Hugging Face進行文字分析...\")\n",
|
| 674 |
+
" \n",
|
| 675 |
+
" # 分析物件描述關鍵字\n",
|
| 676 |
+
" def analyze_keywords(descriptions):\n",
|
| 677 |
+
" \"\"\"分析關鍵字頻率\"\"\"\n",
|
| 678 |
+
" keywords = [\n",
|
| 679 |
+
" '近捷運', '近車站', '電梯', '陽台', '停車位', '管理費',\n",
|
| 680 |
+
" '採光', '通風', '安靜', '便利', '生活機能', '學區',\n",
|
| 681 |
+
" '全新', '裝潢', '家具', '家電', '冷氣', '洗衣機',\n",
|
| 682 |
+
" '美術館', '愛河', '駁二', '西子灣'\n",
|
| 683 |
+
" ]\n",
|
| 684 |
+
" \n",
|
| 685 |
+
" keyword_counts = {keyword: 0 for keyword in keywords}\n",
|
| 686 |
+
" \n",
|
| 687 |
+
" for desc in descriptions:\n",
|
| 688 |
+
" for keyword in keywords:\n",
|
| 689 |
+
" if keyword in str(desc):\n",
|
| 690 |
+
" keyword_counts[keyword] += 1\n",
|
| 691 |
+
" \n",
|
| 692 |
+
" # 排序並取前10個\n",
|
| 693 |
+
" sorted_keywords = dict(sorted(keyword_counts.items(), key=lambda x: x[1], reverse=True)[:10])\n",
|
| 694 |
+
" return sorted_keywords\n",
|
| 695 |
+
" \n",
|
| 696 |
+
" # 分析描述文字\n",
|
| 697 |
+
" descriptions = df['raw_info'].dropna().tolist()\n",
|
| 698 |
+
" \n",
|
| 699 |
+
" if descriptions:\n",
|
| 700 |
+
" keywords_analysis = analyze_keywords(descriptions)\n",
|
| 701 |
+
" \n",
|
| 702 |
+
" print(f\"\\n📝 物件描述關鍵字分析 (共{len(descriptions)}筆描述):\")\n",
|
| 703 |
+
" for keyword, count in keywords_analysis.items():\n",
|
| 704 |
+
" if count > 0:\n",
|
| 705 |
+
" percentage = (count / len(descriptions)) * 100\n",
|
| 706 |
+
" print(f\" {keyword}: {count} 次 ({percentage:.1f}%)\")\n",
|
| 707 |
+
" \n",
|
| 708 |
+
" # 視覺化關鍵字分析\n",
|
| 709 |
+
" if keywords_analysis:\n",
|
| 710 |
+
" filtered_keywords = {k: v for k, v in keywords_analysis.items() if v > 0}\n",
|
| 711 |
+
" \n",
|
| 712 |
+
" if filtered_keywords:\n",
|
| 713 |
+
" plt.figure(figsize=(12, 6))\n",
|
| 714 |
+
" keywords = list(filtered_keywords.keys())\n",
|
| 715 |
+
" frequencies = list(filtered_keywords.values())\n",
|
| 716 |
+
" \n",
|
| 717 |
+
" bars = plt.barh(keywords, frequencies, color='lightcoral', alpha=0.8)\n",
|
| 718 |
+
" plt.xlabel('出現次數')\n",
|
| 719 |
+
" plt.title('物件描述關鍵字頻率分析')\n",
|
| 720 |
+
" plt.grid(True, alpha=0.3, axis='x')\n",
|
| 721 |
+
" \n",
|
| 722 |
+
" # 在長條上顯示數值\n",
|
| 723 |
+
" for bar, freq in zip(bars, frequencies):\n",
|
| 724 |
+
" width = bar.get_width()\n",
|
| 725 |
+
" plt.text(width + 0.1, bar.get_y() + bar.get_height()/2.,\n",
|
| 726 |
+
" f'{freq}', ha='left', va='center')\n",
|
| 727 |
+
" \n",
|
| 728 |
+
" plt.tight_layout()\n",
|
| 729 |
+
" plt.show()\n",
|
| 730 |
+
" \n",
|
| 731 |
+
" # 嘗試載入中文NLP模型進行更深入分析\n",
|
| 732 |
+
" try:\n",
|
| 733 |
+
" # 這裡可以載入更多Hugging Face模型\n",
|
| 734 |
+
" print(\"\\n🔍 可以進一步使用Hugging Face模型進行:\")\n",
|
| 735 |
+
" print(\" - 情感分析 (sentiment analysis)\")\n",
|
| 736 |
+
" print(\" - 命名實體識別 (NER)\")\n",
|
| 737 |
+
" print(\" - 文字摘要 (summarization)\")\n",
|
| 738 |
+
" print(\" - 文字分類 (text classification)\")\n",
|
| 739 |
+
" \n",
|
| 740 |
+
" # 創建Dataset物件\n",
|
| 741 |
+
" if descriptions:\n",
|
| 742 |
+
" hf_dataset = Dataset.from_dict({\n",
|
| 743 |
+
" 'text': descriptions[:10], # 取前10筆作為示例\n",
|
| 744 |
+
" 'price': df['price'].head(10).tolist(),\n",
|
| 745 |
+
" 'area': df['area'].head(10).tolist()\n",
|
| 746 |
+
" })\n",
|
| 747 |
+
" \n",
|
| 748 |
+
" print(f\"\\n📊 創建Hugging Face Dataset成功,包含 {len(hf_dataset)} 筆資料\")\n",
|
| 749 |
+
" print(\"Dataset欄位:\", hf_dataset.column_names)\n",
|
| 750 |
+
" print(\"範例資料:\", hf_dataset[0])\n",
|
| 751 |
+
" \n",
|
| 752 |
+
" except Exception as e:\n",
|
| 753 |
+
" print(f\"⚠️ Hugging Face進階分析時發生錯誤: {e}\")\n",
|
| 754 |
+
"\n",
|
| 755 |
+
"else:\n",
|
| 756 |
+
" print(\"⚠️ Hugging Face套件未安裝,跳過文字分析\")\n",
|
| 757 |
+
" print(\"💡 要安裝Hugging Face套件,請執行:\")\n",
|
| 758 |
+
" print(\" pip install transformers datasets\")"
|
| 759 |
+
]
|
| 760 |
+
},
|
| 761 |
+
{
|
| 762 |
+
"cell_type": "markdown",
|
| 763 |
+
"id": "892cd9fb",
|
| 764 |
+
"metadata": {},
|
| 765 |
+
"source": [
|
| 766 |
+
"## 9. 儲存結果與總結\n",
|
| 767 |
+
"將分析結果儲存為檔案,並提供完整的市場分析總結。"
|
| 768 |
+
]
|
| 769 |
+
},
|
| 770 |
+
{
|
| 771 |
+
"cell_type": "code",
|
| 772 |
+
"execution_count": null,
|
| 773 |
+
"id": "3c92236f",
|
| 774 |
+
"metadata": {},
|
| 775 |
+
"outputs": [],
|
| 776 |
+
"source": [
|
| 777 |
+
"# 由 Copilot 生成\n",
|
| 778 |
+
"import os\n",
|
| 779 |
+
"\n",
|
| 780 |
+
"# 創建輸出目錄\n",
|
| 781 |
+
"output_dir = \"output\"\n",
|
| 782 |
+
"if not os.path.exists(output_dir):\n",
|
| 783 |
+
" os.makedirs(output_dir)\n",
|
| 784 |
+
"\n",
|
| 785 |
+
"# 儲存清洗後的資料\n",
|
| 786 |
+
"timestamp = datetime.now().strftime(\"%Y%m%d_%H%M%S\")\n",
|
| 787 |
+
"csv_filename = f\"{output_dir}/rental_data_analysis_{timestamp}.csv\"\n",
|
| 788 |
+
"df.to_csv(csv_filename, index=False, encoding='utf-8-sig')\n",
|
| 789 |
+
"\n",
|
| 790 |
+
"# 準備分析結果摘要\n",
|
| 791 |
+
"analysis_summary = {\n",
|
| 792 |
+
" 'analysis_date': datetime.now().isoformat(),\n",
|
| 793 |
+
" 'data_source': '591租屋網 (模擬資料)',\n",
|
| 794 |
+
" 'target_area': '高雄市鼓山區',\n",
|
| 795 |
+
" 'property_type': '2房整層電梯大樓',\n",
|
| 796 |
+
" 'total_properties': len(df),\n",
|
| 797 |
+
" 'price_statistics': {\n",
|
| 798 |
+
" 'mean': round(df['price'].mean(), 2),\n",
|
| 799 |
+
" 'median': round(df['price'].median(), 2),\n",
|
| 800 |
+
" 'std': round(df['price'].std(), 2),\n",
|
| 801 |
+
" 'min': int(df['price'].min()),\n",
|
| 802 |
+
" 'max': int(df['price'].max()),\n",
|
| 803 |
+
" 'q25': round(df['price'].quantile(0.25), 2),\n",
|
| 804 |
+
" 'q75': round(df['price'].quantile(0.75), 2)\n",
|
| 805 |
+
" },\n",
|
| 806 |
+
" 'area_statistics': {\n",
|
| 807 |
+
" 'mean': round(df['area'].mean(), 2),\n",
|
| 808 |
+
" 'median': round(df['area'].median(), 2),\n",
|
| 809 |
+
" 'min': round(df['area'].min(), 1),\n",
|
| 810 |
+
" 'max': round(df['area'].max(), 1)\n",
|
| 811 |
+
" } if not df['area'].isna().all() else {},\n",
|
| 812 |
+
" 'price_per_ping_statistics': {\n",
|
| 813 |
+
" 'mean': round(df['price_per_ping'].mean(), 2),\n",
|
| 814 |
+
" 'median': round(df['price_per_ping'].median(), 2),\n",
|
| 815 |
+
" 'min': round(df['price_per_ping'].min(), 2),\n",
|
| 816 |
+
" 'max': round(df['price_per_ping'].max(), 2)\n",
|
| 817 |
+
" } if not df['price_per_ping'].isna().all() else {},\n",
|
| 818 |
+
" 'price_distribution': df['price_range'].value_counts().to_dict(),\n",
|
| 819 |
+
" 'area_distribution': df['area_range'].value_counts().to_dict() if 'area_range' in df.columns else {}\n",
|
| 820 |
+
"}\n",
|
| 821 |
+
"\n",
|
| 822 |
+
"# 儲存分析結果\n",
|
| 823 |
+
"json_filename = f\"{output_dir}/analysis_summary_{timestamp}.json\"\n",
|
| 824 |
+
"with open(json_filename, 'w', encoding='utf-8') as f:\n",
|
| 825 |
+
" json.dump(analysis_summary, f, ensure_ascii=False, indent=2)\n",
|
| 826 |
+
"\n",
|
| 827 |
+
"print(\"💾 資料儲存完成!\")\n",
|
| 828 |
+
"print(f\" 📊 清洗後資料: {csv_filename}\")\n",
|
| 829 |
+
"print(f\" 📋 分析摘要: {json_filename}\")\n",
|
| 830 |
+
"\n",
|
| 831 |
+
"# 生成洞察和建議\n",
|
| 832 |
+
"print(\"\\n\" + \"=\"*60)\n",
|
| 833 |
+
"print(\"🎯 高雄市鼓山區租屋市場分析總結\")\n",
|
| 834 |
+
"print(\"=\"*60)\n",
|
| 835 |
+
"\n",
|
| 836 |
+
"insights = []\n",
|
| 837 |
+
"\n",
|
| 838 |
+
"# 基本市場洞察\n",
|
| 839 |
+
"insights.append(f\"共找到 {len(df)} 筆符合條件的租屋物件\")\n",
|
| 840 |
+
"insights.append(f\"平均租金為 {df['price'].mean():,.0f} 元\")\n",
|
| 841 |
+
"insights.append(f\"租金中位數為 {df['price'].median():,.0f} 元\")\n",
|
| 842 |
+
"\n",
|
| 843 |
+
"if df['price'].mean() > df['price'].median():\n",
|
| 844 |
+
" insights.append(\"租金分布向右偏斜,存在高租金物件拉高平均值\")\n",
|
| 845 |
+
"else:\n",
|
| 846 |
+
" insights.append(\"租金分布相對均勻\")\n",
|
| 847 |
+
"\n",
|
| 848 |
+
"# 租金區間分析\n",
|
| 849 |
+
"most_common_range = df['price_range'].value_counts().index[0]\n",
|
| 850 |
+
"most_common_percentage = (df['price_range'].value_counts().iloc[0] / len(df)) * 100\n",
|
| 851 |
+
"insights.append(f\"最常見的租金區間是 {most_common_range},佔 {most_common_percentage:.1f}%\")\n",
|
| 852 |
+
"\n",
|
| 853 |
+
"# 坪數分析\n",
|
| 854 |
+
"if not df['area'].isna().all():\n",
|
| 855 |
+
" insights.append(f\"平均坪數為 {df['area'].mean():.1f} 坪\")\n",
|
| 856 |
+
" if 'area_range' in df.columns:\n",
|
| 857 |
+
" most_common_area = df['area_range'].value_counts().index[0]\n",
|
| 858 |
+
" insights.append(f\"最常見的坪數區間是 {most_common_area}\")\n",
|
| 859 |
+
"\n",
|
| 860 |
+
"# 每坪租金分析\n",
|
| 861 |
+
"if not df['price_per_ping'].isna().all():\n",
|
| 862 |
+
" insights.append(f\"平均每坪租金為 {df['price_per_ping'].mean():,.0f} 元\")\n",
|
| 863 |
+
"\n",
|
| 864 |
+
"print(\"\\n🔍 重要洞��:\")\n",
|
| 865 |
+
"for i, insight in enumerate(insights, 1):\n",
|
| 866 |
+
" print(f\"{i}. {insight}\")\n",
|
| 867 |
+
"\n",
|
| 868 |
+
"print(f\"\\n💡 投資建議:\")\n",
|
| 869 |
+
"print(f\"1. 鼓山區2房電梯大樓租金水準較為穩定\")\n",
|
| 870 |
+
"print(f\"2. 建議租金預算設定在 {df['price'].quantile(0.25):,.0f} - {df['price'].quantile(0.75):,.0f} 元區間\")\n",
|
| 871 |
+
"print(f\"3. 每坪租金約在 {df['price_per_ping'].quantile(0.25):,.0f} - {df['price_per_ping'].quantile(0.75):,.0f} 元/坪範圍\")\n",
|
| 872 |
+
"print(f\"4. 建議尋找30坪左右的物件,符合市場主流需求\")\n",
|
| 873 |
+
"\n",
|
| 874 |
+
"print(\"\\n\" + \"=\"*60)\n",
|
| 875 |
+
"print(\"✅ 分析完成!資料已儲存至 output 目錄\")\n",
|
| 876 |
+
"print(\"🤗 本分析整合了 Hugging Face 生態系統進行文字處理\")\n",
|
| 877 |
+
"print(\"=\"*60)"
|
| 878 |
+
]
|
| 879 |
+
},
|
| 880 |
+
{
|
| 881 |
+
"cell_type": "markdown",
|
| 882 |
+
"id": "a5e5a43f",
|
| 883 |
+
"metadata": {},
|
| 884 |
+
"source": [
|
| 885 |
+
"## 📝 使用說明與擴展建議\n",
|
| 886 |
+
"\n",
|
| 887 |
+
"### 🚀 快速開始\n",
|
| 888 |
+
"1. 確保已安裝所有必要套件(參見 requirements.txt)\n",
|
| 889 |
+
"2. 依序執行上述程式碼區塊\n",
|
| 890 |
+
"3. 查看生成的圖表和分析結果\n",
|
| 891 |
+
"\n",
|
| 892 |
+
"### 🔧 自訂設定\n",
|
| 893 |
+
"- 修改 `SEARCH_PARAMS` 可以改變搜尋條件\n",
|
| 894 |
+
"- 調整 `max_pages` 參數可以控制爬取頁數\n",
|
| 895 |
+
"- 更改視覺化風格和顏色配置\n",
|
| 896 |
+
"\n",
|
| 897 |
+
"### 🤗 整合 Hugging Face\n",
|
| 898 |
+
"本專案整合了 Hugging Face 生態系統:\n",
|
| 899 |
+
"- **Transformers**: 用於自然語言處理模型\n",
|
| 900 |
+
"- **Datasets**: 用於資料集管理和處理\n",
|
| 901 |
+
"- **可擴展功能**: 情感分析、文字分類、實體識別等\n",
|
| 902 |
+
"\n",
|
| 903 |
+
"### ⚠️ 注意事項\n",
|
| 904 |
+
"- 591網站有反爬蟲機制,建議適度使用\n",
|
| 905 |
+
"- 模擬資料僅供展示,實際使用請替換為真實爬蟲邏輯\n",
|
| 906 |
+
"- 遵守網站使用條款和相關法規\n",
|
| 907 |
+
"\n",
|
| 908 |
+
"### 🔮 未來擴展\n",
|
| 909 |
+
"- 加入更多地區的比較分析\n",
|
| 910 |
+
"- 整合房價預測模型\n",
|
| 911 |
+
"- 建立即時資料更新機制\n",
|
| 912 |
+
"- 開發網頁介面展示分析結果"
|
| 913 |
+
]
|
| 914 |
+
}
|
| 915 |
+
],
|
| 916 |
"metadata": {
|
| 917 |
"language_info": {
|
| 918 |
"name": "python"
|
app.py
CHANGED
|
@@ -1,179 +1,10 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
""
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
import sys
|
| 12 |
-
import argparse
|
| 13 |
-
from datetime import datetime
|
| 14 |
-
|
| 15 |
-
# 加入相對路徑
|
| 16 |
-
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 17 |
-
|
| 18 |
-
from scraper import Rent591Scraper
|
| 19 |
-
from analyzer import RentalDataAnalyzer
|
| 20 |
-
from visualizer import RentalDataVisualizer
|
| 21 |
-
from utils import log_message, create_output_directories, get_current_timestamp
|
| 22 |
-
|
| 23 |
-
class RentalAnalysisApp:
|
| 24 |
-
"""591租屋分析應用程式主類別"""
|
| 25 |
-
|
| 26 |
-
def __init__(self):
|
| 27 |
-
self.scraper = Rent591Scraper()
|
| 28 |
-
self.analyzer = RentalDataAnalyzer()
|
| 29 |
-
self.visualizer = RentalDataVisualizer()
|
| 30 |
-
self.timestamp = get_current_timestamp()
|
| 31 |
-
|
| 32 |
-
def run_full_pipeline(self, max_pages: int = 5, skip_scraping: bool = False):
|
| 33 |
-
"""執行完整的分析流程"""
|
| 34 |
-
print("🏠 591租屋資料分析器啟動")
|
| 35 |
-
print("=" * 50)
|
| 36 |
-
|
| 37 |
-
# 創建輸出目錄
|
| 38 |
-
create_output_directories()
|
| 39 |
-
|
| 40 |
-
# 步驟1: 資料爬取
|
| 41 |
-
if not skip_scraping:
|
| 42 |
-
log_message("開始爬取591租屋資料...")
|
| 43 |
-
rental_data = self.scraper.scrape_rental_data(max_pages=max_pages)
|
| 44 |
-
|
| 45 |
-
if not rental_data:
|
| 46 |
-
log_message("未能獲取任何資料,程式終止", "ERROR")
|
| 47 |
-
return False
|
| 48 |
-
|
| 49 |
-
log_message(f"成功爬取 {len(rental_data)} 筆資料")
|
| 50 |
-
|
| 51 |
-
# 儲存原始資料
|
| 52 |
-
self.scraper.save_data(rental_data, f"raw_data_{self.timestamp}.json")
|
| 53 |
-
|
| 54 |
-
# 轉換為CSV
|
| 55 |
-
df = self.scraper.to_dataframe(rental_data)
|
| 56 |
-
csv_filename = f"output/rental_data_{self.timestamp}.csv"
|
| 57 |
-
df.to_csv(csv_filename, index=False, encoding='utf-8-sig')
|
| 58 |
-
log_message(f"資料已儲存為CSV: {csv_filename}")
|
| 59 |
-
|
| 60 |
-
# 使用最新的資料檔案
|
| 61 |
-
data_file = csv_filename
|
| 62 |
-
else:
|
| 63 |
-
# 尋找最新的資料檔案
|
| 64 |
-
data_files = [f for f in os.listdir("output") if f.startswith("rental_data") and f.endswith(".csv")]
|
| 65 |
-
if not data_files:
|
| 66 |
-
log_message("找不到現有資料檔案,請先執行爬蟲", "ERROR")
|
| 67 |
-
return False
|
| 68 |
-
data_file = f"output/{sorted(data_files)[-1]}"
|
| 69 |
-
log_message(f"使用現有資料檔案: {data_file}")
|
| 70 |
-
|
| 71 |
-
# 步驟2: 資料分析
|
| 72 |
-
log_message("開始資料分析...")
|
| 73 |
-
|
| 74 |
-
# 載入資料
|
| 75 |
-
self.analyzer.load_data(data_file)
|
| 76 |
-
|
| 77 |
-
# 清洗資料
|
| 78 |
-
cleaned_df = self.analyzer.clean_data()
|
| 79 |
-
if cleaned_df is None or len(cleaned_df) == 0:
|
| 80 |
-
log_message("資料清洗後沒有有效資料", "ERROR")
|
| 81 |
-
return False
|
| 82 |
-
|
| 83 |
-
# 執行完整分析
|
| 84 |
-
analysis_results = self.analyzer.run_full_analysis()
|
| 85 |
-
|
| 86 |
-
# 儲存分析結果
|
| 87 |
-
results_filename = f"analysis_results_{self.timestamp}.json"
|
| 88 |
-
self.analyzer.save_analysis_results(results_filename)
|
| 89 |
-
|
| 90 |
-
# 顯示分析摘要
|
| 91 |
-
self.analyzer.print_summary()
|
| 92 |
-
|
| 93 |
-
# 步驟3: 資料視覺化
|
| 94 |
-
log_message("開始生成視覺化圖表...")
|
| 95 |
-
|
| 96 |
-
# 設置視覺化器
|
| 97 |
-
self.visualizer.df = cleaned_df
|
| 98 |
-
self.visualizer.analysis_results = analysis_results
|
| 99 |
-
|
| 100 |
-
# 生成所有圖表
|
| 101 |
-
self.visualizer.generate_all_visualizations()
|
| 102 |
-
|
| 103 |
-
# 創建摘要報告
|
| 104 |
-
summary_filename = f"output/summary_report_{self.timestamp}.png"
|
| 105 |
-
self.visualizer.create_summary_report(summary_filename)
|
| 106 |
-
|
| 107 |
-
log_message("分析完成!", "SUCCESS")
|
| 108 |
-
self.print_completion_summary()
|
| 109 |
-
|
| 110 |
-
return True
|
| 111 |
-
|
| 112 |
-
def print_completion_summary(self):
|
| 113 |
-
"""印出完成摘要"""
|
| 114 |
-
print("\n" + "🎉 分析完成!" + "🎉")
|
| 115 |
-
print("=" * 50)
|
| 116 |
-
print("📁 輸出檔案:")
|
| 117 |
-
print(f" ├── 原始資料: output/raw_data_{self.timestamp}.json")
|
| 118 |
-
print(f" ├── 清洗資料: output/rental_data_{self.timestamp}.csv")
|
| 119 |
-
print(f" ├── 分析結果: output/analysis_results_{self.timestamp}.json")
|
| 120 |
-
print(f" ├── 摘要報告: output/summary_report_{self.timestamp}.png")
|
| 121 |
-
print(" ├── 圖表檔案:")
|
| 122 |
-
print(" │ ├── output/price_distribution.png")
|
| 123 |
-
print(" │ ├── output/price_ranges.png")
|
| 124 |
-
print(" │ ├── output/area_analysis.png")
|
| 125 |
-
print(" │ ├── output/price_per_ping.png")
|
| 126 |
-
print(" │ └── output/keywords_analysis.png")
|
| 127 |
-
print(" └── 互動式儀表板: output/dashboard.html")
|
| 128 |
-
print("\n💡 提示: 打開 dashboard.html 可查看互動式分析���果")
|
| 129 |
-
print("=" * 50)
|
| 130 |
-
|
| 131 |
-
def main():
|
| 132 |
-
"""主函數"""
|
| 133 |
-
parser = argparse.ArgumentParser(description='591租屋資料分析器')
|
| 134 |
-
parser.add_argument('--max-pages', type=int, default=5,
|
| 135 |
-
help='最大爬取頁數 (預設: 5)')
|
| 136 |
-
parser.add_argument('--skip-scraping', action='store_true',
|
| 137 |
-
help='跳過爬蟲,使用現有資料進行分析')
|
| 138 |
-
parser.add_argument('--analysis-only', action='store_true',
|
| 139 |
-
help='僅執行分析,不重新爬取資料')
|
| 140 |
-
|
| 141 |
-
args = parser.parse_args()
|
| 142 |
-
|
| 143 |
-
try:
|
| 144 |
-
app = RentalAnalysisApp()
|
| 145 |
-
|
| 146 |
-
if args.analysis_only:
|
| 147 |
-
# 僅分析模式
|
| 148 |
-
log_message("執行僅分析模式...")
|
| 149 |
-
success = app.run_full_pipeline(max_pages=0, skip_scraping=True)
|
| 150 |
-
else:
|
| 151 |
-
# 完整流程
|
| 152 |
-
success = app.run_full_pipeline(
|
| 153 |
-
max_pages=args.max_pages,
|
| 154 |
-
skip_scraping=args.skip_scraping
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
-
if success:
|
| 158 |
-
log_message("程式執行成功完成!", "SUCCESS")
|
| 159 |
-
return 0
|
| 160 |
-
else:
|
| 161 |
-
log_message("程式執行失敗", "ERROR")
|
| 162 |
-
return 1
|
| 163 |
-
|
| 164 |
-
except KeyboardInterrupt:
|
| 165 |
-
log_message("使用者中斷程式執行", "WARNING")
|
| 166 |
-
return 1
|
| 167 |
-
except Exception as e:
|
| 168 |
-
log_message(f"程式執行時發生未預期錯誤: {e}", "ERROR")
|
| 169 |
-
return 1
|
| 170 |
-
|
| 171 |
-
if __name__ == "__main__":
|
| 172 |
-
# 設置程式資訊
|
| 173 |
-
print("🏠 591租屋資料分析器")
|
| 174 |
-
print("📍 目標區域: 高雄市鼓山區")
|
| 175 |
-
print("🏢 物件類型: 2房、整層、電梯大樓")
|
| 176 |
-
print("🔧 整合 Hugging Face 生態系統")
|
| 177 |
-
print("-" * 50)
|
| 178 |
-
|
| 179 |
-
exit_code = main()
|
|
|
|
| 1 |
+
# �� Copilot �ͦ�
|
| 2 |
+
# 591�����R�� - Hugging Face Spaces����
|
| 3 |
+
# �ϥ�Gradio�@���D�n����
|
| 4 |
+
|
| 5 |
+
from gradio_app import create_interface
|
| 6 |
+
|
| 7 |
+
# �Ұ�Gradio����
|
| 8 |
+
if __name__ == "__main__":
|
| 9 |
+
demo = create_interface()
|
| 10 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# �� Copilot �ͦ�
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from typing import List, Dict
|
| 6 |
+
|
| 7 |
+
def generate_mock_rental_data(sample_size: int = 50) -> List[Dict]:
|
| 8 |
+
"""
|
| 9 |
+
�ͦ����������������s�ϯ��θ��
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
sample_size: �n�ͦ�����Ƶ���
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
�������θ�ƦC��
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
# �]�w�H���ؤl�T�O���G�i���{
|
| 19 |
+
np.random.seed(42)
|
| 20 |
+
|
| 21 |
+
# �w�q�Ѽ�
|
| 22 |
+
base_addresses = [
|
| 23 |
+
"���������s�Ϭ��N�]��",
|
| 24 |
+
"���������s�ϳշR��",
|
| 25 |
+
"���������s�ϩ��۸�",
|
| 26 |
+
"���������s�ϦۥѸ�",
|
| 27 |
+
"���������s�Ϫe���",
|
| 28 |
+
"���������s�Ϥj����",
|
| 29 |
+
"���������s�ϤE�p��",
|
| 30 |
+
"���������s�ϸθ۸�"
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
# ����y�z����r
|
| 34 |
+
keywords_pool = [
|
| 35 |
+
"�B", "�q��j��", "2��2�U", "�ĥ���", "�q���}�n",
|
| 36 |
+
"����N�]", "�ͬ������", "�z�Y��", "������K", "�w�R����",
|
| 37 |
+
"���s���C", "�a��a�q", "���x", "��R�e", "��q�K�Q",
|
| 38 |
+
"��ǰ�", "24�p�ɺz", "���Ϥ��x", "������", "��a��"
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
# �Ӽh�ﶵ
|
| 42 |
+
floors = ["3��", "4��", "5��", "6��", "7��", "8��", "9��", "10��",
|
| 43 |
+
"11��", "12��", "13��", "14��", "15��"]
|
| 44 |
+
|
| 45 |
+
mock_data = []
|
| 46 |
+
|
| 47 |
+
for i in range(sample_size):
|
| 48 |
+
# �ͦ��u�ꪺ���������]��s�Ϲ�ڦ污�^
|
| 49 |
+
# �ϥΦh�p�����������P���Ū�����
|
| 50 |
+
if np.random.random() < 0.3: # 30% ���ɪ���
|
| 51 |
+
price = np.random.normal(32000, 4000)
|
| 52 |
+
elif np.random.random() < 0.6: # 40% ���ɪ���
|
| 53 |
+
price = np.random.normal(26000, 3000)
|
| 54 |
+
else: # 30% ��������
|
| 55 |
+
price = np.random.normal(22000, 2500)
|
| 56 |
+
|
| 57 |
+
price = max(18000, min(45000, int(price))) # ����b�X�z�d��
|
| 58 |
+
|
| 59 |
+
# �ͦ��W�ơ]�Ҽ{�P�����������ʡ^
|
| 60 |
+
base_area = 25 + (price - 22000) / 1000 # �����V���W�ƶV�j
|
| 61 |
+
area = base_area + np.random.normal(0, 3) # �[�J�H���ܰ�
|
| 62 |
+
area = max(20, min(50, round(area, 1)))
|
| 63 |
+
|
| 64 |
+
# ��ܦa�}
|
| 65 |
+
address = np.random.choice(base_addresses) + f"{100 + i}��"
|
| 66 |
+
|
| 67 |
+
# ��ܼӼh
|
| 68 |
+
floor = np.random.choice(floors)
|
| 69 |
+
|
| 70 |
+
# �ͦ�����y�z
|
| 71 |
+
selected_keywords = np.random.choice(
|
| 72 |
+
keywords_pool,
|
| 73 |
+
size=np.random.randint(3, 7),
|
| 74 |
+
replace=False
|
| 75 |
+
)
|
| 76 |
+
description = f"{area}�W {floor} " + " ".join(selected_keywords)
|
| 77 |
+
|
| 78 |
+
# �ھڻ��浥�Žվ���D
|
| 79 |
+
if price >= 30000:
|
| 80 |
+
title_prefix = "��o����"
|
| 81 |
+
elif price >= 25000:
|
| 82 |
+
title_prefix = "�u�����"
|
| 83 |
+
else:
|
| 84 |
+
title_prefix = "��f�ξA"
|
| 85 |
+
|
| 86 |
+
mock_data.append({
|
| 87 |
+
'title': f'{title_prefix}2�йq��j��-���s���u�誫��{i+1:02d}',
|
| 88 |
+
'price': price,
|
| 89 |
+
'address': address,
|
| 90 |
+
'area': area,
|
| 91 |
+
'floor': floor,
|
| 92 |
+
'link': f'https://rent.591.com.tw/rent-detail-{12000+i}.html',
|
| 93 |
+
'raw_info': description,
|
| 94 |
+
'scraped_at': datetime.now().isoformat(),
|
| 95 |
+
'price_per_ping': round(price / area, 0)
|
| 96 |
+
})
|
| 97 |
+
|
| 98 |
+
return mock_data
|
| 99 |
+
|
| 100 |
+
def generate_enhanced_rental_data(sample_size: int = 50) -> pd.DataFrame:
|
| 101 |
+
"""
|
| 102 |
+
�ͦ��W�j�����θ��DataFrame
|
| 103 |
+
|
| 104 |
+
Args:
|
| 105 |
+
sample_size: �n�ͦ�����Ƶ���
|
| 106 |
+
|
| 107 |
+
Returns:
|
| 108 |
+
�]�t�B�~���R��쪺DataFrame
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
# �ͦ����
|
| 112 |
+
raw_data = generate_mock_rental_data(sample_size)
|
| 113 |
+
df = pd.DataFrame(raw_data)
|
| 114 |
+
|
| 115 |
+
# �K�[�B�~���R���
|
| 116 |
+
|
| 117 |
+
# 1. �����϶�
|
| 118 |
+
df['price_range'] = pd.cut(
|
| 119 |
+
df['price'],
|
| 120 |
+
bins=[0, 20000, 25000, 30000, 35000, float('inf')],
|
| 121 |
+
labels=['<20K', '20-25K', '25-30K', '30-35K', '>35K']
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# 2. �W�ư϶�
|
| 125 |
+
df['area_range'] = pd.cut(
|
| 126 |
+
df['area'],
|
| 127 |
+
bins=[0, 25, 30, 35, 40, float('inf')],
|
| 128 |
+
labels=['<25�W', '25-30�W', '30-35�W', '35-40�W', '>40�W']
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# 3. �Ӽh���פ���
|
| 132 |
+
df['floor_level'] = df['floor'].apply(lambda x:
|
| 133 |
+
'�C�Ӽh' if int(x.replace('��', '')) <= 5 else
|
| 134 |
+
'���Ӽh' if int(x.replace('��', '')) <= 10 else
|
| 135 |
+
'���Ӽh'
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# 4. ���š]����^
|
| 139 |
+
df['property_grade'] = df['price'].apply(lambda x:
|
| 140 |
+
'����' if x >= 30000 else
|
| 141 |
+
'����' if x >= 25000 else
|
| 142 |
+
'�g��'
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# 5. �ʻ�����С]���C�W�����^
|
| 146 |
+
price_per_ping_median = df['price_per_ping'].median()
|
| 147 |
+
df['value_rating'] = df['price_per_ping'].apply(lambda x:
|
| 148 |
+
'���ʻ���' if x < price_per_ping_median * 0.9 else
|
| 149 |
+
'����' if x < price_per_ping_median * 1.1 else
|
| 150 |
+
'����'
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
return df
|
| 154 |
+
|
| 155 |
+
def get_market_summary_stats() -> Dict:
|
| 156 |
+
"""
|
| 157 |
+
��������K�n�έp
|
| 158 |
+
|
| 159 |
+
Returns:
|
| 160 |
+
�����έp�K�n�r��
|
| 161 |
+
"""
|
| 162 |
+
|
| 163 |
+
# ����ڹ��s�ϥ����污���έp�ƾ�
|
| 164 |
+
return {
|
| 165 |
+
'market_name': '���������s��',
|
| 166 |
+
'property_type': '2�о�h�q��j��',
|
| 167 |
+
'avg_price_range': '22,000 - 35,000��',
|
| 168 |
+
'avg_area_range': '25 - 40�W',
|
| 169 |
+
'price_per_ping_range': '800 - 1,200��/�W',
|
| 170 |
+
'market_characteristics': [
|
| 171 |
+
'�F����N�]�B�R�e�����I',
|
| 172 |
+
'�ͬ����৹��',
|
| 173 |
+
'��q�K�Q�A�h���������u',
|
| 174 |
+
'���Ϻz�}�n',
|
| 175 |
+
'�A�X�p�a�x�ηs�B�ҩd'
|
| 176 |
+
],
|
| 177 |
+
'investment_highlights': [
|
| 178 |
+
'�a�q�u�V�A�O�ȩʨ�',
|
| 179 |
+
'���λݨDí�w',
|
| 180 |
+
'���ӵo�i��O�j',
|
| 181 |
+
'�ͬ��~���u�}'
|
| 182 |
+
]
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
if __name__ == "__main__":
|
| 186 |
+
# ���ո�ƥͦ�
|
| 187 |
+
print("�ͦ����ո��...")
|
| 188 |
+
|
| 189 |
+
# �ͦ����
|
| 190 |
+
basic_data = generate_mock_rental_data(10)
|
| 191 |
+
print(f"�ͦ� {len(basic_data)} �����")
|
| 192 |
+
print("�d�Ҹ��:")
|
| 193 |
+
print(basic_data[0])
|
| 194 |
+
|
| 195 |
+
# �ͦ��W�j���
|
| 196 |
+
enhanced_df = generate_enhanced_rental_data(10)
|
| 197 |
+
print(f"\n�W�j������: {list(enhanced_df.columns)}")
|
| 198 |
+
print("\n�W�j��Ʋέp:")
|
| 199 |
+
print(enhanced_df[['price', 'area', 'price_per_ping']].describe())
|
| 200 |
+
|
| 201 |
+
# �����K�n
|
| 202 |
+
market_stats = get_market_summary_stats()
|
| 203 |
+
print(f"\n�����K�n:")
|
| 204 |
+
print(f"�ؼХ���: {market_stats['market_name']}")
|
| 205 |
+
print(f"��������: {market_stats['property_type']}")
|
| 206 |
+
print(f"����d��: {market_stats['avg_price_range']}")
|
gradio_app.py
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# �� Copilot �ͦ�
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
import plotly.graph_objects as go
|
| 8 |
+
from plotly.subplots import make_subplots
|
| 9 |
+
import json
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
from rental_analyzer import RentalAnalyzer
|
| 12 |
+
from data_generator import generate_mock_rental_data, get_market_summary_stats
|
| 13 |
+
|
| 14 |
+
# �]�w����r��
|
| 15 |
+
plt.rcParams['font.sans-serif'] = ['DejaVu Sans', 'Arial Unicode MS', 'SimHei']
|
| 16 |
+
plt.rcParams['axes.unicode_minus'] = False
|
| 17 |
+
|
| 18 |
+
def analyze_rental_data(sample_size, use_hf_models):
|
| 19 |
+
"""���毲�Τ��R���D���"""
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
# �B�J1: �ͦ��������
|
| 23 |
+
progress_info = "? ���b�ͦ����R���..."
|
| 24 |
+
|
| 25 |
+
data = generate_mock_rental_data(int(sample_size))
|
| 26 |
+
df = pd.DataFrame(data)
|
| 27 |
+
|
| 28 |
+
# �B�J2: ������R
|
| 29 |
+
progress_info = "? ���b����έp���R..."
|
| 30 |
+
|
| 31 |
+
analyzer = RentalAnalyzer(df, use_hf_models=use_hf_models)
|
| 32 |
+
results = analyzer.run_analysis()
|
| 33 |
+
|
| 34 |
+
# �B�J3: �ͦ����i
|
| 35 |
+
progress_info = "? ���b�ͦ����R���i..."
|
| 36 |
+
|
| 37 |
+
# �έp���i
|
| 38 |
+
report = generate_text_report(results)
|
| 39 |
+
|
| 40 |
+
# �ͦ��Ϫ�
|
| 41 |
+
charts = create_analysis_charts(df, results)
|
| 42 |
+
|
| 43 |
+
# ��ƪ���
|
| 44 |
+
display_df = df[['title', 'price', 'area', 'price_per_ping', 'address']].head(10)
|
| 45 |
+
|
| 46 |
+
return (
|
| 47 |
+
report,
|
| 48 |
+
charts['price_distribution'],
|
| 49 |
+
charts['area_vs_price'],
|
| 50 |
+
charts['price_range_pie'],
|
| 51 |
+
charts['keywords_bar'],
|
| 52 |
+
display_df
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
error_msg = f"���R�L�{���o�Ϳ��~: {str(e)}"
|
| 57 |
+
empty_fig = px.scatter(title="�L���")
|
| 58 |
+
empty_df = pd.DataFrame()
|
| 59 |
+
|
| 60 |
+
return (
|
| 61 |
+
error_msg,
|
| 62 |
+
empty_fig,
|
| 63 |
+
empty_fig,
|
| 64 |
+
empty_fig,
|
| 65 |
+
empty_fig,
|
| 66 |
+
empty_df
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
def generate_text_report(results):
|
| 70 |
+
"""�ͦ���r���i"""
|
| 71 |
+
|
| 72 |
+
report = """
|
| 73 |
+
# ? ���������s�ϯ��Υ������R���i
|
| 74 |
+
**���R�ɶ�**: {analysis_time}
|
| 75 |
+
**��ƨӷ�**: 591���κ��������
|
| 76 |
+
|
| 77 |
+
## ? �������p
|
| 78 |
+
- **�`�����**: {total_properties} ��
|
| 79 |
+
- **���R�d��**: ���������s�� 2�о�h�q��j��
|
| 80 |
+
|
| 81 |
+
## ? �����έp���R
|
| 82 |
+
- **��������**: {mean_price:,} ��
|
| 83 |
+
- **���������**: {median_price:,} ��
|
| 84 |
+
- **�����зǮt**: {std_price:,} ��
|
| 85 |
+
- **�����d��**: {min_price:,} - {max_price:,} ��
|
| 86 |
+
- **�Ĥ@�|�����**: {q25_price:,} ��
|
| 87 |
+
- **�ĤT�|�����**: {q75_price:,} ��
|
| 88 |
+
|
| 89 |
+
## ? �W�Ʋέp���R
|
| 90 |
+
- **�����W��**: {mean_area:.1f} �W
|
| 91 |
+
- **�W�Ƥ����**: {median_area:.1f} �W
|
| 92 |
+
- **�W�ƽd��**: {min_area:.1f} - {max_area:.1f} �W
|
| 93 |
+
|
| 94 |
+
## ? �C�W�������R
|
| 95 |
+
- **�����C�W����**: {mean_ppp:,} ��/�W
|
| 96 |
+
- **�C�W���������**: {median_ppp:,} ��/�W
|
| 97 |
+
- **�C�W�����d��**: {min_ppp:,} - {max_ppp:,} ��/�W
|
| 98 |
+
|
| 99 |
+
## ? �����}��
|
| 100 |
+
{insights}
|
| 101 |
+
|
| 102 |
+
## ? ����ij
|
| 103 |
+
1. ���s��2�йq��j�ӯ������Ǹ���í�w
|
| 104 |
+
2. ��ij�����w��]�w�b {q25_price:,} - {q75_price:,} ���϶�
|
| 105 |
+
3. �C�W�������b {ppp_range} ��/�W�d��
|
| 106 |
+
4. ��ij�M��30�W���k������A�ŦX�����D�y�ݨD
|
| 107 |
+
5. ���s�ϾF����N�]�B�R�e�����I�A�㦳�}�n���ͬ�����
|
| 108 |
+
|
| 109 |
+
---
|
| 110 |
+
*�����i�� Hugging Face Spaces �۰ʥͦ�*
|
| 111 |
+
""".format(
|
| 112 |
+
analysis_time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 113 |
+
total_properties=results['basic_stats']['total_properties'],
|
| 114 |
+
mean_price=results['basic_stats']['price_stats']['mean'],
|
| 115 |
+
median_price=results['basic_stats']['price_stats']['median'],
|
| 116 |
+
std_price=results['basic_stats']['price_stats']['std'],
|
| 117 |
+
min_price=results['basic_stats']['price_stats']['min'],
|
| 118 |
+
max_price=results['basic_stats']['price_stats']['max'],
|
| 119 |
+
q25_price=results['basic_stats']['price_stats']['q25'],
|
| 120 |
+
q75_price=results['basic_stats']['price_stats']['q75'],
|
| 121 |
+
mean_area=results['basic_stats']['area_stats']['mean'],
|
| 122 |
+
median_area=results['basic_stats']['area_stats']['median'],
|
| 123 |
+
min_area=results['basic_stats']['area_stats']['min'],
|
| 124 |
+
max_area=results['basic_stats']['area_stats']['max'],
|
| 125 |
+
mean_ppp=results['basic_stats']['price_per_ping_stats']['mean'],
|
| 126 |
+
median_ppp=results['basic_stats']['price_per_ping_stats']['median'],
|
| 127 |
+
min_ppp=results['basic_stats']['price_per_ping_stats']['min'],
|
| 128 |
+
max_ppp=results['basic_stats']['price_per_ping_stats']['max'],
|
| 129 |
+
ppp_range=f"{int(results['basic_stats']['price_per_ping_stats']['min'])} - {int(results['basic_stats']['price_per_ping_stats']['max'])}",
|
| 130 |
+
insights="\n".join([f"{i+1}. {insight}" for i, insight in enumerate(results.get('insights', []))])
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
return report
|
| 134 |
+
|
| 135 |
+
def create_analysis_charts(df, results):
|
| 136 |
+
"""�Ыؤ��R�Ϫ�"""
|
| 137 |
+
|
| 138 |
+
charts = {}
|
| 139 |
+
|
| 140 |
+
# 1. ����������
|
| 141 |
+
charts['price_distribution'] = px.histogram(
|
| 142 |
+
df,
|
| 143 |
+
x='price',
|
| 144 |
+
nbins=20,
|
| 145 |
+
title='����������',
|
| 146 |
+
labels={'price': '���� (��)', 'count': '����ƶq'},
|
| 147 |
+
color_discrete_sequence=['skyblue']
|
| 148 |
+
)
|
| 149 |
+
charts['price_distribution'].update_layout(
|
| 150 |
+
xaxis_title="���� (��)",
|
| 151 |
+
yaxis_title="����ƶq"
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# 2. �W��vs�������I��
|
| 155 |
+
charts['area_vs_price'] = px.scatter(
|
| 156 |
+
df,
|
| 157 |
+
x='area',
|
| 158 |
+
y='price',
|
| 159 |
+
hover_data=['title'],
|
| 160 |
+
title='�W�ƻP�������Y',
|
| 161 |
+
labels={'area': '�W��', 'price': '���� (��)'},
|
| 162 |
+
color_discrete_sequence=['coral']
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
# �K�[�Ͷսu
|
| 166 |
+
z = np.polyfit(df['area'], df['price'], 1)
|
| 167 |
+
line_x = [df['area'].min(), df['area'].max()]
|
| 168 |
+
line_y = [z[0] * x + z[1] for x in line_x]
|
| 169 |
+
|
| 170 |
+
charts['area_vs_price'].add_trace(
|
| 171 |
+
go.Scatter(
|
| 172 |
+
x=line_x,
|
| 173 |
+
y=line_y,
|
| 174 |
+
mode='lines',
|
| 175 |
+
name='�Ͷսu',
|
| 176 |
+
line=dict(color='red', dash='dash')
|
| 177 |
+
)
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# 3. �����϶�����
|
| 181 |
+
price_dist = df['price_range'].value_counts()
|
| 182 |
+
charts['price_range_pie'] = px.pie(
|
| 183 |
+
values=price_dist.values,
|
| 184 |
+
names=price_dist.index,
|
| 185 |
+
title='�����϶�����',
|
| 186 |
+
color_discrete_sequence=px.colors.qualitative.Set3
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
# 4. ����r���R������
|
| 190 |
+
if 'keywords_analysis' in results and results['keywords_analysis']:
|
| 191 |
+
keywords_data = results['keywords_analysis']
|
| 192 |
+
filtered_keywords = {k: v for k, v in keywords_data.items() if v > 0}
|
| 193 |
+
|
| 194 |
+
if filtered_keywords:
|
| 195 |
+
charts['keywords_bar'] = px.bar(
|
| 196 |
+
x=list(filtered_keywords.values()),
|
| 197 |
+
y=list(filtered_keywords.keys()),
|
| 198 |
+
orientation='h',
|
| 199 |
+
title='����y�z����r�W�v',
|
| 200 |
+
labels={'x': '�X�{����', 'y': '����r'},
|
| 201 |
+
color_discrete_sequence=['lightcoral']
|
| 202 |
+
)
|
| 203 |
+
else:
|
| 204 |
+
charts['keywords_bar'] = px.bar(title="�L����r���")
|
| 205 |
+
else:
|
| 206 |
+
charts['keywords_bar'] = px.bar(title="�L����r���")
|
| 207 |
+
|
| 208 |
+
return charts
|
| 209 |
+
|
| 210 |
+
# ��Gradio����
|
| 211 |
+
def create_interface():
|
| 212 |
+
"""�Ы�Gradio�ϥΪ̤���"""
|
| 213 |
+
|
| 214 |
+
with gr.Blocks(
|
| 215 |
+
title="591�����R�� - ���������s��",
|
| 216 |
+
theme=gr.themes.Soft(),
|
| 217 |
+
css="""
|
| 218 |
+
.main-header { text-align: center; color: #2E86AB; }
|
| 219 |
+
.info-box { background-color: #f0f8ff; padding: 15px; border-radius: 10px; }
|
| 220 |
+
"""
|
| 221 |
+
) as demo:
|
| 222 |
+
|
| 223 |
+
# ���D
|
| 224 |
+
gr.Markdown(
|
| 225 |
+
"""
|
| 226 |
+
# ? 591�����R�� - ���������s��
|
| 227 |
+
### �M�~���Υ������R�u�� | ��X Hugging Face �ͺA�t��
|
| 228 |
+
|
| 229 |
+
���R�ؼСG**���������s��** | **2�о�h�q��j��**
|
| 230 |
+
""",
|
| 231 |
+
elem_classes=["main-header"]
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
# �\���
|
| 235 |
+
with gr.Row():
|
| 236 |
+
gr.Markdown(
|
| 237 |
+
"""
|
| 238 |
+
<div class="info-box">
|
| 239 |
+
|
| 240 |
+
### ? ���R�\��
|
| 241 |
+
- ? **�����έp**: �����ȡB����ơB�������R
|
| 242 |
+
- ? **�W�Ƥ��R**: �W�ƻP�������Y���Q
|
| 243 |
+
- ? **�ʻ���**: �C�W�����έp���R
|
| 244 |
+
- ? **�����Ͷ�**: �����϶������Ϫ�
|
| 245 |
+
- ? **��r���R**: ����y�z����r����
|
| 246 |
+
- ? **AI�ҫ�**: ��XHugging Face�۵M�y���B�z
|
| 247 |
+
|
| 248 |
+
</div>
|
| 249 |
+
""",
|
| 250 |
+
elem_classes=["info-box"]
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
# ����O
|
| 254 |
+
with gr.Row():
|
| 255 |
+
with gr.Column(scale=1):
|
| 256 |
+
gr.Markdown("### ?? ���R�]�w")
|
| 257 |
+
|
| 258 |
+
sample_size = gr.Slider(
|
| 259 |
+
minimum=30,
|
| 260 |
+
maximum=100,
|
| 261 |
+
value=50,
|
| 262 |
+
step=10,
|
| 263 |
+
label="? ��Ƶ���",
|
| 264 |
+
info="���R�����Ϊ���ƶq"
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
use_hf_models = gr.Checkbox(
|
| 268 |
+
value=True,
|
| 269 |
+
label="? �ϥ� Hugging Face �ҫ�",
|
| 270 |
+
info="�ҥ�AI��r���R�\��"
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
analyze_btn = gr.Button(
|
| 274 |
+
"? �}�l���R",
|
| 275 |
+
variant="primary",
|
| 276 |
+
size="lg"
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
# ���G��ܰϰ�
|
| 280 |
+
gr.Markdown("---")
|
| 281 |
+
gr.Markdown("## ? ���R���G")
|
| 282 |
+
|
| 283 |
+
with gr.Tabs():
|
| 284 |
+
# ���R���i����
|
| 285 |
+
with gr.Tab("? ���R���i"):
|
| 286 |
+
report_output = gr.Markdown()
|
| 287 |
+
|
| 288 |
+
# ��ı�ƹϪ�����
|
| 289 |
+
with gr.Tab("? ��ı�ƹϪ�"):
|
| 290 |
+
with gr.Row():
|
| 291 |
+
price_dist_plot = gr.Plot(label="����������")
|
| 292 |
+
area_price_plot = gr.Plot(label="�W�ƻP�������Y")
|
| 293 |
+
|
| 294 |
+
with gr.Row():
|
| 295 |
+
price_pie_plot = gr.Plot(label="�����϶�����")
|
| 296 |
+
keywords_plot = gr.Plot(label="����r���R")
|
| 297 |
+
|
| 298 |
+
# ��ƪ��歶��
|
| 299 |
+
with gr.Tab("? ��Ƥ@��"):
|
| 300 |
+
data_table = gr.Dataframe(
|
| 301 |
+
headers=["����W��", "����", "�W��", "�C�W����", "�a�}"],
|
| 302 |
+
label="���θ�ƪ� (�e10��)",
|
| 303 |
+
interactive=False
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
+
# �]�w���s�ƥ�
|
| 307 |
+
analyze_btn.click(
|
| 308 |
+
fn=analyze_rental_data,
|
| 309 |
+
inputs=[sample_size, use_hf_models],
|
| 310 |
+
outputs=[
|
| 311 |
+
report_output,
|
| 312 |
+
price_dist_plot,
|
| 313 |
+
area_price_plot,
|
| 314 |
+
price_pie_plot,
|
| 315 |
+
keywords_plot,
|
| 316 |
+
data_table
|
| 317 |
+
]
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
# ������T
|
| 321 |
+
gr.Markdown(
|
| 322 |
+
"""
|
| 323 |
+
---
|
| 324 |
+
### ? �ϥλ���
|
| 325 |
+
1. �վ���R�Ѽơ]��Ƶ��ơBAI�ҫ��ﶵ�^
|
| 326 |
+
2. �I���u�}�l���R�v���s
|
| 327 |
+
3. �d�ݤ��R���i�B�Ϫ��M��ƪ���
|
| 328 |
+
4. �Ҧ����R���G��������ơA�Ȩѥܽd�ϥ�
|
| 329 |
+
|
| 330 |
+
### ?? �`�N�ƶ�
|
| 331 |
+
- ��ƨӷ��������ͦ��A�Ω�i�ܤ��R�\��
|
| 332 |
+
- ��ڳ��p�ɥi�걵�u�ꪺ591���κ�API
|
| 333 |
+
- �ϥ�Hugging Face�ҫ��i��ݭn�����B�z�ɶ�
|
| 334 |
+
|
| 335 |
+
**? �� Hugging Face Spaces ���Ѥ䴩 | �ϥ� GitHub Copilot �ͦ�**
|
| 336 |
+
"""
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
+
return demo
|
| 340 |
+
|
| 341 |
+
# �D�{��
|
| 342 |
+
if __name__ == "__main__":
|
| 343 |
+
# �פJnumpy�]�ץ����e����|�^
|
| 344 |
+
import numpy as np
|
| 345 |
+
|
| 346 |
+
demo = create_interface()
|
| 347 |
+
demo.launch()
|
main.py
CHANGED
|
@@ -5,6 +5,12 @@
|
|
| 5 |
|
| 6 |
���{����X�F�������ΡB��Ƥ��R�M��ı�ƥ\��A
|
| 7 |
�M���Ω���R591���κ������θ�ơC
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
|
| 10 |
import os
|
|
|
|
| 5 |
|
| 6 |
���{����X�F�������ΡB��Ƥ��R�M��ı�ƥ\��A
|
| 7 |
�M���Ω���R591���κ������θ�ơC
|
| 8 |
+
|
| 9 |
+
�j�M����G
|
| 10 |
+
- �a�ϡG���������s�� (region=17§ion=247)
|
| 11 |
+
- ���G2�� (layout=2)
|
| 12 |
+
- �����G��h���a (kind=1)
|
| 13 |
+
- �ؿv�G�q��j�� (shape=2)
|
| 14 |
"""
|
| 15 |
|
| 16 |
import os
|
rental_analyzer.py
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# �� Copilot �ͦ�
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from typing import Dict, List
|
| 5 |
+
import json
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
from datasets import Dataset
|
| 8 |
+
|
| 9 |
+
class RentalAnalyzer:
|
| 10 |
+
"""���θ�Ƥ��R�� - Hugging Face Spaces����"""
|
| 11 |
+
|
| 12 |
+
def __init__(self, df: pd.DataFrame, use_hf_models: bool = True):
|
| 13 |
+
"""
|
| 14 |
+
��l�Ƥ��R��
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
df: ����DataFrame
|
| 18 |
+
use_hf_models: �O�_�ϥ�Hugging Face�ҫ�
|
| 19 |
+
"""
|
| 20 |
+
self.df = df.copy()
|
| 21 |
+
self.use_hf_models = use_hf_models
|
| 22 |
+
self.analysis_results = {}
|
| 23 |
+
|
| 24 |
+
# ��l��Hugging Face�ҫ�
|
| 25 |
+
self.sentiment_analyzer = None
|
| 26 |
+
if use_hf_models:
|
| 27 |
+
try:
|
| 28 |
+
# ���J���屡�P���R�ҫ�
|
| 29 |
+
self.sentiment_analyzer = pipeline(
|
| 30 |
+
"sentiment-analysis",
|
| 31 |
+
model="ckiplab/bert-base-chinese",
|
| 32 |
+
return_all_scores=True
|
| 33 |
+
)
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Warning: Could not load Hugging Face model: {e}")
|
| 36 |
+
self.use_hf_models = False
|
| 37 |
+
|
| 38 |
+
def clean_data(self) -> pd.DataFrame:
|
| 39 |
+
"""�M�~���"""
|
| 40 |
+
|
| 41 |
+
# �������Ƹ��
|
| 42 |
+
original_count = len(self.df)
|
| 43 |
+
self.df = self.df.drop_duplicates(subset=['title', 'address', 'price'])
|
| 44 |
+
|
| 45 |
+
# �B�z�������
|
| 46 |
+
self.df['price'] = pd.to_numeric(self.df['price'], errors='coerce')
|
| 47 |
+
self.df = self.df[self.df['price'] > 0]
|
| 48 |
+
|
| 49 |
+
# �B�z�W�Ƹ��
|
| 50 |
+
self.df['area'] = pd.to_numeric(self.df['area'], errors='coerce')
|
| 51 |
+
self.df = self.df[self.df['area'] > 0]
|
| 52 |
+
|
| 53 |
+
# �p��C�W����
|
| 54 |
+
self.df['price_per_ping'] = self.df['price'] / self.df['area']
|
| 55 |
+
|
| 56 |
+
# �������`��
|
| 57 |
+
self.df = self.remove_outliers(self.df, 'price')
|
| 58 |
+
|
| 59 |
+
# �K�[�������
|
| 60 |
+
self.add_categorical_columns()
|
| 61 |
+
|
| 62 |
+
return self.df
|
| 63 |
+
|
| 64 |
+
def remove_outliers(self, df: pd.DataFrame, column: str) -> pd.DataFrame:
|
| 65 |
+
"""�������`�ȡ]�ϥ�IQR��k�^"""
|
| 66 |
+
Q1 = df[column].quantile(0.25)
|
| 67 |
+
Q3 = df[column].quantile(0.75)
|
| 68 |
+
IQR = Q3 - Q1
|
| 69 |
+
|
| 70 |
+
lower_bound = Q1 - 1.5 * IQR
|
| 71 |
+
upper_bound = Q3 + 1.5 * IQR
|
| 72 |
+
|
| 73 |
+
return df[(df[column] >= lower_bound) & (df[column] <= upper_bound)]
|
| 74 |
+
|
| 75 |
+
def add_categorical_columns(self):
|
| 76 |
+
"""�K�[�������"""
|
| 77 |
+
|
| 78 |
+
# �����϶�
|
| 79 |
+
self.df['price_range'] = pd.cut(
|
| 80 |
+
self.df['price'],
|
| 81 |
+
bins=[0, 20000, 25000, 30000, 35000, float('inf')],
|
| 82 |
+
labels=['<20K', '20-25K', '25-30K', '30-35K', '>35K']
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# �W�ư϶�
|
| 86 |
+
self.df['area_range'] = pd.cut(
|
| 87 |
+
self.df['area'],
|
| 88 |
+
bins=[0, 25, 30, 35, 40, float('inf')],
|
| 89 |
+
labels=['<25�W', '25-30�W', '30-35�W', '35-40�W', '>40�W']
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
def basic_statistics(self) -> Dict:
|
| 93 |
+
"""�έp���R"""
|
| 94 |
+
|
| 95 |
+
stats = {
|
| 96 |
+
'total_properties': len(self.df),
|
| 97 |
+
'price_stats': {
|
| 98 |
+
'mean': round(self.df['price'].mean(), 2),
|
| 99 |
+
'median': round(self.df['price'].median(), 2),
|
| 100 |
+
'std': round(self.df['price'].std(), 2),
|
| 101 |
+
'min': int(self.df['price'].min()),
|
| 102 |
+
'max': int(self.df['price'].max()),
|
| 103 |
+
'q25': round(self.df['price'].quantile(0.25), 2),
|
| 104 |
+
'q75': round(self.df['price'].quantile(0.75), 2)
|
| 105 |
+
},
|
| 106 |
+
'area_stats': {
|
| 107 |
+
'mean': round(self.df['area'].mean(), 2),
|
| 108 |
+
'median': round(self.df['area'].median(), 2),
|
| 109 |
+
'min': round(self.df['area'].min(), 1),
|
| 110 |
+
'max': round(self.df['area'].max(), 1)
|
| 111 |
+
},
|
| 112 |
+
'price_per_ping_stats': {
|
| 113 |
+
'mean': round(self.df['price_per_ping'].mean(), 2),
|
| 114 |
+
'median': round(self.df['price_per_ping'].median(), 2),
|
| 115 |
+
'min': round(self.df['price_per_ping'].min(), 2),
|
| 116 |
+
'max': round(self.df['price_per_ping'].max(), 2)
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
return stats
|
| 121 |
+
|
| 122 |
+
def price_distribution_analysis(self) -> Dict:
|
| 123 |
+
"""�����������R"""
|
| 124 |
+
|
| 125 |
+
distribution = self.df['price_range'].value_counts().sort_index()
|
| 126 |
+
return distribution.to_dict()
|
| 127 |
+
|
| 128 |
+
def area_distribution_analysis(self) -> Dict:
|
| 129 |
+
"""�W�Ƥ������R"""
|
| 130 |
+
|
| 131 |
+
distribution = self.df['area_range'].value_counts().sort_index()
|
| 132 |
+
return distribution.to_dict()
|
| 133 |
+
|
| 134 |
+
def keywords_analysis(self) -> Dict:
|
| 135 |
+
"""����r���R"""
|
| 136 |
+
|
| 137 |
+
# �w�q�Ыά�������r
|
| 138 |
+
keywords = [
|
| 139 |
+
'�B', '��', '�q��', '���x', '������', '�z�O',
|
| 140 |
+
'�ĥ�', '�q��', '�w�R', '�K�Q', '�ͬ�����', '�ǰ�',
|
| 141 |
+
'���s', '���C', '�a��', '�a�q', '�N��', '�~���',
|
| 142 |
+
'���N�]', '�R�e', '��G', '��l�W', '���s', '�����P'
|
| 143 |
+
]
|
| 144 |
+
|
| 145 |
+
keyword_counts = {keyword: 0 for keyword in keywords}
|
| 146 |
+
|
| 147 |
+
descriptions = self.df['raw_info'].dropna().tolist()
|
| 148 |
+
|
| 149 |
+
for desc in descriptions:
|
| 150 |
+
for keyword in keywords:
|
| 151 |
+
if keyword in str(desc):
|
| 152 |
+
keyword_counts[keyword] += 1
|
| 153 |
+
|
| 154 |
+
# �ƧǨè��e10��
|
| 155 |
+
sorted_keywords = dict(
|
| 156 |
+
sorted(keyword_counts.items(), key=lambda x: x[1], reverse=True)[:10]
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
return sorted_keywords
|
| 160 |
+
|
| 161 |
+
def huggingface_analysis(self) -> Dict:
|
| 162 |
+
"""�ϥ�Hugging Face�ҫ��i����R"""
|
| 163 |
+
|
| 164 |
+
if not self.use_hf_models or self.sentiment_analyzer is None:
|
| 165 |
+
return {}
|
| 166 |
+
|
| 167 |
+
try:
|
| 168 |
+
descriptions = self.df['raw_info'].dropna().tolist()[:10] # ���e10���קK�W��
|
| 169 |
+
|
| 170 |
+
if not descriptions:
|
| 171 |
+
return {}
|
| 172 |
+
|
| 173 |
+
# ���P���R
|
| 174 |
+
sentiments = []
|
| 175 |
+
for desc in descriptions:
|
| 176 |
+
try:
|
| 177 |
+
result = self.sentiment_analyzer(desc[:100]) # �������
|
| 178 |
+
sentiments.append(result[0]['label'] if result else 'NEUTRAL')
|
| 179 |
+
except:
|
| 180 |
+
sentiments.append('NEUTRAL')
|
| 181 |
+
|
| 182 |
+
# �έp���P����
|
| 183 |
+
sentiment_counts = {}
|
| 184 |
+
for sentiment in sentiments:
|
| 185 |
+
sentiment_counts[sentiment] = sentiment_counts.get(sentiment, 0) + 1
|
| 186 |
+
|
| 187 |
+
# �Ы�Dataset
|
| 188 |
+
hf_dataset = Dataset.from_dict({
|
| 189 |
+
'text': descriptions,
|
| 190 |
+
'price': self.df['price'].head(len(descriptions)).tolist(),
|
| 191 |
+
'area': self.df['area'].head(len(descriptions)).tolist(),
|
| 192 |
+
'sentiment': sentiments
|
| 193 |
+
})
|
| 194 |
+
|
| 195 |
+
return {
|
| 196 |
+
'sentiment_distribution': sentiment_counts,
|
| 197 |
+
'dataset_size': len(hf_dataset),
|
| 198 |
+
'sample_analysis': True
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
except Exception as e:
|
| 202 |
+
print(f"Hugging Face analysis error: {e}")
|
| 203 |
+
return {}
|
| 204 |
+
|
| 205 |
+
def correlation_analysis(self) -> Dict:
|
| 206 |
+
"""�����ʤ��R"""
|
| 207 |
+
|
| 208 |
+
numeric_columns = ['price', 'area', 'price_per_ping']
|
| 209 |
+
available_columns = [
|
| 210 |
+
col for col in numeric_columns
|
| 211 |
+
if col in self.df.columns and not self.df[col].isna().all()
|
| 212 |
+
]
|
| 213 |
+
|
| 214 |
+
if len(available_columns) < 2:
|
| 215 |
+
return {}
|
| 216 |
+
|
| 217 |
+
correlation_matrix = self.df[available_columns].corr()
|
| 218 |
+
|
| 219 |
+
correlations = {}
|
| 220 |
+
for i, col1 in enumerate(available_columns):
|
| 221 |
+
for j, col2 in enumerate(available_columns):
|
| 222 |
+
if i < j: # �קK����
|
| 223 |
+
correlations[f"{col1}_vs_{col2}"] = round(
|
| 224 |
+
correlation_matrix.loc[col1, col2], 3
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
return correlations
|
| 228 |
+
|
| 229 |
+
def generate_insights(self) -> List[str]:
|
| 230 |
+
"""�ͦ����R�}��"""
|
| 231 |
+
|
| 232 |
+
insights = []
|
| 233 |
+
|
| 234 |
+
# �έp�}��
|
| 235 |
+
if 'basic_stats' in self.analysis_results:
|
| 236 |
+
stats = self.analysis_results['basic_stats']
|
| 237 |
+
insights.append(f"�@��� {stats['total_properties']} ���ŦX�����Ϊ���")
|
| 238 |
+
insights.append(f"���������� {stats['price_stats']['mean']:,} ��")
|
| 239 |
+
insights.append(f"��������Ƭ� {stats['price_stats']['median']:,} ��")
|
| 240 |
+
|
| 241 |
+
if stats['price_stats']['mean'] > stats['price_stats']['median']:
|
| 242 |
+
insights.append("���������V�k���סA�s�b�����������������")
|
| 243 |
+
|
| 244 |
+
# �������R�}��
|
| 245 |
+
if 'price_distribution' in self.analysis_results:
|
| 246 |
+
dist = self.analysis_results['price_distribution']
|
| 247 |
+
if dist:
|
| 248 |
+
most_common_range = max(dist, key=dist.get)
|
| 249 |
+
count = dist[most_common_range]
|
| 250 |
+
percentage = (count / self.analysis_results['basic_stats']['total_properties']) * 100
|
| 251 |
+
insights.append(f"�̱`���������϶��O {most_common_range}�A�� {percentage:.1f}%")
|
| 252 |
+
|
| 253 |
+
# Hugging Face���R�}��
|
| 254 |
+
if 'hf_analysis' in self.analysis_results and self.analysis_results['hf_analysis']:
|
| 255 |
+
hf_results = self.analysis_results['hf_analysis']
|
| 256 |
+
if 'sentiment_distribution' in hf_results:
|
| 257 |
+
insights.append("�w�ϥ�Hugging Face�ҫ��i�污�P���R")
|
| 258 |
+
|
| 259 |
+
return insights
|
| 260 |
+
|
| 261 |
+
def run_analysis(self) -> Dict:
|
| 262 |
+
"""���槹����R"""
|
| 263 |
+
|
| 264 |
+
# �M�~���
|
| 265 |
+
self.clean_data()
|
| 266 |
+
|
| 267 |
+
# �έp
|
| 268 |
+
self.analysis_results['basic_stats'] = self.basic_statistics()
|
| 269 |
+
|
| 270 |
+
# �������R
|
| 271 |
+
self.analysis_results['price_distribution'] = self.price_distribution_analysis()
|
| 272 |
+
self.analysis_results['area_distribution'] = self.area_distribution_analysis()
|
| 273 |
+
|
| 274 |
+
# ����r���R
|
| 275 |
+
self.analysis_results['keywords_analysis'] = self.keywords_analysis()
|
| 276 |
+
|
| 277 |
+
# �����ʤ��R
|
| 278 |
+
self.analysis_results['correlation'] = self.correlation_analysis()
|
| 279 |
+
|
| 280 |
+
# Hugging Face���R
|
| 281 |
+
if self.use_hf_models:
|
| 282 |
+
self.analysis_results['hf_analysis'] = self.huggingface_analysis()
|
| 283 |
+
|
| 284 |
+
# �ͦ��}��
|
| 285 |
+
self.analysis_results['insights'] = self.generate_insights()
|
| 286 |
+
|
| 287 |
+
return self.analysis_results
|
requirements.txt
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 1 |
-
# �� Copilot �ͦ�
|
| 2 |
-
|
| 3 |
-
|
| 4 |
pandas>=2.0.0
|
| 5 |
numpy>=1.24.0
|
| 6 |
matplotlib>=3.7.0
|
| 7 |
seaborn>=0.12.0
|
|
|
|
|
|
|
|
|
|
| 8 |
transformers>=4.30.0
|
| 9 |
datasets>=2.14.0
|
| 10 |
-
|
| 11 |
-
jupyter>=1.0.0
|
| 12 |
-
lxml>=4.9.0
|
| 13 |
-
selenium>=4.10.0
|
| 14 |
-
webdriver-manager>=3.8.0
|
|
|
|
| 1 |
+
# �� Copilot �ͦ� - Hugging Face Spaces �ۮe����
|
| 2 |
+
streamlit>=1.28.0
|
| 3 |
+
gradio>=3.50.0
|
| 4 |
pandas>=2.0.0
|
| 5 |
numpy>=1.24.0
|
| 6 |
matplotlib>=3.7.0
|
| 7 |
seaborn>=0.12.0
|
| 8 |
+
plotly>=5.15.0
|
| 9 |
+
requests>=2.31.0
|
| 10 |
+
beautifulsoup4>=4.12.0
|
| 11 |
transformers>=4.30.0
|
| 12 |
datasets>=2.14.0
|
| 13 |
+
scikit-learn>=1.3.0
|
|
|
|
|
|
|
|
|
|
|
|