(See Getting Started with SFrames for setup instructions)
import graphlab
# Limit number of worker processes. This preserves system memory, which prevents hosted notebooks from crashing.
graphlab.set_runtime_config('GRAPHLAB_DEFAULT_NUM_PYLAMBDA_WORKERS', 4)
Dataset is from house sales in King County, the region where the city of Seattle, WA is located.
SFrame 是一种数据结构,在 Graphlab Create 中被用来表示单元数居
sales = graphlab.SFrame('home_data.gl/') # 这是一个公共数据集,记录了在西雅图区域被售出的房子的情况
sales
The house price is correlated with the number of square feet of living space.
view="Scatter Plot":表示要绘制散点图
graphlab.canvas.set_target('ipynb'):canvas 绘制在 Notebook 内部
graphlab.canvas.set_target('ipynb')
sales.show(view="Scatter Plot", x="sqft_living", y="price") # x 轴表示居住面积,y 轴表示房价
Split data into training and testing.
We use seed=0 so that everyone running this notebook gets the same results. In practice, you may set a random seed (or let GraphLab Create pick a random seed for you).
通过调用 random.split(训练集比例,seed=随机种子) 函数,将数据分为训练集和测试集
seed=0:可以保证每次得到相同的结果
train_data,test_data = sales.random_split(.8,seed=0) # 80% 作为训练集,20% 作为测试集
创建回归模型
函数 graphlab.linear_regression.create():创建线性回归模型
Graghlab Create 默认会自动选择算法
sqft_model = graphlab.linear_regression.create(train_data, target='price', features=['sqft_living'],validation_set=None)
mean():求平均值
模型.evaluate(数据集):评估建立的模型(max_error:最大误差;rmse:均方根误差)
print test_data['price'].mean()
print sqft_model.evaluate(test_data)
RMSE of about \$255,170!
Matplotlib is a Python plotting library that is also useful for plotting. You can install it with:
'pip install matplotlib'
%matplotlib inline:在 Notebook 上绘图
matplotlib.pyplot.plot(图形1的x轴, 图形1的y轴, 图形1的标记符号, 图形2的x轴, 图形2的y轴, 图形2的标记符号, ...)
模型.predict(数据集):利用模型计算数据集的预测值
模型.get('coefficients'):输出模型的参数
import matplotlib.pyplot as plt
%matplotlib inline
# 画 2 个图:图形 1 绘制测试集的房屋面积和实际房价的关系;图形 2 试集的房屋面积和预测房价的关系
plt.plot(test_data['sqft_living'],test_data['price'],'.',
test_data['sqft_living'],sqft_model.predict(test_data),'-')
Above: blue dots are original data, green line is the prediction from the simple regression.
Below: we can view the learned regression coefficients.
sqft_model.get('coefficients')
# 下面的表格中,第一行为截距,第二行为斜率
To build a more elaborate model, we will explore using more features.
my_features = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'zipcode']
sales[my_features].show()
view='BoxWhisker Plot':绘制两个变量之间的关系(红色线:平均值;横线:最大值与最小值)
sales.show(view='BoxWhisker Plot', x='zipcode', y='price')
Pull the bar at the bottom to view more of the data.
98039 is the most expensive zip code.
my_features_model = graphlab.linear_regression.create(train_data,target='price',features=my_features,validation_set=None)
print my_features
print sqft_model.evaluate(test_data)
print my_features_model.evaluate(test_data)
# 可以看出最大误差和均方误差都比单一特征的模型有所降低
The RMSE goes down from \$255,170 to \$179,508 with more features.
The first house we will use is considered an "average" house in Seattle.
house1 = sales[sales['id']=='5309101200']
house1
在 Notebook 中添加图片:
<img src="url或者本地路径">
print house1['price']
print sqft_model.predict(house1)
print my_features_model.predict(house1)
In this case, the model with more features provides a worse prediction than the simpler model with only 1 feature. However, on average, the model with more features is better.
We will now examine the predictions for a fancier house.
house2 = sales[sales['id']=='1925069082']
house2
print sqft_model.predict(house2)
print my_features_model.predict(house2)
In this case, the model with more features provides a better prediction. This behavior is expected here, because this house is more differentiated by features that go beyond its square feet of living space, especially the fact that it's a waterfront house.
Our last house is a very large one owned by a famous Seattleite.
预测比尔盖茨的 house。。。
bill_gates = {'bedrooms':[8],
'bathrooms':[25],
'sqft_living':[50000],
'sqft_lot':[225000],
'floors':[4],
'zipcode':['98039'],
'condition':[10],
'grade':[10],
'waterfront':[1],
'view':[4],
'sqft_above':[37500],
'sqft_basement':[12500],
'yr_built':[1994],
'yr_renovated':[2010],
'lat':[47.627606],
'long':[-122.242054],
'sqft_living15':[5000],
'sqft_lot15':[40000]}
print my_features_model.predict(graphlab.SFrame(bill_gates))
The model predicts a price of over $13M for this house! But we expect the house to cost much more. (There are very few samples in the dataset of houses that are this fancy, so we don't expect the model to capture a perfect prediction here.)