使用economics数据集进行线性回归时,SGDRegressor拟合图像与LinearRegression差异过大的问题求助
economics数据集进行线性回归时,SGDRegressor拟合图像与LinearRegression差异过大的问题求助
我最近在用ggplot2内置的economics数据集做线性回归练习,本来想对比SGDRegressor和LinearRegression的拟合效果,结果发现SGD的拟合图完全不对,和LinearRegression的结果差得特别多,实在搞不懂哪里出问题了。
先贴一下我的代码:
x = df.loc[:,'pce'].values.reshape(-1,1) y = df.loc[:,'psavert'].values.reshape(-1,1) from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(x,y, test_size = 0.2) from sklearn.linear_model import SGDRegressor sr = SGDRegressor(eta0 = 0.001, verbose = 1) sr.fit(x_train,y_train.flatten()) plt.scatter(x_train,y_train, s = 5, alpha = 0.3, c = 'blue') plt.plot(x_train,sr.predict(x_train), c = 'green') plt.xlabel('pce(billions)') plt.ylabel('saving rate') plt.show()
具体的问题表现是:SGDRegressor拟合出来的直线完全不符合数据的趋势,看起来像是一条和数据走向完全偏离的直线;而用LinearRegression拟合出来的直线则正常贴合了数据的整体趋势。
我已经尝试过调整eta0(学习率)的大小,也修改过max_iter(最大迭代次数),但不管怎么调,SGD的拟合结果还是不对,有没有大佬能帮我看看问题出在哪?
备注:内容来源于stack exchange,提问作者靹滌頋�,1"
相关产品推荐
相关产品推荐

