斯坦福房价预测

目录

 相关资料

 FTR文件上传解压问题

解压

房价预测代码


 相关资料

​​​​​​课程地址:2.1 探索性数据分析【斯坦福21秋季:实用机器学习中文版】
数据集地址:https://c.d2l.ai/stanford-cs329p/assignments.html#assignment-1
原版代码:https://c.d2l.ai/stanford-cs329p/_static/notebooks/cs329p_notebook_eda.slides.html#/
 

 FTR文件上传解压问题

jupyter再上传ftr文件时如果出现无法上传问题可以打包为压缩包进行上传

 选择上传位置进行上传

解压

新建一个notebook文件,运行以下代码

# xxx.zip,这里xxx是你打包的名字
import zipfile
import os
files = zipfile.ZipFile('xxx.zip', 'r')
files.extractall(os.getcwd())
files.close() 

房价预测代码

# !pip install seaborn pandas matplotlib numpy
import numpy as np
# 方便处理tabular data的表,用于数据不是特别大
import pandas as pd 
# 两个画图
import matplotlib.pyplot as plt
import seaborn as sns
# 图片svg分辨率高
from IPython import display
display.set_matplotlib_formats('svg')
# Alternative to set svg for newer versions
# import matplotlib_inline
# matplotlib_inline.backend_inline.set_matplotlib_formats('svg')
# 读取数据
data = pd.read_feather('house_sales.ftr')
data.shape

 (164944, 1789)

# 头部的10个数据
data.head(10)

 

# 空数据的数量
null_sum = data.isnull().sum()
# 缺失数据小于该数据的30%就保留
data.columns[null_sum < len(data)*0.3]

# 没有保留的列去掉
data.drop(columns = data.columns[null_sum > len(data) * 0.3],inplace=True)
# 数据类型转换
data['Id']=data['Id'].astype(int)
data['Elementary School Score']=data['Elementary School Score'].astype(float)
data['Total spaces']=data['Total spaces'].astype(float)
data['Bathrooms']=data['Bathrooms'].astype(float)
data['Elementary School Distance']=data['Elementary School Distance'].astype(float)
data['Bathrooms']=data['Bathrooms'].astype(float)
data['Garage spaces']=data['Garage spaces'].astype(float)
data['Zip']=data['Zip'].astype(int)
# 展示数据类型
data.dtypes

currency = ['Sold Price','Listed Price','Tax assessed value','Annual tax amount']
for c in currency:
    data[c] = data[c].replace(
#         $,-(表示没有)都转换成float类型
    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
#     先将sqft,Acres变成空再转换float类型
    col = data[c].replace(r'\b sqft\b|\b Acres\b|\b,\b','',regex=True).astype(float)
#    同一单位
    col[acres]*=43560
    data[c]=col
# 把数据列都列出来,然后分析
data.describe()

 

# 看看一定范围外的数据的个数
abnormal = (data[areas[1]] < 10) | (data[areas[1]] > 1e4)
data = data[~abnormal]
sum(abnormal)

# log10让数据更直观
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()]);

# 房子的类型,取里面独一无二的值,然后计算每个值上面有多少个独一无二的值
# 打印前面20个
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');

#箱式图
# 不同房子类型之间每平米的售价
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]);
#中间横线是中位数
#上面的横线是最大值
#方框上边界为3/4的值
#方框下边界为1/4的值

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);

data.dtypes

# 矩阵图可以看关联度
_, ax = plt.subplots(figsize=(6,6))
columns = ['Sold Price', 'Listed Price', 'Annual tax amount', 'Price per living sqft', 'Elementary School Score', 'High School Score']
sns.heatmap(data[columns].corr(),annot=True,cmap='RdYlGn', ax=ax);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值