【線性回歸】波士頓房價預測


from sklearn import datasets # 數據集
from sklearn.model_selection import train_test_split from sklearn import linear_model import matplotlib.pyplot as plt 

 

boston = datasets.load_boston() # 波士頓房價數據
boston

 

 

 

 

# 創建訓練集 與 測試集
x_train,x_test,y_train,y_test = train_test_split(boston.data,boston.target,test_size=0.1,random_state=42) print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)

 # 訓練數據
 linreg = linear_model.LinearRegression()
 linreg.fit(x_train, y_train)

 

 # 得出預測值

 y_pred = linreg.predict(x_test)
 y_pred

 

 

 

 

 

plt.figure(figsize=(10,6)) # 設置大小
 plt.plot(y_test,linewidth=3,label='Actual') plt.plot(y_pred,linewidth=3,label='Prediction') # 顯示上面設置的名字與底部
plt.legend(loc='best') plt.xlabel('test data point') plt.ylabel('target value')

 

 
         

 

 
         

plt.plot(y_test,y_pred,'o')
plt.plot([-10,60],[-10,60],'k--')
plt.axis([-10,60,-10,60])

 
         

plt.xlabel('Actual')
plt.ylabel('Prediction')

 

 

 

 

 

 

 

有道詞典
# 訓練數據 linreg = ...
詳細 X
# training data Linreg = linear_model. LinearRegression () Linreg. Fit (x_train y_train)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM