描述性统计
相关性分析
从相关系数表和回归图来看,间隔(即居室)和楼龄都与香港二手房房价无明显的相关性。实用面积与房价具有较强的正相关性,一般来说,人们在看房子时看到的面积是建筑面积,但却不是实用面积。套内建筑面积=套内使用面积+套内墙体面积+阳台面积,而实用面积就是套内使用面积。另外,实用率与房价也无相关性,这与大部分人的感性认识存在偏差。
技术实现
本文数据来源于中原地产,网页结构相对简单。数据清洗主要用到Python的pandas库,由于内容较多,仅提供核心字段清洗代码。数据可视化主要用到Python的pyecharts库,都是一些基础图表,本公众号往期原创文章也已多次提及。
数据获取
爬虫核心代码
#将繁体转换成简体 def tradition2simple(line): return Converter(‘zh-hans’).convert(line) #解析网页 def get_page(page): if page <11: url = ‘http://hk.centanet.com/findproperty/BLL/Result_SearchHandler.ashx?url=http%3A%2F%2Fhk.centanet.com%2Ffindproperty%2Fzh-HK%2FHome%2FSearchResult%3Fposttype%3DS%26src%3DC%26minprice%3D%26maxprice%3D%26sortcolumn%3D%26sorttype%3D%26limit%3D100%26currentpage%3D{0}’.format(page) else: url = ‘http://hk.centanet.com/findproperty/BLL/Result_SearchHandler.ashx?url=http%3A%2F%2Fhk.centanet.com%2Ffindproperty%2Fzh-HK%2FHome%2FSearchResult%3Fposttype%3DS%26src%3DC%26minprice%3D%26maxprice%3D%26sortcolumn%3D%26sorttype%3D%26limit%3D-1%26currentpage%3D{0}’.format(page) req = requests.get(url, headers = headers) bs = req.json()
print(bs)
ts = tradition2simple(bs[‘post’])
print(ts)
html = etree.HTML(ts) if name == ‘main’: ua = UserAgent(verify_ssl=False) headers = {“User-Agent”: ua.random} for page in range(1,2624): #共2623页 get_page(page)
time.sleep(1)
print(“第%d页爬取完成”%page) print(‘-’*100)
数据预览
数据清洗
建筑面积/单价
#异常字符替换为空 df[“建筑面积”] = df[“建筑面积”].str.replace(“,”,“”).astype(“float”) df[“建面单价”] = df[“建面单价”].str.replace(“$”,“”).str.replace(“,”,“”).str.replace(“/呎”,“”).astype(“float”) #建筑面积和建面单价缺失值用均值填充 df = df.fillna(value={‘建筑面积’:df[“建筑面积”].mean(),‘建面单价’:df[“建面单价”].mean()})
间隔
存在缺失值、换行符、非数字型、无房间数等脏数据
df[“间隔”] = df[“间隔”].str.replace(“\r\n”,“”).str[:1] df = df[ ~ df[‘间隔’].isin([‘(’])] #删除某列包含特殊字符的行 df[“间隔”] = df[“间隔”].str.replace(“开”,“0”).astype(“float”) df = df.fillna(value={‘间隔’:df[“间隔”].mean()}) df[“间隔”] = df[“间隔”].astype(“int”)
售价
#售价单位存在万和亿,进行统一化处理 df[“售价”] = (df[“售价”].str.replace(“$”,“”).str.replace(“,”,“”).str[:-1].astype(float) * df[‘售价’].str[-1].map({“万”: 1, “亿”: 10000})).astype(“int”)
数据可视化
回归图
fig,axes=plt.subplots(5,1,figsize=(12,30)) sns.regplot(x=‘间隔’,y=‘实用单价’,data=df1,color=‘green’,marker=‘*’,ax=axes[0]) sns.regplot(x=‘楼龄’,y=‘实用单价’,data=df1,color=‘green’,marker=‘*’,ax=axes[1]) sns.regplot(x=‘实用面积’,y=‘实用单价’,data=df1,color=‘green’,marker=‘*’,ax=axes[2]) sns.regplot(x=‘建筑面积’,y=‘实用单价’,data=df1,color=‘green’,marker=‘*’,ax=axes[3]) sns.regplot(x=‘实用率’,y=‘实用单价’,data=df1,color=‘green’,marker=‘*’,ax=axes[4])
条形图
df5 = df1.groupby(‘屋苑位置’)[‘实用单价’].count() df5 = df5.sort_values(ascending=True) df5 = df5.tail(10) print(df5.index.to_list()) print(df5.to_list()) c = ( Bar(init_opts=opts.InitOpts(theme=ThemeType.WONDERLAND)) .add_xaxis(df5.index.to_list()) .add_yaxis(“”,df5.to_list()).reversal_axis() #X轴与y轴调换顺序
现在能在网上找到很多很多的学习资源,有免费的也有收费的,当我拿到1套比较全的学习资源之前,我并没着急去看第1节,我而是去审视这套资源是否值得学习,有时候也会去问一些学长的意见,如果可以之后,我会对这套学习资源做1个学习计划,我的学习计划主要包括规划图和学习进度表。
分享给大家这份我薅到的免费视频资料,质量还不错,大家可以跟着学习