sklearn kneighbours内存错误python

时间:2016-06-13 04:55:19

标签: python scikit-learn out-of-memory nearest-neighbor knn

我正在使用Windows 7 8gb RAM。

这是我用来矢量化52MB训练数据集中的自由文本列的矢量化器

vec = CountVectorizer(analyzer='word',stop_words='english',decode_error='ignore',binary=True)

我想用这个数据集计算5个最近邻居的18MB测试集。

nbrs = NearestNeighbors(n_neighbors=5).fit(vec.transform(data['clean_sum']))
vectors = vec.transform(data_test['clean_sum'])
distances,indices = nbrs.kneighbors(vectors)

这是堆栈跟踪 -

Traceback (most recent call last):
  File "cr_nearness.py", line 224, in <module>
    distances,indices = nbrs.kneighbors(vectors)
  File "C:\Anaconda2\lib\site-packages\sklearn\neighbors\base.py", line 371,
kneighbors
    n_jobs=n_jobs, squared=True)
  File "C:\Anaconda2\lib\site-packages\sklearn\metrics\pairwise.py", line 12
in pairwise_distances
    return _parallel_pairwise(X, Y, func, n_jobs, **kwds)
  File "C:\Anaconda2\lib\site-packages\sklearn\metrics\pairwise.py", line 10
in _parallel_pairwise
    return func(X, Y, **kwds)
  File "C:\Anaconda2\lib\site-packages\sklearn\metrics\pairwise.py", line 23
n euclidean_distances
    distances = safe_sparse_dot(X, Y.T, dense_output=True)
  File "C:\Anaconda2\lib\site-packages\sklearn\utils\extmath.py", line 181,
afe_sparse_dot
    ret = ret.toarray()
  File "C:\Anaconda2\lib\site-packages\scipy\sparse\compressed.py", line 940
 toarray
    return self.tocoo(copy=False).toarray(order=order, out=out)
  File "C:\Anaconda2\lib\site-packages\scipy\sparse\coo.py", line 250, in to
y
    B = self._process_toarray_args(order, out)
  File "C:\Anaconda2\lib\site-packages\scipy\sparse\base.py", line 817, in _
ess_toarray_args
    return np.zeros(self.shape, dtype=self.dtype, order=order)
MemoryError

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

将KNN与KD TREE一起使用

  

model =   KNeighborsClassifier(N_NEIGHBORS = 5,算法= 'kd_tree')。适合(X_train,   Y_train)

模型默认为algorithm ='brute'。粗暴的假记忆太多了。 我认为你的模型看起来应该是这样的

  

nbrs =   NearestNeighbors(N_NEIGHBORS = 5,算法= 'kd_tree')。适合(vec.transform(数据[ 'clean_sum']))