【问题标题】:Reading documents from file when using sklearn.feature_extraction.text CountVectorizer使用 sklearn.feature_extraction.text CountVectorizer 时从文件中读取文档
【发布时间】:2013-11-04 18:02:37
【问题描述】:

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

corpus = [
   'this is the first document',
   'this is the second second document',
   'and the third one',
   'is this the first document?'
]

X = vectorizer.fit_transform(语料库)

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

" 适合(原始文档,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 :

"

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

【问题讨论】:

    标签: python-2.7 scikit-learn


    【解决方案1】:

    将矢量化器的input constructor parameter 设置为filenamefile。它的默认值为content,假设您已经将文件读入内存。

    【讨论】:

    • 谢谢,这是我在解释文档时迷失的地方。我实际上将文件直接输入到构造函数中,但没有收到任何警告,所以我没有看到它。
    猜你喜欢
    • 2021-01-25
    • 2020-08-17
    • 1970-01-01
    • 2013-08-02
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多