- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将大型语料库拆分为 5K 个文件,我正在尝试使用 TF-IDF 转换生成基于 IDF 的词汇表。
这是代码:基本上我有一个迭代器,它循环遍历 .tsv 文件的目录,读取每个文件并产生。
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import pandas as pd
import numpy as np
import os
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
def make_corpus():
inputFeatureFiles = [x for x in os.listdir('C:\Folder') if x.endswith("*.tsv")]
for file in inputFeatureFiles:
filePath= 'C:\\' + os.path.splitext(file)[0] + ".tsv"
with open(filePath, 'rb') as infile:
content = infile.read()
yield content
corpus = make_corpus()
vectorizer = TfidfVectorizer(stop_words='english',use_idf=True, max_df=0.7, smooth_idf=True)
vectorizer.fit_transform(corpus)
c:\python27\lib\site-packages\sklearn\feature_extraction\text.pyc in _count_vocab(self, raw_documents, fixed_vocab)
809 vocabulary = dict(vocabulary)
810 if not vocabulary:
--> 811 raise ValueError("empty vocabulary; perhaps the documents only"
812 " contain stop words")
813
ValueError: empty vocabulary; perhaps the documents only contain stop words
corpusGenerator= [open(os.path.join('C:\CorpusFiles\',f)) for f in os.listdir('C:\CorpusFiles')]
vectorizer = TfidfVectorizer(stop_words='english',use_idf=True,smooth_idf=True, sublinear_tf=True, input="file", min_df=1)
feat = vectorizer.fit_transform(corpusGenerator)
[Errno 24] Too many open files: 'C:\CorpusFiles\file1.tsv'
最佳答案
哎,最近我也在研究同样的问题。根据我的经验,也许您可以尝试以下演示代码:
import glob
all_files_path = glob.glob(path_to_the_dir_of_your_data_files)
def fit_iterator():
for file_path in all_files_path:
with open(file_path, "r", encoding="utf-8") as file:
for line in file:
yield line # please make sure that line is a instance of str
# representing a single sample.
corpus = fit_iterator()
tfidf = TfidfVectorizer()
tfidf.fit(corpus)
祝你好运!
关于python - 带有生成器的大型语料库上的 TfidfVectorizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50750052/
我认为函数 TfidfVectorizer 没有正确计算 IDF 因子。例如,从 tf-idf feature weights using sklearn.feature_extraction.tex
我将大型语料库拆分为 5K 个文件,我正在尝试使用 TF-IDF 转换生成基于 IDF 的词汇表。 这是代码:基本上我有一个迭代器,它循环遍历 .tsv 文件的目录,读取每个文件并产生。 import
假设我用于单个文档 text="bla agao haa" singleTFIDF = TfidfVectorizer(analyzer='char_wb', ngram_range= (4,6),p
我尝试在语料库上使用 TfidfVectorizer,但每次都会出现此错误 File "sparsefuncs.pyx", line 117, in sklearn.utils.sparsefuncs
虽然有六个不同的词。结果只打印了5个字。如何根据所有单词(6列向量)获得结果? from sklearn.feature_extraction.text import TfidfVectorizer
我正在尝试使用 sklearn 的 TfidfVectorizer 输出由两个一元组组成的输入列表的 tf-idf 分数和二元组。 这是我正在做的事情的本质: comprehensive_ngrams
我正在寻找一种方法来加载我之前使用 scikit-learn 的 TfidfVectorizer 生成的向量。总的来说,我希望更好地了解 TfidfVectorizer 的数据持久性。 例如,到目前为
就我而言,不存在这样的问题。我正在 Kaggle 中从事 NLP 和情感分析项目,首先我正在准备我的数据。数据框是一个文本列,后跟 0 到 9 之间的数字,用于对行(文档)所属的簇进行分类。我在 sk
我使用 TfIdfVectorizer 和 MultinomialNB 训练了我的模型,并将其保存到 pickle 文件中。 现在我正尝试使用另一个文件中的分类器来预测看不见的数据,我不能这样做,因为
我有一个大型语料库,存储为 25 个列表的字典,我想使用 SKLearn 的 TfidfVectorizer 进行分析。每个列表包含许多字符串。现在,我既关心整个语料库中的总体词频 (tf),也关心
我对 skelearn 的 TfidfVectorizer 在我不知道的情况下到底做了什么感到有点困惑。 我有这样的句子: sentence_1 = 'Sum: 1 Mean: 1 Min:1' 但是
给出以下代码: import pandas as pd from sklearn.feature_extraction.text import TfidfVectorizer import urlli
我正在使用 sklearn Pipeline 和 FeatureUnion 从文本文件创建特征,我想打印出特征名称。 首先,我将所有转换收集到一个列表中。 In [225]:components Ou
我想确保我了解属性 use_idf 和 sublinear_tf 在 TfidfVectorizer 对象中的作用。这几天我一直在研究这个。我正在尝试对不同长度的文档进行分类,目前使用 tf-idf
我正在测试 TfidfVectorizer举个简单的例子,我想不出结果。 corpus = ["I'd like an apple", "An apple a day keeps
在 scikit-learn TfidfVectorizer允许我们拟合训练数据,然后使用相同的向量化器来转换我们的测试数据。 训练数据转换的输出是一个矩阵,表示给定文档的每个单词的 tf-idf 分
我正在尝试删除 TfidfVectorizer 中法语和英语的停用词。到目前为止,我只成功地从英语中删除了停用词。当我尝试为 stop_words 输入法语时,收到一条错误消息,指出它不是内置的。 事
我正在尝试在一组上训练 NLP 模型,保存词汇和模型,然后将其应用于单独的验证集。代码正在运行,但我如何确定它按我的预期工作? 换句话说,我从训练集中保存了词汇和 nmodel,然后使用保存的词汇创建
我有一个相当简单的 NLTK 和 sklearn 分类器(我对此完全是菜鸟)。 我进行通常的导入 import pandas as pd import matplotlib.pyplot as plt
我正在绘制一组二维文本文档,我注意到一些异常值,我希望能够找出这些异常值是什么。我使用原始文本,然后使用 SKLearn 内置的 TfidfVectorizer。 vectorizer = Tfi
我是一名优秀的程序员,十分优秀!