使用sklearn.feature_extraction.text CountVectorizer时从文件中读取文档

use*_*472 2 python-2.7 scikit-learn

我可以使用文档中的示例中的代码,其中fit_transform()函数的输入是一个句子列表,即:

corpus = [
   'this is the first document',
   'this is the second second document',
   'and the third one',
   'is this the first document?'
]
Run Code Online (Sandbox Code Playgroud)

X = vectorizer.fit_transform(语料库)

并获得预期的数据.但是,当我尝试用文件列表或文件对象替换语料库时,文档建议它可以是:

"fit(raw_documents,y = None)

Learn a vocabulary dictionary of all tokens in the raw documents.
Parameters :    
raw_documents : iterable
    An iterable which yields either str, unicode or file objects.
Returns :   
self :
Run Code Online (Sandbox Code Playgroud)

"

..我认为,对管道的理解有些缺失.给定一个我想要CountVectorize的文件目录,我该怎么做?如果我尝试提供文件对象列表,如[open(file,'r')],我得到的错误信息是文件对象没有较低的功能.

Fre*_*Foo 5

将vectorizer的input 构造函数参数设置为filename或file.它的默认值是content,假设您已经将文件读入内存.