基于大数据的房价数据可视化分析预测系统
溫馨提示:文末有 CSDN 平臺(tái)官方提供的博主 Wechat / QQ 名片 :)
1. 項(xiàng)目背景
????????房地產(chǎn)是促進(jìn)我國(guó)經(jīng)濟(jì)持續(xù)增長(zhǎng)的基礎(chǔ)性、主導(dǎo)性產(chǎn)業(yè),二手房市場(chǎng)是我國(guó)房地產(chǎn)市場(chǎng)不可或缺的組成部分。由于二手房的特殊性,目前市場(chǎng)上實(shí)時(shí)監(jiān)測(cè)二手房市場(chǎng)房?jī)r(jià)漲幅的情況較少,影響二手房?jī)r(jià)的因素錯(cuò)綜復(fù)雜,價(jià)格并非呈傳統(tǒng)的線性變化。
????????本項(xiàng)目利用Python實(shí)現(xiàn)某一城市二手房相關(guān)信息的爬取,并對(duì)爬取的原始數(shù)據(jù)進(jìn)行數(shù)據(jù)清洗,存儲(chǔ)到數(shù)據(jù)庫(kù)中,通過(guò) flask 搭建后臺(tái),分析影響二手房房?jī)r(jià)的各類(lèi)因素,并構(gòu)建遞歸決策樹(shù)模型,實(shí)現(xiàn)房?jī)r(jià)預(yù)測(cè)建模。
基于大數(shù)據(jù)的房?jī)r(jià)數(shù)據(jù)可視化分析預(yù)測(cè)系統(tǒng)
2. 二手房數(shù)據(jù)
????????二手房信息爬取流程為,先獲取該市所有在售樓盤(pán),以保定市為例,其中,p1 表示分頁(yè)的頁(yè)碼,因此可以構(gòu)造循環(huán),抓取所有分頁(yè)下的樓盤(pán)數(shù)據(jù)。
base_url = 'https://baoding.xxxx.com/community/p{}/'all_xqlb_links = set() for page in range(1, 51):url = base_url.format(page)# 獲取 html 頁(yè)碼,并進(jìn)行dom解析# ...?通過(guò)分析 html 頁(yè)面的 Dom 結(jié)構(gòu),利用 Bootstrap 進(jìn)行解析,獲取樓盤(pán)的詳細(xì)字段信息。?
同理,獲取樓盤(pán)下所有在售房源信息:??
def get_house_info(house_link):"""獲取房屋的信息"""headers = {'accept': '*/*','accept-encoding': 'gzip, deflate, br','accept-language': 'zh-CN,zh;q=0.9,en;q=0.8','cookie': 'Your cookie','referer': house_link,'sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'}response = requests.get(house_link, headers=headers)response.encoding = 'utf8'soup = BeautifulSoup(response.text, 'lxml')house_info = {'鏈接': house_link,'產(chǎn)權(quán)性質(zhì)': '未知','房屋類(lèi)型': '未知','產(chǎn)權(quán)年限': '未知','房本年限': '未知','唯一住房': '未知','參考首付': '未知','發(fā)布時(shí)間': '未知','總價(jià)': '','單價(jià)': '','房屋戶型': '未知','所在樓層': '未知','建筑面積': '','裝修程度': '未知','房屋朝向': '未知','建造年代': '未知','配套電梯': '無(wú)','所屬小區(qū)': '未知','所在位置': '未知'}tbody = soup.select('tbody.houseInfo-main')[0]tds = tbody.select('td')for td in tds:datas = td.text.split(' ')if '產(chǎn)權(quán)性質(zhì)' in datas[0]:house_info['產(chǎn)權(quán)性質(zhì)'] = datas[0].replace('產(chǎn)權(quán)性質(zhì)', '')if '產(chǎn)權(quán)年限' in datas[0]:house_info['產(chǎn)權(quán)年限'] = datas[0].replace('產(chǎn)權(quán)年限', '')if '發(fā)布時(shí)間' in datas[0]:house_info['發(fā)布時(shí)間'] = datas[0].replace('發(fā)布時(shí)間', '')if '唯一住房' in datas[0]:house_info['唯一住房'] = datas[0].replace('唯一住房', '')if '房屋類(lèi)型' in datas[0]:house_info['房屋類(lèi)型'] = datas[0].replace('房屋類(lèi)型', '')if '房本年限' in datas[0]:house_info['房本年限'] = datas[0].replace('房本年限', '')if '參考預(yù)算' in datas[0]:yusuan = datas[0].replace('參考預(yù)算', '')house_info['參考首付'] = yusuan[2:].split(',')[0]total_price = soup.select('span.maininfo-price-num')[0]total_price = total_price.text.strip()house_info['總價(jià)'] = total_priceavgprice = soup.select('div.maininfo-avgprice-price')[0]avgprice = avgprice.text.strip()house_info['單價(jià)'] = avgpricehuxing = soup.select('div.maininfo-model-item.maininfo-model-item-1')[0]huxing = huxing.text.strip()house_info['房屋戶型'] = huxing.split(' ')[0]house_info['所在樓層'] = huxing.split(' ')[1]daxiao = soup.select('div.maininfo-model-item.maininfo-model-item-2')[0]daxiao = daxiao.text.strip()house_info['建筑面積'] = daxiao.split(' ')[0]house_info['裝修程度'] = daxiao.split(' ')[1]chaoxiang = soup.select('div.maininfo-model-item.maininfo-model-item-3')[0]chaoxiang = chaoxiang.text.strip()house_info['房屋朝向'] = chaoxiang.split(' ')[0]house_info['建造年代'] = chaoxiang.split(' ')[1].split('/')[0][:-2]bar = soup.select('div.crumbs.crumbs-middle')[0]xiaoqu = bar.select('a.anchor.anchor-weak')[-1]house_info['所屬小區(qū)'] = xiaoqu.text.strip()tags = soup.select('div.maininfo-tags')[0].textif '電梯' in tags:house_info['配套電梯'] = '有'# 所屬區(qū)域for line in soup.select('div.maininfo-community-item'):text = line.text.strip()if '所屬區(qū)域' in text:house_info['所在位置'] = text.split(' ')[1].split('\xa0')[0]return house_info3. 二手房數(shù)據(jù)清洗與存儲(chǔ)
抓取的原始數(shù)據(jù)可能存在數(shù)據(jù)異常、缺失等情況,需要進(jìn)行數(shù)據(jù)清洗和數(shù)據(jù)類(lèi)型轉(zhuǎn)換等預(yù)處理操作。清洗后的數(shù)據(jù)存儲(chǔ)到 mysql 或 sqlite 等關(guān)系型數(shù)據(jù)庫(kù)中。
for house_info in all_house_infos:for key in all_keys:if key not in house_info:house_info[key] = '暫無(wú)'if isinstance(house_info['單價(jià)'], float):continuehouse_info['單價(jià)'] = float(house_info['單價(jià)'][:-3].strip())house_info['總價(jià)'] = float(house_info['總價(jià)'].strip())house_info['建筑面積'] = float(house_info['建筑面積'][:-1].strip())house_info['參考首付'] = float(house_info['參考首付'][:-1].strip())tmp = list(map(int, re.findall(r'(\d+)', house_info['房屋戶型'])))house_info['房屋戶型_室數(shù)'] = tmp[0]house_info['房屋戶型_廳數(shù)'] = tmp[1]house_info['房屋戶型_衛(wèi)數(shù)'] = tmp[2]del house_info['房屋戶型']if '(' not in house_info['所在樓層']:house_info['所在樓層'] = '底層({})'.format(house_info['所在樓層'])house_info['總樓層'] = list(map(int, re.findall(r'(\d+)', house_info['所在樓層'])))[0]house_info['所在樓層'] = house_info['所在樓層'][:2]4. 二手房?jī)r(jià)可視化分析預(yù)測(cè)系統(tǒng)
系統(tǒng)采用 flask 搭建 web 后臺(tái),利用 pandas 等工具包實(shí)現(xiàn)對(duì)當(dāng)前城市二手房現(xiàn)狀、二手房?jī)r(jià)格影響因素等進(jìn)行統(tǒng)計(jì)分析,并利用 bootstrap + echarts 進(jìn)行前端渲染可視化。系統(tǒng)通過(guò)構(gòu)建機(jī)器學(xué)習(xí)模型(決策樹(shù)、隨機(jī)森林、神經(jīng)網(wǎng)絡(luò)等模型),對(duì)二手房?jī)r(jià)格進(jìn)行預(yù)測(cè)。
4.1 系統(tǒng)首頁(yè)/注冊(cè)登錄
?
4.2 小區(qū)樓盤(pán)名稱(chēng)關(guān)鍵詞抽取與詞云展示
?
4.3?二手房房屋類(lèi)型與產(chǎn)權(quán)年限分布
?
4.4 不同區(qū)域在售二手房房源數(shù)量與均價(jià)對(duì)比
?
?
4.5 房?jī)r(jià)影響因素分析?
?房?jī)r(jià)影響因素包括:建造年代、建筑面積、房屋戶型、朝向、房屋類(lèi)型、樓層、裝修程度、配套電梯、房本年限、產(chǎn)權(quán)性質(zhì)、唯一住房、所在區(qū)域、周邊設(shè)施等等諸多因素。我們對(duì)每類(lèi)因素的影響情況分別進(jìn)行可視化展示:
?
?
?
4.6 基于機(jī)器學(xué)習(xí)模型的二手房?jī)r(jià)格預(yù)測(cè)
通過(guò)一些列的特征工程、數(shù)據(jù)標(biāo)準(zhǔn)化、訓(xùn)練集驗(yàn)證集構(gòu)造、決策樹(shù)模型構(gòu)建等操作,完成決策樹(shù)模型的交叉驗(yàn)證訓(xùn)練和模型評(píng)估:
print('---> cv train to choose best_num_boost_round') dtrain = xgb.DMatrix(train_X, label=train_Y, feature_names=df_columns)xgb_params = {'learning_rate': 0.005,'n_estimators': 4000,'max_depth': 3,'min_child_weight': 1.5,'eval_metric': 'rmse','objective': 'reg:linear','nthread': -1,'silent': 1,'booster': 'gbtree' }cv_result = xgb.cv(dict(xgb_params),dtrain,num_boost_round=4000,early_stopping_rounds=100,verbose_eval=400,show_stdv=False,) best_num_boost_rounds = len(cv_result) mean_train_logloss = cv_result.loc[best_num_boost_rounds-11 : best_num_boost_rounds-1, 'train-rmse-mean'].mean() mean_test_logloss = cv_result.loc[best_num_boost_rounds-11 : best_num_boost_rounds-1, 'test-rmse-mean'].mean() print('best_num_boost_rounds = {}'.format(best_num_boost_rounds))print('mean_train_rmse = {:.7f} , mean_valid_rmse = {:.7f}\n'.format(mean_train_logloss, mean_test_logloss))模型訓(xùn)練結(jié)果:
---> cv train to choose best_num_boost_round [0] train-rmse:4.10205 test-rmse:4.10205 [400] train-rmse:0.59919 test-rmse:0.605451 [800] train-rmse:0.20857 test-rmse:0.230669 [1200] train-rmse:0.185981 test-rmse:0.21354 [1600] train-rmse:0.181188 test-rmse:0.211841 [2000] train-rmse:0.177933 test-rmse:0.211291 [2400] train-rmse:0.174346 test-rmse:0.210886 best_num_boost_rounds = 2512 mean_train_rmse = 0.1733781 , mean_valid_rmse = 0.2108875測(cè)試集預(yù)測(cè)結(jié)果與真實(shí)值分布情況:
print('決策樹(shù)模型在驗(yàn)證集上的均方誤差 RMSE 為:', rmse(valid_Y, predict_valid))>> 決策樹(shù)模型在驗(yàn)證集上的均方誤差 RMSE 為: 0.19991482173207226?
?二手房?jī)r(jià)格預(yù)測(cè)模型交互式頁(yè)面:
?
5. 總結(jié)
????????本項(xiàng)目利用Python實(shí)現(xiàn)某城市二手房相關(guān)信息的爬取,并對(duì)爬取的原始數(shù)據(jù)進(jìn)行數(shù)據(jù)清洗,存儲(chǔ)到數(shù)據(jù)庫(kù)中,通過(guò) flask 搭建后臺(tái),分析影響二手房房?jī)r(jià)的各類(lèi)因素,并構(gòu)建遞歸決策樹(shù)模型,實(shí)現(xiàn)房?jī)r(jià)預(yù)測(cè)建模。
歡迎大家點(diǎn)贊、收藏、關(guān)注、評(píng)論啦 ,由于篇幅有限,只展示了部分核心代碼。
技術(shù)交流認(rèn)準(zhǔn)下方?CSDN 官方提供的學(xué)長(zhǎng) Wechat / QQ 名片 :)
精彩專(zhuān)欄推薦訂閱:
1. Python 畢設(shè)精品實(shí)戰(zhàn)案例
2. 自然語(yǔ)言處理 NLP 精品實(shí)戰(zhàn)案例
3. 計(jì)算機(jī)視覺(jué) CV 精品實(shí)戰(zhàn)案例
總結(jié)
以上是生活随笔為你收集整理的基于大数据的房价数据可视化分析预测系统的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MTK平台Android4.4 拍照默认
- 下一篇: Linux的 i2c 驱动框架分析