gpt4 book ai didi

python - 大型文档语料库上的 Sklearn TFIDF

转载 作者:行者123 更新时间:2023-12-01 09:13:44 25 4
gpt4 key购买 nike

在实习项目中,我必须对大量文件(~18000)执行 tfidf 分析。我正在尝试使用 sklearn 中的 TFIDF 矢量器,但我面临以下问题:如何避免一次将所有文件加载到内存中?根据我在其他帖子上读到的内容,使用可迭代似乎是可行的,但是如果我使用 [open(file) for file in os.listdir(path)] 作为 raw_documents 输入对于 fit_transform() 函数,我收到“打开文件太多”错误。预先感谢您的建议!干杯!保罗

最佳答案

您是否在 TfidfVectorizer 中尝试过 input='filename' 参数?像这样的事情:

raw_docs_filepaths = [#List containing the filepaths of all the files]

tfidf_vectorizer = TfidfVectorizer(`input='filename'`)
tfidf_data = tfidf_vectorizer.fit_transform(raw_docs_filepaths)

这应该可行,因为在这种情况下,矢量化器在处理该文件时将一次打开一个文件。这可以通过交叉检查 source code here 来确认。

def decode(self, doc):
...
...
if self.input == 'filename':
with open(doc, 'rb') as fh:
doc = fh.read()
...
...

关于python - 大型文档语料库上的 Sklearn TFIDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51422688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com