【问题标题】:Does scikit learn provide an easier solution to getting a confidence level for the coefficient?scikit learn 是否提供了一种更简单的解决方案来获得系数的置信度?
【发布时间】:2020-11-17 04:02:51
【问题描述】:

我正在学习《统计学习简介》,但我无法使用 scikit learn 得出系数的置信区间(我正在尝试在 Python 中复制代码)。

X = df.iloc[:, :-1].values
y = df.iloc[:, 1].values


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state = 0)


classifier = LinearRegression()
classifier.fit(X_train, y_train)

#predciting on X_test
y_pred = classifier.predict(X_test)


print(r2_score(y_pred, y_test))

print(classifier.intercept_)
print(classifier.coef_)

使用标准差和 95% 置信水平:

stdev = np.sqrt(sum((classifier.predict(X_train) - y_train)**2) / (len(y_train) - 2))


prediction = (y_pred - 1.96*stdev, y_pred + 1.96*stdev)

使用 OLS:

ols = sm.OLS(X, y)
ols_result = ols.fit()
print(ols_result.summary())


print(ols_result.bse)



ci = classifier.coef_ + 2 * ols_result.bse
ci2 = classifier.coef_ - 2 * ols_result.bse

final_ci = ci + ci2

我得到了不同的结果,所以我不完全确定哪一个更准确。有什么建议? scikit learn 是否提供了一种更简单的解决方案来获得系数的置信度?

【问题讨论】:

    标签: python scikit-learn linear-regression


    【解决方案1】:

    【讨论】:

    • 我看过这个,但我不明白预测部分 - 1.96*stdev。我尝试了以下 - 这是准确的吗? ci1 = y_pred - 1.96*stdev ci2 = y_pred + 1.96*stdev
    猜你喜欢
    • 2016-12-26
    • 2022-01-24
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多