为什么我的 SGD 与我的线性回归模型相去甚远?

Why my SGD is far off than my linear regression model?

我正在尝试将线性回归(正规方程)与 SGD 进行比较,但看起来 SGD 相去甚远。我做错了什么吗?

这是我的代码

x = np.random.randint(100, size=1000)
y = x * 0.10
slope, intercept, r_value, p_value, std_err = stats.linregress(x=x, y=y)
print("slope is %f and intercept is %s" % (slope,intercept))
#slope is 0.100000 and intercept is 1.61435309565e-11

这是我的 SGD

x = x.reshape(1000,1)
clf = linear_model.SGDRegressor()
clf.fit(x, y, coef_init=0, intercept_init=0)

print(clf.intercept_)
print(clf.coef_)

#[  1.46746270e+10]
#[  3.14999003e+10]

我本以为coefintercept几乎一样,因为数据是线性的。

当我尝试 运行 这段代码时,出现溢出错误。我怀疑您遇到了同样的问题,但出于某种原因,它并没有引发错误。

如果缩小功能,一切都会按预期进行。使用 scipy.stats.linregress:

>>> x = np.random.random(1000) * 10
>>> y = x * 0.10
>>> slope, intercept, r_value, p_value, std_err = stats.linregress(x=x, y=y)
>>> print("slope is %f and intercept is %s" % (slope,intercept))
slope is 0.100000 and intercept is -2.22044604925e-15

使用linear_model.SGDRegressor

>>> clf.fit(x[:,None], y)
SGDRegressor(alpha=0.0001, epsilon=0.1, eta0=0.01, fit_intercept=True,
       l1_ratio=0.15, learning_rate='invscaling', loss='squared_loss',
       n_iter=5, penalty='l2', power_t=0.25, random_state=None,
       shuffle=False, verbose=0, warm_start=False)
>>> print("slope is %f and intercept is %s" % (clf.coef_, clf.intercept_[0]))
slope is 0.099763 and intercept is 0.00163353754797

slope 的值略低,但我猜这是因为正则化。