使用GraphLab Create构建房屋售价预测回归模型

 


# coding: utf-8

# In[1]:

# 启动graphlab create
import graphlab
sales = graphlab.SFrame("data/home_data.gl")
sales


# In[2]:

sales.show()


# In[3]:

graphlab.canvas.set_target('ipynb')


# In[5]:

# 房屋面积与价格的散点图
sales.show(view='Scatter Plot', x='sqft_living', y='price')


# # 分离出训练集和测试集

# In[6]:

train_data, test_data = sales.random_split(.8, seed=0)


# ## 构建回归模型

# In[13]:

sqft_model = graphlab.linear_regression.create(train_data, target='price', features=['sqft_living'])


# # 评估线性回归模型

# In[14]:

print test_data['price'].mean()


# In[15]:

print sqft_model.evaluate(test_data)


# # 用图表查看预测价格和真实价格的散点图

# In[17]:

import matplotlib.pyplot as plt
get_ipython().magic(u'matplotlib inline')


# In[18]:

plt.plot(test_data['sqft_living'], test_data['price'], '.',
        test_data['sqft_living'], sqft_model.predict(test_data), '-')


# In[20]:

# 预测模型系数
sqft_model.get('coefficients')


# # 探索数据中的其它特征

# In[27]:

my_features = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'zipcode']


# In[28]:

sales[my_features].show()


# In[29]:

sales.show(view='BoxWhisker Plot', x='zipcode', y='price')


# # 构建更多特征的回归模型

# In[33]:

my_features_model = graphlab.linear_regression.create(train_data, target='price', features=my_features)


# In[35]:

print my_features_model.evaluate(test_data)


# In[36]:

plt.plot(test_data['sqft_living'], test_data['price'], '.',
        test_data['sqft_living'], my_features_model.predict(test_data), '-')


# In[37]:

# 预测模型系数
my_features_model.get('coefficients')


# # 应用学到的模型来预测房屋的售价

# In[39]:

house1 = sales[sales['id'] == '5309101200']


# In[40]:

house1


# In[41]:

print sqft_model.predict(house1)


# In[42]:

print my_features_model.predict(house1)


# In[ ]:



 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值