我已经在线阅读了文档和各种来源,但我不确定如何在 sklearn 中为线性回归模型实现 SGD 的动量变体。任何帮助我入门的帮助将不胜感激。
from sklearn import linear_model
from sklearn.datasets import load_boston
X,y = load_boston().data,load_boston().target
clf = linear_model.SGDRegressor(loss='squared_loss',penalty='l2',alpha=0.01,max_iter=1000)
clf.fit(X, y)
print('Score:',clf.score(X,y))
print('Regression coefficients:',clf.coef_)
print('deviation:',clf.intercept_ )
答案 0 :(得分:1)
一种解决方法是使用没有隐藏层的 MLPRegressor
,这与执行 LinearRegression
是一样的。
这将允许您使用 SGD
的动量变化,例如 adam
。
您可以执行以下操作:
from sklearn.neural_network import MLPRegressor
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
X, y = make_regression(n_samples=5_000, random_state=1, n_features=10)
X_train, X_test, y_train, y_test = train_test_split(X, y)
clf = MLPRegressor(solver='adam',
alpha=0.01,
max_iter=5000,
hidden_layer_sizes=(),
)
clf.fit(X_train, y_train)
print('Score:',clf.score(X_test,y_test))
print('Regression coefficients:',clf.coefs_[0])
print('deviation:',clf.intercepts_ [0])