- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在处理约 10 万篇研究论文的语料库。我正在考虑三个领域:
我使用 TfIdfVectorizer 获取明文字段的 TfIdf 表示,并将由此产生的词汇反馈回标题和摘要的 Vectorizers,以确保所有三种表示都在同一个词汇上工作。我的想法是,由于明文字段比其他两个字段大得多,因此它的词汇很可能涵盖其他字段中的所有单词。但是,如果不是这种情况,TfIdfVectorizer 将如何处理新词/标记?
这是我的代码示例:
vectorizer = TfidfVectorizer(min_df=2)
plaintexts_tfidf = vectorizer.fit_transform(plaintexts)
vocab = vectorizer.vocabulary_
# later in an another script after loading the vocab from disk
vectorizer = TfidfVectorizer(min_df=2, vocabulary=vocab)
titles_tfidf = vectorizer.fit_transform(titles)
词汇表有约 90 万个单词。
在矢量化过程中我没有遇到任何问题,但后来当我想使用 sklearn.metrics.pairwise.cosine_similarity 比较矢量化标题之间的相似性时,我遇到了这个错误:
>> titles_sim = cosine_similarity(titles_tfidf)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-237-5aa86fe892da> in <module>()
----> 1 titles_sim = cosine_similarity(titles)
/usr/local/lib/python3.5/dist-packages/sklearn/metrics/pairwise.py in cosine_similarity(X, Y, dense_output)
916 Y_normalized = normalize(Y, copy=True)
917
--> 918 K = safe_sparse_dot(X_normalized, Y_normalized.T, dense_output=dense_output)
919
920 return K
/usr/local/lib/python3.5/dist-packages/sklearn/utils/extmath.py in safe_sparse_dot(a, b, dense_output)
184 ret = a * b
185 if dense_output and hasattr(ret, "toarray"):
--> 186 ret = ret.toarray()
187 return ret
188 else:
/usr/local/lib/python3.5/dist-packages/scipy/sparse/compressed.py in toarray(self, order, out)
918 def toarray(self, order=None, out=None):
919 """See the docstring for `spmatrix.toarray`."""
--> 920 return self.tocoo(copy=False).toarray(order=order, out=out)
921
922 ##############################################################
/usr/local/lib/python3.5/dist-packages/scipy/sparse/coo.py in toarray(self, order, out)
256 M,N = self.shape
257 coo_todense(M, N, self.nnz, self.row, self.col, self.data,
--> 258 B.ravel('A'), fortran)
259 return B
260
ValueError: could not convert integer scalar
我不太确定这是否相关,但我真的看不出这里出了什么问题。也因为我在计算明文向量的相似性时没有遇到错误。
我是不是遗漏了什么?有没有更好的方法来使用 Vectorizer?
编辑:
稀疏 csr_matrices 的形状是相等的。
>> titles_tfidf.shape
(96582, 852885)
>> plaintexts_tfidf.shape
(96582, 852885)
最佳答案
恐怕矩阵可能太大了。它将是 96582*96582=9328082724 个单元格。尝试将 titles_tfidf 切片并检查一下。
来源:http://scipy-user.10969.n7.nabble.com/SciPy-User-strange-error-when-creating-csr-matrix-td20129.html
美国东部时间:如果您使用的是较旧的 SciPy/Numpy 版本,您可能需要更新:https://github.com/scipy/scipy/pull/4678
EDT2:此外,如果您使用的是 32 位 python,切换到 64 位可能会有所帮助(我想)
EDT3:回答你原来的问题。当您使用 plaintexts
中的词汇时,titles
中会有新词,它们将被忽略 - 但不会影响 tfidf 值。希望这个片段可以让它更容易理解:
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
plaintexts =["They are", "plain texts texts amoersand here"]
titles = ["And here", "titles ", "wolf dog eagle", "But here plain"]
vectorizer = TfidfVectorizer()
plaintexts_tfidf = vectorizer.fit_transform(plaintexts)
vocab = vectorizer.vocabulary_
vectorizer = TfidfVectorizer(vocabulary=vocab)
titles_tfidf = vectorizer.fit_transform(titles)
print('values using vocabulary')
print(titles_tfidf)
print(vectorizer.get_feature_names())
print('Brand new vectorizer')
vectorizer = TfidfVectorizer()
titles_tfidf = vectorizer.fit_transform(titles)
print(titles_tfidf)
print(vectorizer.get_feature_names())
结果是:
values using vocabulary
(0, 2) 1.0
(3, 3) 0.78528827571
(3, 2) 0.61913029649
['amoersand', 'are', 'here', 'plain', 'texts', 'they']
Brand new vectorizer
(0, 0) 0.78528827571
(0, 4) 0.61913029649
(1, 6) 1.0
(2, 7) 0.57735026919
(2, 2) 0.57735026919
(2, 3) 0.57735026919
(3, 4) 0.486934264074
(3, 1) 0.617614370976
(3, 5) 0.617614370976
['and', 'but', 'dog', 'eagle', 'here', 'plain', 'titles', 'wolf']
请注意,它不一样,因为我会从标题中删除未出现在明文中的单词。
关于python - TfIdfVectorizer : How does the vectorizer with fixed vocab deal with new words?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42068474/
当处理数百万文档并将它们保存为空间文档以供以后使用(更多处理、可视化、提取特征)时,一种明显的扩展解决方案是并行/分布式处理。这意味着每个并行进程都将拥有自己的 Vocab,这些 Vocab 可能会随
这不是一个新问题,我找到的引用文献没有任何适合我的解决方案 first和 second .我是 PyTorch 的新手,面对 AttributeError: 'Field' object has no
下面的代码是获取每个句子的文档的示例代码。 Get docs 为了获取每个单词的属性,我们使用文档,示例代码如下。 Get Tokens 通过深入挖掘 spacy 代码,我发现对于每个名词,我们确实有
我正在处理约 10 万篇研究论文的语料库。我正在考虑三个领域: 明文 标题 摘要 我使用 TfIdfVectorizer 获取明文字段的 TfIdf 表示,并将由此产生的词汇反馈回标题和摘要的 Vec
什么是@vocab JSON-LD 中的属性?正如我所见,您可以“导入”远程词汇表,但这与您可以使用 @context 做的事情不一样吗? ?如果我没有错,那么您可以为 @context“导入”远程源
我正在尝试使用来自 TensorFlow Hub 的 Bert 并构建一个分词器,这就是我正在做的: >>> import tensorflow_hub as hub >>> from bert.to
我看到别人的BERT模型,其中vocab.txt的大小是22110,但是bert_config.json中vocab_size参数的值为21128。 我明白这两个数字一定是完全一样的。是吗? 最佳答案
假设我有这个有效的微数据增强 HTML 片段: Example (我已经用谷歌的结构化数据测试工具对此进行了测试。) 现在我想添加一个在 schema.org 中不可用但在不同词汇中的属性(在
此问题适合那些熟悉 GPT 或 GPT2 的人OpenAI 模型。特别是编码任务(字节对编码)。这是我的问题: 我想知道如何创建自己的 vocab.bpe 文件。 我有一个西类牙语语料库文本,我想用它
当我尝试查看批处理时,通过打印 BucketIterator 对象的下一次迭代,抛出了 AttributeError。 tv_datafields=[("Tweet",TEXT), ("Anger",
我正在执行的所有代码是: from __future__ import unicode_literals import spacy from spacy.vocab import Vocab nlp
我是一名优秀的程序员,十分优秀!