我正在研究Assignment 3: Regularization。在查看了Github之后,我尝试自己解决了这个任务,但是我遇到了运行时错误。请注意,我选择了比链接更小的数据集。
情况就是这样:
print('Training set', train_dataset.shape, train_labels.shape)
print('Validation set', valid_dataset.shape, valid_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)
#Training set (20000, 784) (20000, 10)
#Validation set (1000, 784) (1000, 10)
#Test set (1000, 784) (1000, 10)
这就是问题所在:
from sklearn.linear_model import LogisticRegression
original_train_labels = train_labels
logit_clf = LogisticRegression(penalty='l2')
logit_clf.fit(train_dataset[:1000,:], original_train_labels[:1000])
运行时,给出:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-12-4888dc0bbc75> in <module>()
4
5 logit_clf = LogisticRegression(penalty='l2')
----> 6 logit_clf.fit(train_dataset[:1000,:], original_train_labels[:1000])
7 predicted = logit_clf.predict(test_dataset)
8 print('accuracy', accuracy((np.arange(num_labels) == predicted[:,None]).astype(np.float32), test_labels), '%')
/usr/local/lib/python2.7/dist-packages/sklearn/linear_model/logistic.pyc in fit(self, X, y, sample_weight)
1140
1141 X, y = check_X_y(X, y, accept_sparse='csr', dtype=np.float64,
-> 1142 order="C")
1143 check_classification_targets(y)
1144 self.classes_ = np.unique(y)
/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.pyc in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
513 dtype=None)
514 else:
--> 515 y = column_or_1d(y, warn=True)
516 _assert_all_finite(y)
517 if y_numeric and y.dtype.kind == 'O':
/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.pyc in column_or_1d(y, warn)
549 return np.ravel(y)
550
--> 551 raise ValueError("bad input shape {0}".format(shape))
552
553
ValueError: bad input shape (1000, 10)
有关如何解决这个问题的想法吗?
答案 0 :(得分:1)
您对train_labels使用单热编码。意思是它的形状像[1000。 10],1000个样本,每个样本有10列#39; 1指示我们正在谈论的课程。它是神经网络所必需的,但物流回归来自sklearn requires它的形状[1000,1],这意味着它应该只是一个1000行的向量,并且在每一行中你应该有一个int表示目标类。使用argmax函数将单热编码转换为整数,您应该全部设置。
答案 1 :(得分:1)
Maxim Haytovich是正确的,您的train_labels的形状已经改变,您应该保留火车标签的副本,然后再将其重新格式化为(20000, 10)
。最初train_labels
的形状为(20000,)
,这是一个带有类别标签值的向量,例如[1 2 4 3 ..]
。 Logistic回归需要这些类标签。
当您使用train_dataset, train_labels = reformat(train_dataset, train_labels)
重新格式化时,train_labels变为(20000,10)
,其中之前的[1 2 4 3 ...]
变为[[1 0 0 0 0 ...], [0 1 0 0 ...], [0 0 0 1 0 ...], ...]
,这是神经网络所需但不适用于Logistic回归。
然后您分配了original_train_labels = train_labels
,表示original_train_labels具有相同的(20000, 10)
形状。因此,对于错误的类标签形状,您将获得Value Error。您需要在original_train_labels = train_labels
之前执行步骤train_dataset, train_labels = reformat(train_dataset, train_labels)
。检查以下步骤的顺序,并相应地更正您的代码gsamaras
original_train_labels = train_labels
...
...
train_dataset, train_labels = reformat(train_dataset, train_labels)
...
...
logit_clf.fit(train_dataset[:1000,:], original_train_labels[:1000])