从scikit学习和计数矢量化器创建ngrams会引发内存错误

iNi*_*kkz 2 python memory numpy n-gram scikit-learn

我建立的n-gram使用多个文本文档scikit学习.我需要使用countVectorizer构建文档频率.

示例:

document1 = "john is a nice guy"

document2 = "person can be a guy"
Run Code Online (Sandbox Code Playgroud)

所以,文档频率将是

{'be': 1,
 'can': 1,
 'guy': 2,
 'is': 1,
 'john': 1,
 'nice': 1,
 'person': 1}
Run Code Online (Sandbox Code Playgroud)

这里的文档只是字符串,但是当我尝试使用大量数据时.它会引发MEMORY ERROR.

代码:

import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
document = [Huge amount of data around 7MB] # ['john is a guy', 'person guy']
vectorizer = CountVectorizer(ngram_range=(1, 5))
X = vectorizer.fit_transform(document).todense()
tranformer = vectorizer.transform(document).todense()
matrix_terms = np.array(vectorizer.get_feature_names())
lst_freq =  map(sum,zip(*tranformer.A))          
matrix_freq = np.array(lst_freq)
final_matrix = np.array([matrix_terms,matrix_freq])
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
  File "demo1.py", line 13, in build_ngrams_matrix
    X = vectorizer.fit_transform(document).todense()
  File "/usr/local/lib/python2.7/dist-packages/scipy/sparse/base.py", line 605, in todense
    return np.asmatrix(self.toarray(order=order, out=out))
  File "/usr/local/lib/python2.7/dist-packages/scipy/sparse/compressed.py", line 901, in toarray
    return self.tocoo(copy=False).toarray(order=order, out=out)
  File "/usr/local/lib/python2.7/dist-packages/scipy/sparse/coo.py", line 269, in toarray
    B = self._process_toarray_args(order, out)
  File "/usr/local/lib/python2.7/dist-packages/scipy/sparse/base.py", line 789, in _process_toarray_args
    return np.zeros(self.shape, dtype=self.dtype, order=order)
MemoryError
Run Code Online (Sandbox Code Playgroud)

per*_*iae 9

正如评论所提到的,当您将大型稀疏矩阵转换为密集格式时,您会遇到内存问题.尝试这样的事情:

import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
document = [Huge amount of data around 7MB] # ['john is a guy', 'person guy']
vectorizer = CountVectorizer(ngram_range=(1, 5))

# Don't need both X and transformer; they should be identical
X = vectorizer.fit_transform(document)
matrix_terms = np.array(vectorizer.get_feature_names())

# Use the axis keyword to sum over rows
matrix_freq = np.asarray(X.sum(axis=0)).ravel()
final_matrix = np.array([matrix_terms,matrix_freq])
Run Code Online (Sandbox Code Playgroud)

编辑:如果你想要一个从词到频率的字典,请在调用后尝试fit_transform:

terms = vectorizer.get_feature_names()
freqs = X.sum(axis=0).A1
result = dict(zip(terms, freqs))
Run Code Online (Sandbox Code Playgroud)