栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在sklearn中的LinearRegression方法中,fit_intercept参数究竟在做什么?[关闭]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在sklearn中的LinearRegression方法中,fit_intercept参数究竟在做什么?[关闭]

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

from sklearn.linear_model import LinearRegressionfrom sklearn.datasets import make_regressionimport numpy as npimport matplotlib.pyplot as pltbias = 100X = 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 + noiselr_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.32210Intercept when fit_intercept=False : 0.00000

从视觉上可以清楚地看到是什么

fit_intercept
。当
fit_intercept=True
为时,允许最佳拟合线“拟合”
y轴(在此示例中接近100)。当为时
fit_intercept=False
,截距将强制为原点(0,0)。


如果我包括一列一或零并设置

fit_intercept
为True或False,会发生什么情况?

下面显示了如何检查此示例。

from sklearn.linear_model import LinearRegressionfrom sklearn.datasets import make_regressionimport numpy as npimport matplotlib.pyplot as pltnp.random.seed(1)bias = 100X = 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 onesX_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 ones104.156765787 [ 0.29634031]# fit_intercept=False, no column of zeros or ones0.0 [ 0.45265361]# fit_intercept=True, column of zeros or ones104.156765787 [ 0.          0.29634031]# fit_intercept=False, column of zeros or ones0.0 [ 104.15676579    0.29634031]


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611931.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号