回归模型与房价预测

from sklearn.datasets import load_boston#导入数据集
boston=load_boston()
#住宅平均房数与房价之间的关系
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
lineR=LinearRegression()
x=boston.data[:,6]
y=boston.target
plt.figure(figsize=(10,6))
plt.scatter(x,y)
lineR.fit(x.reshape(-1,1),y)
w=lineR.coef_
b=lineR.intercept_
plt.plot(x,w*x+b,'r')
plt.show()
# 多元线性回归模型,建立13个变量与房价之间的预测模型,并检测模型好坏
# 划分数据集
from sklearn.cross_validation import train_test_split 
x_train, x_test, y_train, y_test = train_test_split(boston.data,boston.target,test_size=0.3)
# 建立多项式性回归模型
lineR = LinearRegression()
lineR.fit(x_train,y_train)
 
 
# 检测模型好坏
import numpy as np
x_predict = lineR.predict(x_test)
# 打印预测的均方误差
print("预测的均方误差:", np.mean(x_predict - y_test)**2)
# 打印模型的分数
print("模型的分数:",lineR.score(x_test, y_test))
import matplotlib.pyplot as plt
 
x=boston.data[:,12].reshape(-1,1)
y=boston.target
plt.figure(figsize=(10,6))
plt.scatter(x,y)
from sklearn.linear_model import LinearRegression
lineR=LinearRegression()
lineR.fit(x,y)
y_pred=lineR.predict(x)
plt.plot(x,y_pred)
print(lineR.coef_,lineR.intercept_)
plt.show()
#一元多项式回归模型,建立一个变量与房价之间的预测模型,
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree=2)
x_poly = poly.fit_transform(x)
lp = LinearRegression()#G构建模型
lp.fit(x_poly,y)
y_poly_pred = lp.predict(x_poly)
 
plt.scatter(x,y)
plt.plot(x,y_poly_pred,'r')
plt.show()
 
 
lrp = LinearRegression()
lrp.fit(x_poly,y)
plt.scatter(x,y)
plt.scatter(x,y_pred)
plt.scatter(x,y_poly_pred)   #多项回归
plt.show()

  

  

你可能感兴趣的:(回归模型与房价预测)