预测线性回归的准确性

数据挖掘 scikit-学习 线性回归
2021-10-07 03:43:14

如何测试线性回归模型中的预测值是否与实际值匹配?

我尝试使用 - 混淆矩阵,但出现此错误 -

#==============================================================================
# Create confusion matrix to evaluate performance of data
#==============================================================================
from sklearn.metrics import confusion_matrix
confusionMatrix = confusion_matrix (dv_test, y_pred)

print(confusionMatrix)

ValueError: Can't handle mix of multiclass and continuous

当我执行以下代码时 -

##Performing Linear Regression
from sklearn.linear_model import LinearRegression
from sklearn import model_selection
regressor=LinearRegression()
##Fit train
regressor.fit(iv_train,dv_train)
y_pred=regressor.predict(iv_test)
print('Accuracy of LR',mean_squared_error(y_pred,dv_test))

结果 - LR 7837176694.18 的准确性

这是不正确的。

以下是我的示例数据集 -

longitude   latitude    housing_median_age  total_rooms total_bedrooms  population  households  median_income   ocean_proximity median_house_value
-122.23 37.88   41  880 129 322 126 8.3252  NEAR BAY    452600
-122.22 37.86   21  7099    1106    2401    1138    8.3014  NEAR BAY    358500
-122.24 37.85   52  1467    190 496 177 7.2574  NEAR BAY    352100
-122.25 37.85   52  1274    235 558 219 5.6431  NEAR BAY    341300
-122.25 37.85   52  1627    280 565 259 3.8462  NEAR BAY    342200
-122.25 37.85   52  919 213 413 193 4.0368  NEAR BAY    269700
-122.25 37.84   52  2535    489 1094    514 3.6591  NEAR BAY    299200
1个回答

有几种方法可以检查线性回归模型的准确性。通常,您可以使用Root mean squared error您可以训练多个线性回归模型,向数据集添加或删除特征,并查看哪个模型的RMSE最低- 在您的情况下是最好的模型。还尝试在适合线性回归模型之前对数据进行规范化。

混淆矩阵用于检查离散结果,但线性回归模型将预测结果作为连续值返回。这就是您收到错误的原因:您的dv_test数据可能是整数,但是y_pred是浮点数。

如果它适合您尝试解决的问题,您可以尝试使用分类模型 - 取决于您尝试预测的内容。但是对于回归问题,最好使用上面提到的度量。