Ima*_*e.r 5 python machine-learning vectorization python-2.7 scikit-learn
我想要为包含我的OneClassSVM分类器的训练语料库的txt文件进行矢量化.为此,我正在使用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
Run Code Online (Sandbox Code Playgroud)
当我在我的文件上运行我的函数(大约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.
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决这个问题吗?我已经被困了一段时间:).
如果你查看源代码,你可以在这里找到它,例如,你可以发现它检查这个条件是否为真(x是你的数组)
if len(x.shape) == 0:
Run Code Online (Sandbox Code Playgroud)
如果是这样,它将引发此异常
TypeError("Singleton array %r cannot be considered a valid collection." % x)
Run Code Online (Sandbox Code Playgroud)
我建议你试图找出array_file这个函数的返回值是否或者你的返回值的形状长度> 0
| 归档时间: |
|
| 查看次数: |
9688 次 |
| 最近记录: |