sklearn簡單線性回歸


 

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# 加載數據,波士頓房價
boston = datasets.load_boston()
x, y = boston.data, boston.target

# 划分訓練集和檢驗集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=10010)

# 使用訓練集訓練模型
reg = LinearRegression()
reg.fit(x_train, y_train)

# 使用模型進行預測
y_predict = reg.predict(x_test)

# 計算模型的預測值與真實值之間的均方誤差MSE
print(mean_squared_error(y_test, y_predict))

 


免責聲明!

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



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