【问题标题】:Get confidence intervall from sklearn linear regression in python从python中的sklearn线性回归获取置信区间
【发布时间】:2020-08-01 04:26:14
【问题描述】:

我想要一个线性回归结果的置信区间。我正在使用波士顿房价数据集。

我发现了这个问题: How to calculate the 99% confidence interval for the slope in a linear regression model in python? 但是,这并不能完全回答我的问题。

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from math import pi

import pandas as pd
import seaborn as sns

# import the data
from sklearn.datasets import load_boston
boston_dataset = load_boston()

boston = pd.DataFrame(boston_dataset.data, columns=boston_dataset.feature_names)
boston['MEDV'] = boston_dataset.target

X = pd.DataFrame(np.c_[boston['LSTAT'], boston['RM']], columns=['LSTAT', 'RM'])
Y = boston['MEDV']

from sklearn.model_selection import train_test_split

# splits the training and test data set in 80% : 20%
# assign random_state to any value.This ensures consistency.
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=5)

from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

lin_model = LinearRegression()
lin_model.fit(X_train, Y_train)

# model evaluation for training set

y_train_predict = lin_model.predict(X_train)
rmse = (np.sqrt(mean_squared_error(Y_train, y_train_predict)))
r2 = r2_score(Y_train, y_train_predict)

# model evaluation for testing set

y_test_predict = lin_model.predict(X_test)
# root mean square error of the model
rmse = (np.sqrt(mean_squared_error(Y_test, y_test_predict)))

# r-squared score of the model
r2 = r2_score(Y_test, y_test_predict)

plt.scatter(Y_test, y_test_predict)
plt.show()

如何从中获得例如 95% 或 99% 的置信区间?是否有某种内置函数或代码?

【问题讨论】:

标签: python scikit-learn linear-regression


【解决方案1】:

也许您必须自己构建它,或者您必须为此使用statsmodel。根据 sklearn 文档:docs,它没有那个 conf inte。
或者您可以按照本指南:medium

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-13
    • 2019-08-29
    • 1970-01-01
    • 2018-02-05
    • 2018-05-16
    • 2012-09-25
    • 2017-10-13
    • 2023-03-26
    相关资源
    最近更新 更多