我想对包含onecclasssvm分类器训练语料库的文本文件进行矢量化。为此,我使用了scikit-learn库中的CountVectorizer。下面是我的代码:
def file_to_corpse(file_name, stop_words):
array_file = []
with open(file_name) as fd:
corp = fd.readlines()
array_file = np.array(corp)
stwf = stopwords.words('french')
for w in stop_words:
stwf.append(w)
vectorizer = CountVectorizer(decode_error = 'replace', stop_words=stwf, min_df=1)
X = vectorizer.fit_transform(array_file)
return X
当我在我的文件(大约206346行)上运行我的函数时,我得到以下错误,我似乎无法理解它:
Traceback (most recent call last):
File "svm.py", line 93, in <module>
clf_svm.fit(training_data)
File "/home/imane/anaconda/lib/python2.7/site-packages/sklearn/svm/classes.py", line 1028, in fit
super(OneClassSVM, self).fit(X, np.ones(_num_samples(X)), sample_weight=sample_weight,
File "/home/imane/anaconda/lib/python2.7/site-packages/sklearn/utils/validation.py", line 122, in _num_samples
" a valid collection." % x)
TypeError: Singleton array array(<536172x13800 sparse matrix of type '<type 'numpy.int64'>'
with 1952637 stored elements in Compressed Sparse Row format>, dtype=object) cannot be considered a valid collection.
有人能帮我解决这个问题吗?我被卡住了一段时间:)
如果你看源代码,你可以在这里找到它例如,你可以发现它检查这个条件是否为真(x是你的数组)
if len(x.shape) == 0:
如果是,它将引发这个异常
TypeError("Singleton array %r cannot be considered a valid collection." % x)
我建议您尝试找出array_file
或此函数的返回值是否具有形状长度> 0