上一集,我們討論了單變量的情況下,如何利用sklearn模塊的線性回歸模型預測房價,擬合出了一條回歸直線。
在上一集,我們假定只考慮房屋面積這一個變量,那么預測函數應該是:
h(x)=theta0+theta1*x1
用模型求出theta0和theta1,擬合出了一條直線,其中x1是訓練數據(房屋的面積)。
然而,如果需要考慮兩個以上的變量,如在預測中加入房間數量對房價的影響,那么構造的預測函數就應該是:
h(x)=theta0+theta1*x1+theta2*x2
其中,x1和x2是訓練數據面積和房屋數量。
模型求出theta0,theta1,theta2后,我們可以發現,這其實是一個平面,我們可以在圖上畫出這個平面,觀查他擬合情況的好壞!
1、構造模型需要的訓練數據
上一集中我們已經知道了線性回歸模型需要的訓練數據:
Paste_Image.png
對下面的訓練數據:
Paste_Image.png
我們可以采用和上一集同樣的方法來處理:
# Function to get data
def get_data(file_name):
data = pd.read_csv(file_name)
print data
X1_parameter = []
X2_parameter = []
Y_parameter = []
for single_square_feet, single_bedroom,price_value in zip(data['livingarea']\
,data['bedrooms'],data['prices']):
X1_parameter.append([float(single_square_feet)])
X2_parameter.append([float(single_bedroom)])
Y_parameter.append([float(price_value)])
return X1_parameter,X2_parameter,Y_parameter
可以看到,我們只是添加了一個變量X2_parameter
,同時將數據集中 bedrooms
列的數據傳給了它。
但是,模型需要的訓練數據的形式是(X,y),X是一個變量矩陣,y是一個目標值數組。
所以,我們還需要對X1_parameter,X2_parameter
進行重組,這里numpy
提供了一個非常好用的函數column_stack
他可以將一維數組變成矩陣的一列,如:
X,Z = np.column_stack((X1,X2)),zz
最終的矩陣X的第一列為X1,第二列為X2。
2、用模型計算預測函數h(x)的參數。
X1,X2,zz=get_data('./2dimension_input.csv')
X,Z = np.column_stack((X1,X2)),zz
regr = linear_model.LinearRegression()
regr.fit(X,Z)
a,b = regr.coef_, regr.intercept_
print 'theta0 is:%s;\ntheta1 is:%s;\ntheta2 is:%s' %(b[0],a[0][0],a[0][1])
print regr.predict(X)
Paste_Image.png
那么問題是?這些參數是否是合理?
3、下面,我們從圖形上來看看:
#plot the training data and predict plane.
fig = plt.figure()
ax = fig.gca(projection='3d')
# define the predict range.
xxx1 = np.linspace(1100,3000,10)
xxx2 = np.linspace(1,10,10)
# Function meshgrid is very important.
xx1, xx2 = np.meshgrid(xxx1, xxx2)
XX=np.column_stack((xx1.flatten(),xx2.flatten()))
# plot the training data points with red color.
ax.scatter(X1, X2, zz,c='r',s=50)
# plot the wirefrme of predict plane.
ax.plot_wireframe(xx1, xx2, regr.predict(XX).reshape(10,10))
ax.plot_surface(xx1, xx2, regr.predict(XX).reshape(10,10), alpha=0.11)
plt.show()
Paste_Image.png
從圖中可以看出,預測的平面與訓練數據契合度非常好!
4、完整代碼
# coding = utf-8
import numpy as np
import pandas as pd
from sklearn import datasets, linear_model
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pdb
# Function to get data
def get_data(file_name):
data = pd.read_csv(file_name)
print data
X1_parameter = []
X2_parameter = []
Y_parameter = []
for single_square_feet, single_bedroom,price_value in zip(data['livingarea']\
,data['bedrooms'],data['prices']):
X1_parameter.append([float(single_square_feet)])
X2_parameter.append([float(single_bedroom)])
Y_parameter.append([float(price_value)])
return X1_parameter,X2_parameter,Y_parameter
pdb.set_trace()
X1,X2,zz=get_data('./2dimension_input.csv')
X,Z = np.column_stack((X1,X2)),zz
regr = linear_model.LinearRegression()
regr.fit(X,Z)
a,b = regr.coef_, regr.intercept_
print 'theta0 is:%s;\ntheta1 is:%s;\ntheta2 is:%s' %(b[0],a[0][0],a[0][1])
print regr.predict(X)
#plot the training data and predict plane.
fig = plt.figure()
ax = fig.gca(projection='3d')
# define the predict range.
xxx1 = np.linspace(1100,3000,10)
xxx2 = np.linspace(1,10,10)
# Function meshgrid is very important.
xx1, xx2 = np.meshgrid(xxx1, xxx2)
XX=np.column_stack((xx1.flatten(),xx2.flatten()))
# plot the training data points with red color.
ax.scatter(X1, X2, zz,c='r',s=50)
# plot the wirefrme of predict plane.
ax.plot_wireframe(xx1, xx2, regr.predict(XX).reshape(10,10))
ax.plot_surface(xx1, xx2, regr.predict(XX).reshape(10,10), alpha=0.11)
plt.show()