回归模型与房价预测
1. 導入boston房價數據集
from sklearn.datasets import load_boston boston=load_boston() boston.keys() print(boston.DESCR) boston.data.shape import pandas as pd pd.DataFrame(boston.data)? 2. 一元線性回歸模型,建立一個變量與房價之間的預測模型,并圖形化顯示。
import matplotlib.pyplot as plt x=boston.data[:,5] y=boston.target plt.figure(figsize=(20,6)) plt.scatter(x,y) plt.plot(x,9*x-20,'r') plt.show() x.shape?3.多元線性回歸模型,建立13個變量與房價之間的預測模型,并檢測模型好壞,并圖形化顯示檢查結果。
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()4.? 一元多項式回歸模型,建立一個變量與房價之間的預測模型,并圖形化顯示。
?
from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree=2) x_poly = poly.fit_transform(x)lp = LinearRegression() lp.fit(x_poly,y) y_poly_pred = lp.predict(x_poly)plt.scatter(x,y) plt.plot(x,y_poly_pred,'g') plt.show()from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(degree=2) x_poly = poly.fit_transform(x)lrp = LinearRegression() lrp.fit(x_poly,y) plt.scatter(x,y) plt.scatter(x,y_pred) plt.scatter(x,y_poly_pred) #多項回歸 plt.show()?
?
?
?
轉載于:https://www.cnblogs.com/zhanyuki/p/10075761.html
總結
- 上一篇: 7号团队-团队任务3:每日例会(2018
- 下一篇: DBUtils (30)