scikit-learn 中岭回归的系数路径
Coefficient paths for Ridge Regression in scikit-learn
从 pandas DataFrame 开始,d_train
(774 行):
思路是按照示例here 研究岭系数路径。
在该示例中,变量类型如下:
X, y, w = make_regression(n_samples=10, n_features=10, coef=True,
random_state=1, bias=3.5)
print X.shape, type(X), y.shape, type(y), w.shape, type(w)
>> (10, 10) <type 'numpy.ndarray'> (10,) <type 'numpy.ndarray'> (10,) <type'numpy.ndarray'>
为了避免 中提到的问题,我将所有内容都转换为 numpy 数组:
predictors = ['p1', 'p2', 'p3', 'p4']
target = ['target_bins']
X = d_train[predictors].as_matrix()
### X = np.transpose(d_train[predictors].as_matrix())
y = d_train['target_bins'].as_matrix()
w = numpy.full((774,), 3, dtype=float)
print X.shape, type(X), y.shape, type(y), w.shape, type(w)
>> (774, 4) <type 'numpy.ndarray'> y_shape: (774,) <type 'numpy.ndarray'> w_shape: (774,) <type 'numpy.ndarray'>
然后我 运行
(a) 示例中的确切代码,
(b) 将参数 fit_intercept = True, normalize = True
添加到 ridge 调用中(我的数据未按比例缩放)
得到相同的错误信息:
my_ridge = Ridge()
coefs = []
errors = []
alphas = np.logspace(-6, 6, 200)
for a in alphas:
my_ridge.set_params(alpha=a, fit_intercept = True, normalize = True)
my_ridge.fit(X, y)
coefs.append(my_ridge.coef_)
errors.append(mean_squared_error(my_ridge.coef_, w))
>> ValueError: Found input variables with inconsistent numbers of samples: [4, 774]
正如代码的注释部分所示,我还尝试了 "same" 代码,但使用了 t运行sposed X 矩阵。在创建 X matrix
之前,我还尝试了 缩放 数据。收到相同的错误消息。
最后,我使用 'RidgeClassifier' 做了同样的事情,并得到了不同的错误消息。
>> Found input variables with inconsistent numbers of samples: [1, 774]
问题:我不知道这里发生了什么——你能帮忙吗?
在 Canopy 1.7.4.3348(64 位)上使用 python 2.7,scikit-learn 18.01-3 和 pandas 0.19.2-2
谢谢。
您需要拥有与特征数量一样多的权重 w
(因为您为每个特征学习了一个权重),但在您的代码中,权重向量的维数是 774(这是数字训练数据集中的行),这就是它不起作用的原因。将代码修改为以下内容(改为使用 4 个权重),一切都会起作用:
w = np.full((4,), 3, dtype=float) # number of features = 4, namely p1, p2, p3, p4
print X.shape, type(X), y.shape, type(y), w.shape, type(w)
#(774L, 4L) <type 'numpy.ndarray'> (774L,) <type 'numpy.ndarray'> (4L,) <type 'numpy.ndarray'>
现在您可以 运行 来自 http://scikit-learn.org/stable/auto_examples/linear_model/plot_ridge_coeffs.html#sphx-glr-auto-examples-linear-model-plot-ridge-coeffs-py 的其余代码,以查看权重和误差如何随正则化参数 alpha
和网格搜索而变化,并获得以下内容数字
从 pandas DataFrame 开始,d_train
(774 行):
思路是按照示例here 研究岭系数路径。
在该示例中,变量类型如下:
X, y, w = make_regression(n_samples=10, n_features=10, coef=True,
random_state=1, bias=3.5)
print X.shape, type(X), y.shape, type(y), w.shape, type(w)
>> (10, 10) <type 'numpy.ndarray'> (10,) <type 'numpy.ndarray'> (10,) <type'numpy.ndarray'>
为了避免
predictors = ['p1', 'p2', 'p3', 'p4']
target = ['target_bins']
X = d_train[predictors].as_matrix()
### X = np.transpose(d_train[predictors].as_matrix())
y = d_train['target_bins'].as_matrix()
w = numpy.full((774,), 3, dtype=float)
print X.shape, type(X), y.shape, type(y), w.shape, type(w)
>> (774, 4) <type 'numpy.ndarray'> y_shape: (774,) <type 'numpy.ndarray'> w_shape: (774,) <type 'numpy.ndarray'>
然后我 运行
(a) 示例中的确切代码,
(b) 将参数 fit_intercept = True, normalize = True
添加到 ridge 调用中(我的数据未按比例缩放)
得到相同的错误信息:
my_ridge = Ridge()
coefs = []
errors = []
alphas = np.logspace(-6, 6, 200)
for a in alphas:
my_ridge.set_params(alpha=a, fit_intercept = True, normalize = True)
my_ridge.fit(X, y)
coefs.append(my_ridge.coef_)
errors.append(mean_squared_error(my_ridge.coef_, w))
>> ValueError: Found input variables with inconsistent numbers of samples: [4, 774]
正如代码的注释部分所示,我还尝试了 "same" 代码,但使用了 t运行sposed X 矩阵。在创建 X matrix
之前,我还尝试了 缩放 数据。收到相同的错误消息。
最后,我使用 'RidgeClassifier' 做了同样的事情,并得到了不同的错误消息。
>> Found input variables with inconsistent numbers of samples: [1, 774]
问题:我不知道这里发生了什么——你能帮忙吗?
在 Canopy 1.7.4.3348(64 位)上使用 python 2.7,scikit-learn 18.01-3 和 pandas 0.19.2-2
谢谢。
您需要拥有与特征数量一样多的权重 w
(因为您为每个特征学习了一个权重),但在您的代码中,权重向量的维数是 774(这是数字训练数据集中的行),这就是它不起作用的原因。将代码修改为以下内容(改为使用 4 个权重),一切都会起作用:
w = np.full((4,), 3, dtype=float) # number of features = 4, namely p1, p2, p3, p4
print X.shape, type(X), y.shape, type(y), w.shape, type(w)
#(774L, 4L) <type 'numpy.ndarray'> (774L,) <type 'numpy.ndarray'> (4L,) <type 'numpy.ndarray'>
现在您可以 运行 来自 http://scikit-learn.org/stable/auto_examples/linear_model/plot_ridge_coeffs.html#sphx-glr-auto-examples-linear-model-plot-ridge-coeffs-py 的其余代码,以查看权重和误差如何随正则化参数 alpha
和网格搜索而变化,并获得以下内容数字