我正在尝试在网上看到的训练数据集上的代码,但似乎无法解决上述错误。
当我第一次运行代码时,我得到了上述错误:
ValueError Traceback (most recent call last)
----> 2 knn_cv.fit(X_train, y_train)
<ipython-input-21-fb975450c609> in fit(self, X, y)
214 X = normalize(X, norm='l1', copy=False)
215
--> 216 cv = check_cv(self.cv, X, y)
/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_split.py in
check_cv(cv, y, classifier)
1980
1981 if isinstance(cv, numbers.Integral):
-> 1982 if (classifier and (y is not None) and
1983 (type_of_target(y) in ('binary', 'multiclass'))):
1984 return StratifiedKFold(cv)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
错误似乎在check_cv函数中,并且看起来y_train抛出了布尔值,但我不确定如何修改它。我知道原因是“ and”语句,该语句通常可以修改,但是在这种情况下,错误驻留在check_cv函数中,并且我不确定如何修改该语句。 我尝试了使用a.any()或a.all()的建议操作,但每次都会抛出新错误。
如果我使用 y_train.any()它给我一个错误:
269 if y.ndim > 2 or (y.dtype == object and len(y) and
270 not isinstance(y.flat[0], str)):
--> 271 return 'unknown' # [[[1, 2]]] or [obj_1] and not
["label_1"]
272
273 if y.ndim == 2 and y.shape[1] == 0:
TypeError: len() of unsized object
如果我使用y_train.all(),它说 TypeError:'KFold'对象不可迭代
另一个查询建议将数组更改为列表,但这给了我
np.array(y_train).tolist()
结果:
TypeError:未缩放对象的len()
也更新了sklearn,但似乎无法解决该错误。希望有人可以解释什么地方出了问题,或者我该如何修改代码(如果可能的话,也请解释一下)。 我对这部分代码还是不熟悉)
使用GoogleNews-vectors-negative300.bin.gz创建的培训样本
y_train = array([3,17,14,14,14,5,13,... 0,1,17,16,2])
y_train.shape()=(100,)
X_train =类型''的<100x5100稀疏矩阵 以压缩的稀疏行格式存储了10049个元素>
X = check_array(X_train, accept_sparse='csr', copy=True)
print(X)
(0, 679) 1.0
(0, 701) 1.0
(0, 1851) 2.0
(0, 1889) 1.0
(0, 2498) 1.0
(0, 2539) 1.0
(0, 2589) 1.0
(0, 2679) 1.0...
X.shape = (100, 5100)
我附上了代码的主要部分,如果您需要整个内容的引用,请在下面提供链接 http://vene.ro/blog/word-movers-distance-in-python.html
def fit(self, X, y):
if self.n_neighbors_try is None:
n_neighbors_try = range(1, 6)
else:
n_neighbors_try = self.n_neighbors_try
X = check_array(X, accept_sparse='csr', copy=True)
X = normalize(X, norm='l1', copy=False)
cv = check_cv(self.cv, X, y)
knn = KNeighborsClassifier(metric='precomputed', algorithm='brute')
scorer = check_scoring(knn, scoring=self.scoring)
scores = []
for train_ix, test_ix in cv:
dist = self._pairwise_wmd(X[test_ix], X[train_ix])
knn.fit(X[train_ix], y[train_ix])
scores.append([
scorer(knn.set_params(n_neighbors=k), dist, y[test_ix])
for k in n_neighbors_try
])
scores = np.array(scores)
self.cv_scores_ = scores
best_k_ix = np.argmax(np.mean(scores, axis=0))
best_k = n_neighbors_try[best_k_ix]
self.n_neighbors = self.n_neighbors_ = best_k
return super(WordMoversKNNCV, self).fit(X, y)
knn_cv = WordMoversKNNCV(cv=3,n_neighbors_try=range(1, 20),
W_embed=W_common, verbose=5, n_jobs=3)
knn_cv.fit(X_train, y_train.all())
根据作者,我应该得到这个:
[Parallel(n_jobs=3)]: Done 12 tasks | elapsed: 30.8s
[Parallel(n_jobs=3)]: Done 34 out of 34 | elapsed: 2.0min finished
[Parallel(n_jobs=3)]: Done 12 tasks | elapsed: 25.7s
[Parallel(n_jobs=3)]: Done 33 out of 33 | elapsed: 2.9min finished
[Parallel(n_jobs=3)]: Done 12 tasks | elapsed: 53.3s
[Parallel(n_jobs=3)]: Done 33 out of 33 | elapsed: 2.0min finished
WordMoversKNNCV(W_embed=memmap([[ 0.04283, -0.01124, ..., -0.05679, -0.00763],
[ 0.02884, -0.05923, ..., -0.04744, 0.06698],
...,
[ 0.08428, -0.15534, ..., -0.01413, 0.04561],
[-0.02052, 0.08666, ..., 0.03659, 0.10445]]),
cv=3, n_jobs=3, n_neighbors_try=range(1, 20), scoring=None,
verbose=5)
答案 0 :(得分:2)
您使用的check_cv
错误。根据{{3}}:-
check_cv(cv=’warn’, y=None, classifier=False):
cv : int,
cross-validation generator or an iterable, optional
y : array-like, optional
The target variable for supervised learning problems.
classifier : boolean, optional, default False
Whether the task is a classification task,
in which case stratified KFold will be used
因此它希望输入y
和estimator
。但是您提供的X
和y
是错误的。更改以下几行:
cv = check_cv(self.cv, X, y)
knn = KNeighborsClassifier(metric='precomputed', algorithm='brute')
收件人:
knn = KNeighborsClassifier(metric='precomputed', algorithm='brute')
cv = check_cv(self.cv, y, knn)
注意行的顺序。