【问题标题】:Linear regression R-squared is 1.0线性回归 R 平方为 1.0
【发布时间】:2020-12-14 01:34:13
【问题描述】:

我正在对 Python 执行线性回归,以预测在科特迪瓦不同地点分布的 Stock。我有 2016 年到 2019 年 9 月的数据,看起来像 。这些列是。 我在站点代码上使用了标签编码。有 156 个不同的站点,每个站点都标记为 0-155。同样,我使用 Get_dummies 函数为 11 个不同的产品代码获取 11 个不同的列。 然后我使用线性回归来帮助预测输出,令我惊讶的是,R 平方值为 100%。 代码:


lm=sm.OLS(df_logistics_new_onehot_label['stock_distributed'],df_logistics_new_onehot_label[['intercept','year', 'month','site_code', 'stock_initial',
       'stock_received', 'stock_adjustment', 'stock_end',
       'average_monthly_consumption', 'stock_stockout_days', 'stock_ordered',
       'site_latitude', 'site_longitude',
       'product_code_AS21126', 'product_code_AS27000',
       'product_code_AS27132', 'product_code_AS27133', 'product_code_AS27134',
       'product_code_AS27137', 'product_code_AS27138', 'product_code_AS27139',
       'product_code_AS42018', 'product_code_AS46000',
       'site_type_Health Center',
       'site_type_University Hospital/National Institute']])

results=lm.fit()
results.summary()

回归的输出看起来像这样

我进一步将数据分为训练和测试

X=df_logistics_new_onehot_label[['intercept','year', 'month','site_code', 'stock_initial',
       'stock_received', 'stock_adjustment', 'stock_end',
       'average_monthly_consumption', 'stock_stockout_days', 'stock_ordered',
       'site_latitude', 'site_longitude',
       'product_code_AS21126', 'product_code_AS27000',
       'product_code_AS27132', 'product_code_AS27133', 'product_code_AS27134',
       'product_code_AS27137', 'product_code_AS27138', 'product_code_AS27139',
       'product_code_AS42018', 'product_code_AS46000',
       'site_type_Health Center',
       'site_type_University Hospital/National Institute']]
y=df_logistics_new_onehot_label['stock_distributed']

X_train, X_test, y_train, y_test = train_test_split(X, y ,test_size = 0.2, shuffle=False)

clf=LinearRegression()

clf.predict(X_test)

20% 数据的线性回归输出与“股票分布”变量完全匹配,您可以在此处看到 是模型过拟合还是我做错了什么?

【问题讨论】:

  • 您的问题是什么?您的输入的预期输出是什么?
  • training 数据的 R 平方值为 1.0 一点也不稀奇。
  • @desertnaut 请查看我的编辑。线性回归的输出与我保留用于测试的 20% 数据完全匹配。这是常见的还是我做错了什么?
  • @jlandercy 请查看我的编辑。模型如何 100% 准确。它是过拟合的,对吧?

标签: python machine-learning linear-regression statsmodels


【解决方案1】:

您的目标变量与以下列完美相关:

  • 'stock_initial',
  • 'stock_received'
  • 'stock_adjustment'
  • 'stock_end'

从逻辑上讲,这些是相关的。先尝试删除这些列,然后再次尝试线性回归。

【讨论】:

  • 感谢您的评论。我会再试一次
猜你喜欢
  • 2018-08-20
  • 2020-09-20
  • 2017-03-24
  • 1970-01-01
  • 2018-12-04
  • 2011-08-01
  • 2013-02-11
相关资源
最近更新 更多