0
点赞
收藏
分享

微信扫一扫

房屋价格预测-数据分析(python)

覃榜言 2022-02-11 阅读 54

房屋价格预测-数据分析(python)

dataset download(https://c.d2l.ai/stanford-cs329p/assignments.html#assignment-1)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from IPython import display
display.set_matplotlib_formats('svg')

data = pd.read_feather('house_sales.ftr')
# data = pd.read_csv('house_sales.zip')
# print(data.shape)
# print(data.head)

null_sum = data.isnull().sum()
print(data.columns[null_sum < len(data) * 0.3])
data.drop(columns=data.columns[null_sum > len(data) * 0.3], inplace=True)

print(data.dtypes)

currency = ['Sold Price', 'Listed Price', 'Tax assessed value', 'Annual tax amount']
for c in currency:
    data[c] = data[c].replace(
        r'[$,-]', '', regex=True).replace(
        r'^\s*$', np.nan, regex=True).astype(float)

areas = ['Total interior livable area', 'Lot size']
for c in areas:
    acres = data[c].str.contains('Acres') == True
    col = data[c].replace(r'\b sqft\b|\b Acres\b|\b,\b', '', regex=True).astype(float)
    col[acres] *= 43560
    data[c] = col

print(data.describe())

# simple handle, handle the too small or too big data
abnormal = (data[areas[1]] < 10) | (data[areas[1]] > 1e4)
data = data[~abnormal]
print('abnormal: ', sum(abnormal))

# ax = sns.histplot(np.log10(data['Sold Price']))
# ax.set_xlim([3, 8])
# ax.set_xticks(range(3, 9))
# ax.set_xticklabels(['%.0e'%a for a in 10**ax.get_xticks()])
# plt.show()

print(data['Type'].value_counts()[0:20])

types = data['Type'].isin(['SingleFamily', 'Condo', 'MultiFamily', 'Townhouse'])
# sns.displot(pd.DataFrame({
#     'Sold Price': np.log10(data[types]['Sold Price']),
#     'Type': data[types]['Type']
# }), x='Sold Price', hue='Type', kind='kde')
# plt.show()

data['Price per living sqft'] = data['Sold Price'] / data['Total interior livable area']
ax = sns.boxplot(x='Type', y='Price per living sqft', data=data[types], fliersize=0)
ax.set_ylim([0, 2000])
# plt.show()

d = data[data['Zip'].isin(data['Zip'].value_counts()[:20].keys())]
ax = sns.boxplot(x='Zip', y='Price per living sqft', data=d, fliersize=0)
ax.set_ylim([0, 2000])
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
# plt.show()

_, ax = plt.subplots(figsize=(6, 6))
columns = ['Sold Price', 'Listed Price', 'Annual tax amount', 'Price per living sqft']
sns.heatmap(data[columns].corr(), annot=True, cmap='RdYlGn', ax=ax)
plt.show()

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

举报

相关推荐

0 条评论