矢量化:不是有效的集合

时间:2016-06-29 13:44:04

标签: python python-2.7 machine-learning scikit-learn vectorization

我想要为包含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

当我在我的文件上运行我的函数(大约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.

有人可以帮我解决这个问题吗?我已经被困了一段时间:)。

1 个答案:

答案 0 :(得分:7)

如果查看源代码,可以找到它here,例如,你可以发现它检查这个条件是否为真(x是你的数组)

$auth = [
    'Username' => 'XXXXX',
    'Password' => 'XXXXX',
];

$ns = 'http://www.w3.org/2003/05/soap-envelope/';

$header = new SOAPHeader($ns, 'AuthenticationCredentials', $auth);        

$client->__setSoapHeaders($header);

如果是这样,它将引发此异常

if len(x.shape) == 0:

我建议您尝试找出TypeError("Singleton array %r cannot be considered a valid collection." % x) 或此函数的返回值是否具有形状长度&gt; 0