【问题标题】:Calculate residual values from trainfset or test set从 trainfset 或测试集计算残差值
【发布时间】:2019-06-16 14:20:30
【问题描述】:

我想执行残差分析,我知道残差等于观察值减去预测值。但我不知道我应该从训练集还是测试集计算残差?

我应该用这个吗:

import statsmodels.api as sm 
# Making predictions
lm = sm.OLS(y_train,X_train).fit()

y_pred = lm.predict(X_train)
resid = y_train - y_pred.to_frame('price')

或者这个:

import statsmodels.api as sm 
# Making predictions
lm = sm.OLS(y_train,X_train).fit()

y_pred = lm.predict(X_test)
resid = y_test- y_pred.to_frame('price')

【问题讨论】:

    标签: python machine-learning linear-regression


    【解决方案1】:

    应根据测试集y_test 的实际值(预期结果)和X_test 的拟合模型的预测值计算残差。将模型拟合到训练集,然后在测试集上测试其准确性。这就是我直观的看法,首先将两个数据集正式称为train(用于训练)然后用于测试(test)的主要原因。

    具体来说,使用第二种情况

    resid = y_test- y_pred.to_frame('price')
    

    【讨论】:

    • 为了回归的目的,测试集和训练集必须分开,但是如果你想在回归完成后了解整个数据集的建模,那么这不会这样做,所有数据都会使用。
    • @JamesPhillips 所以你说残差分析应该在拆分之前完成。我没听懂。你能解释一下吗?谢谢
    • 选择模型后,可能包括从模型中删除不重要的预测变量,您可能希望在整个数据集上运行模型以进行最终统计分析。
    猜你喜欢
    • 2016-01-09
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 2018-08-17
    • 2021-12-23
    • 1970-01-01
    • 2017-06-03
    相关资源
    最近更新 更多