我试图为回归建立一个简单的测试用例,一个x的线性函数,但是sgdregrestor给出了一个错误的结果
import numpy as np
from sklearn.linear_model import SGDRegressor
from random import random
X = np.array(range(1000))
y = np.array([x + random() for x in X])
X = X.reshape(1000,1)
sgd = SGDRegressor()
sgd.fit(X, y)
print [sgd.intercept_, sgd.coef_]
[array([ -4.13761484e+08]), array([ -9.66320825e+10])]
我认为这与这样一个事实有关:在0-1000的整数中添加
random()
对int的影响很小,因为它们越大。使用StandardScaler
作为预处理步骤的功能缩放可能会有所帮助。在根据Sklearn的实用技巧:
在修改了您的示例并没有使用特性缩放之后,我注意到产生差异的参数组合是:loss、nUiter、eta0和power\t是需要重点关注的参数组合-
eta0
是主要参数。SGDRegressor的默认值对于这个问题来说太高了。在打印结果如下:
^{pr2}$这是打印出来的样子(注意:每次重新运行时都会发生变化,因此您的输出看起来可能与我的不同):
有趣的是,当其他三个损失函数保持在预期范围内时,
squared_epsilon_insensitive
的y轴突然消失。在为了好玩,将
power_t
从0.15改为0.5。产生影响的原因是默认的learning_rate
参数是由eta = eta0 / pow(t, power_t)
计算的'invscaling'
尝试设置低于默认值
0.01
的初始学习率,例如:输出:
^{pr2}$编辑:我不确定确切的原因,但是}中包含的大值似乎会引起一些数值稳定性问题。在
X
和{SGDRegressor
中设置verbose=1
,它将显示具有默认学习率的以下输出:这意味着内部计算已经溢出了。使用
eta=0.000001
:另一种可能的方法是预先将数据(输入和输出)调整到正常范围,例如使用
StandardScaler
。预处理完成后,默认参数工作良好。在相关问题 更多 >
编程相关推荐