我正在使用scikit learn nearest neighbors查找最近的邻居,在people wiki数据上使用tfidf。
在我的.kneighbors()方法调用中

res = neigh.kneighbors(obama_tfidf, return_distance=False)

模块引发了以下异常:
ValueError: UPDATEIFCOPY base is read-only

我已经在我的github location here上传了完整的代码和样本数据(80mb大小),以供参考。
下面是错误列表的一部分:
---------------------------------------------------------------------------
JoblibValueError                          Traceback (most recent call last)
<ipython-input-12-dbcbed49b042> in <module>()
      1 obama_word_counts = count_vectorizer.transform(['obama'])
      2 obama_tfidf = tfidf_transformer.transform(obama_word_counts)
----> 3 res = neigh.kneighbors(obama_tfidf, return_distance=False)
      4 print res

/usr/local/lib/python2.7/dist-packages/sklearn/neighbors/base.pyc in kneighbors(self, X, n_neighbors, return_distance)
    355             if self.effective_metric_ == 'euclidean':
    356                 dist = pairwise_distances(X, self._fit_X, 'euclidean',
--> 357                                           n_jobs=n_jobs, squared=True)
    358             else:
    359                 dist = pairwise_distances(

/usr/local/lib/python2.7/dist-packages/sklearn/metrics/pairwise.pyc in pairwise_distances(X, Y, metric, n_jobs, **kwds)
   1245         func = partial(distance.cdist, metric=metric, **kwds)
   1246
-> 1247     return _parallel_pairwise(X, Y, func, n_jobs, **kwds)
   1248
   1249

/usr/local/lib/python2.7/dist-packages/sklearn/metrics/pairwise.pyc in _parallel_pairwise(X, Y, func, n_jobs, **kwds)
   1094     ret = Parallel(n_jobs=n_jobs, verbose=0)(
   1095         fd(X, Y[s], **kwds)
-> 1096         for s in gen_even_slices(Y.shape[0], n_jobs))
   1097
   1098     return np.hstack(ret)

/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.pyc in __call__(self, iterable)
    787                 # consumption.
    788                 self._iterating = False
--> 789             self.retrieve()
    790             # Make sure that we get a last message telling us we are done
    791             elapsed_time = time.time() - self._start_time

/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.pyc in retrieve(self)
    738                     exception = exception_type(report)
    739
--> 740                     raise exception
    741
    742     def __call__(self, iterable):

JoblibValueError: JoblibValueError

我无法粘贴整个多处理异常,因为它超过了S/O发布限制。
我错过了什么?

最佳答案

n_jobs等于-1时,作业数设置为CPU核数,如ref中所述。
该错误发生在sklearn NN函数调用_parallel_pairwise()时,然后该函数尝试获取偶数片。
尝试将n_jobs设置为偶数,这当然小于cpu核的数量。
如前所述,可以在n_jobs等于1的情况下运行此代码,这不会并行化代码,因此不会公开错误。

关于python - tfidf上的scikit-learn NearestNeighbors .kneighbors()给出ValueError:UPDATEIFCOPY基为只读,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47136101/

10-12 21:38