【问题标题】:In the LinearRegression method in sklearn, what exactly is the fit_intercept parameter doing? [closed]在sklearn的LinearRegression方法中,fit_intercept参数到底是干什么的? [关闭]
【发布时间】:2018-03-28 12:33:13
【问题描述】:

sklearn.linear_model.LinearRegression方法中,有一个参数是fit_intercept = TRUEfit_intercept = FALSE。我想知道我们是否将其设置为 TRUE,它是否会在您的数据集中添加一个全为 1 的额外截距列?如果我已经有一个包含 1 列的数据集,fit_intercept = FALSE 是否说明了这一点,还是强制它适应零截距模型?

更新:似乎人们不明白我的问题。问题是,如果我的预测变量数据集中已经有一列 1(1 用于截距),该怎么办?那么,

  1. 如果我使用fit_intercept = FALSE,它会删除 1 的列吗?

  2. 如果我使用fit_intercept = TRUE,它会添加一个额外的 1 列吗?

【问题讨论】:

  • 请查看this questionthisthis
  • 我的问题与所有 3 个问题都无关,我已相应地对其进行了更新以进行更多说明。

标签: python scikit-learn linear-regression


【解决方案1】:

fit_intercept=False 将 y 截距设置为 0。如果 fit_intercept=True,y 截距将由最佳拟合线确定。

from sklearn.linear_model import LinearRegression
from sklearn.datasets import make_regression
import numpy as np
import matplotlib.pyplot as plt

bias = 100

X = np.arange(1000).reshape(-1,1)
y_true = np.ravel(X.dot(0.3) + bias)
noise = np.random.normal(0, 60, 1000)
y = y_true + noise

lr_fi_true = LinearRegression(fit_intercept=True)
lr_fi_false = LinearRegression(fit_intercept=False)

lr_fi_true.fit(X, y)
lr_fi_false.fit(X, y)

print('Intercept when fit_intercept=True : {:.5f}'.format(lr_fi_true.intercept_))
print('Intercept when fit_intercept=False : {:.5f}'.format(lr_fi_false.intercept_))

lr_fi_true_yhat = np.dot(X, lr_fi_true.coef_) + lr_fi_true.intercept_
lr_fi_false_yhat = np.dot(X, lr_fi_false.coef_) + lr_fi_false.intercept_

plt.scatter(X, y, label='Actual points')
plt.plot(X, lr_fi_true_yhat, 'r--', label='fit_intercept=True')
plt.plot(X, lr_fi_false_yhat, 'r-', label='fit_intercept=False')
plt.legend()

plt.vlines(0, 0, y.max())
plt.hlines(bias, X.min(), X.max())
plt.hlines(0, X.min(), X.max())

plt.show()

这个例子打印:

Intercept when fit_intercept=True : 100.32210
Intercept when fit_intercept=False : 0.00000

很明显fit_intercept 做了什么。当fit_intercept=True 时,允许最佳拟合线“拟合”y 轴(在本例中接近100)。 fit_intercept=False时,强制截取原点(0, 0)。


如果我包含一列 1 或 0 并将 fit_intercept 设置为 True 或 False,会发生什么?

下面显示了一个如何检查的示例。

from sklearn.linear_model import LinearRegression
from sklearn.datasets import make_regression
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(1)
bias = 100

X = np.arange(1000).reshape(-1,1)
y_true = np.ravel(X.dot(0.3) + bias)
noise = np.random.normal(0, 60, 1000)
y = y_true + noise

# with column of ones
X_with_ones = np.hstack((np.ones((X.shape[0], 1)), X))

for b,data in ((True, X), (False, X), (True, X_with_ones), (False, X_with_ones)):
  lr = LinearRegression(fit_intercept=b)
  lr.fit(data, y)

  print(lr.intercept_, lr.coef_)

外卖:

# fit_intercept=True, no column of zeros or ones
104.156765787 [ 0.29634031]
# fit_intercept=False, no column of zeros or ones
0.0 [ 0.45265361]
# fit_intercept=True, column of zeros or ones
104.156765787 [ 0.          0.29634031]
# fit_intercept=False, column of zeros or ones
0.0 [ 104.15676579    0.29634031]

【讨论】:

  • 如果我已经在我的一组预测变量列中包含了一列,如果我使用 TRUE 然后 FALSE 拟合它会发生什么?
  • 图片有误吗?虚线应该是 fit_intercept = False,实线应该是 fit_intercept = True,对吧?
  • @HuyTruong 是什么让你这么认为?
  • 天哪,我很抱歉。我的错。我快速浏览了一下情节,认为 y = 100 是 y = 0。(所以虚线穿过 y = 0,这就是为什么我声称图中有错误)。
猜你喜欢
  • 1970-01-01
  • 2010-11-06
  • 2020-05-28
  • 1970-01-01
  • 2018-03-04
  • 2020-02-05
  • 2018-07-27
  • 2016-12-29
  • 1970-01-01
相关资源
最近更新 更多