我正在做回歸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
。
添加回答
舉報
0/150
提交
取消