- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 scikit 学习的新手。我正在尝试进行 tfidf 向量化以适应 1*M numpy.array 即 tot_data(在下面的代码中),由英文句子组成。这里的 'words' 是一个 numpy.array (1*173),包含停用词列表。我需要明确定义参数 stop_words。如果我不明确使用参数 stop_words,代码运行良好,但下面的行显示错误。
word = numpy.array(['a','about',...])
>>> vectorizer = TfidfVectorizer(max_df=.95,stop_words=word).fit(tot_data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python2.7/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 1203, in fit
X = super(TfidfVectorizer, self).fit_transform(raw_documents)
File "/usr/local/python2.7/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 780, in fit_transform
vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary)
File "/usr/local/python2.7/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 710, in _count_vocab
analyze = self.build_analyzer()
File "/usr/local/python2.7/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 225, in build_analyzer
stop_words = self.get_stop_words()
File "/usr/local/python2.7/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 208, in get_stop_words
return _check_stop_list(self.stop_words)
File "/usr/local/python2.7/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 85, in _check_stop_list
if stop == "english":
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
最佳答案
原因:错误的原因是numpy数组将比较传播到元素:
>>> word == 'english'
array([False, False, False], dtype=bool)
和 if
语句无法将结果数组转换为 bool 值:
>>> if word == 'english': pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
解决方案:将单词转换为普通列表:words = list(words)
。
演示:
>>> import numpy as np
>>> from sklearn.feature_extraction.text import TfidfVectorizer
>>> word = np.array(['one','two','three'])
>>> tot_data = np.array(['one two three', 'who do I see', 'I see two girls'])
>>> v = TfidfVectorizer(max_df=.95,stop_words=list(word))
>>> v.fit(tot_data)
TfidfVectorizer(analyzer=u'word', binary=False, charset=None,
...
tokenizer=None, use_idf=True, vocabulary=None)
关于python - 使用参数 stop_words 时 scikit 学习 TfidfVectorizer 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20400454/
我认为函数 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
我是一名优秀的程序员,十分优秀!