我只是一个初学者,我正试图在scikit-learn中实现多项式回归。没有正规化的通常回归工作正常
regr = linear_model.LinearRegression(copy_X=True)
X = np.array(time_list[0:24]).reshape(24,1)
for i in range(2,10):
X=np.append(X, X**i, 1)
Y = np.array(tempm_list[0:24]).reshape(24,1)
regr.fit(X, Y)
但是当我尝试以完全相同的方式实现Ridge回归时,我收到以下错误:
regularized_regr=linear_model.Ridge(alpha =1, copy_X=True)
regularized_regr.fit(X,Y)
File "/usr/local/lib/python2.7/site-packages/sklearn/linear_model/ridge.py", line 449, in fit
return super(Ridge, self).fit(X, y, sample_weight=sample_weight)
File "/usr/local/lib/python2.7/site-packages/sklearn/linear_model/ridge.py", line 338, in fit
solver=self.solver)
File "/usr/local/lib/python2.7/site-packages/sklearn/linear_model/ridge.py", line 294, in ridge_regression
coef = safe_sparse_dot(X.T, dual_coef, dense_output=True).T
UnboundLocalError: local variable 'dual_coef' referenced before assignment
由于
答案 0 :(得分:1)
第一个建议:将多项式度数降低到例如: < = 5.根据您的样本数量,上述任何内容都将进入过度拟合领域
第二个建议:升级Scikit学习到最前沿的github版本,这似乎是一个与异常相关的错误,因为你的矩阵是单数。
如果您无法升级scikit learn,请尝试使用更强大的正规化:
import numpy as np
_, S, _ = np.linalg.svd(X, full_matrices=False)
s = S[0]
alpha = 1.2 * s # you may vary this fraction between 0.1 and larger
regularized_regr=linear_model.Ridge(alpha=alpha)
regularized_regr.fit(X,Y)