亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么線性回歸的預測值與真實值完全相同?

為什么線性回歸的預測值與真實值完全相同?

白豬掌柜的 2023-09-05 20:53:59
我正在做回歸LinearRegression并得到均方誤差0。我認為應該有一些偏差(至少很?。?。您能解釋一下這個現象嗎?## Import packagesimport numpy as npimport pandas as pdfrom sklearn.linear_model import LinearRegressionfrom sklearn.metrics import mean_squared_errorimport urllib.request## Import dataseturllib.request.urlretrieve('https://raw.githubusercontent.com/Data-Science-FMI/ml-from-scratch-2019/master/data/house_prices_train.csv',                           'house_prices_train.csv')df_train = pd.read_csv('house_prices_train.csv')x = df_train['GrLivArea'].values.reshape(1, -1)y = df_train['SalePrice'].values.reshape(1, -1)print('The explanatory variable is', x)print('The variable to be predicted is', y)## Regressionreg = LinearRegression().fit(x, y)mean_squared_error(y, reg.predict(x))print('The MSE is', mean_squared_error(y, reg.predict(x)))print('Predicted value is', reg.predict(x))print('True value is', y)結果是The explanatory variable is [[1710 1262 1786 ... 2340 1078 1256]]The variable to be predicted is [[208500 181500 223500 ... 266500 142125 147500]]The MSE is 0.0Predicted value is [[208500. 181500. 223500. ... 266500. 142125. 147500.]]True value is [[208500 181500 223500 ... 266500 142125 147500]]
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

雖然模型在其自身訓練集上的得分會被夸大的評論肯定是正確的,但它不太可能與線性回歸完美契合,尤其是只有一個特征。

您的問題是您錯誤地重塑了數據:reshape(1, -1)創建了一個 shape 數組(1, n),因此您的模型認為它具有僅單個樣本的n特征和n輸出,因此具有完美擬合的多元線性回歸。嘗試使用reshape(-1, 1)forx而不是重塑 for y。


查看完整回答
反對 回復 2023-09-05
  • 1 回答
  • 0 關注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號