【问题标题】:Tfidfvectorizer - get features with weights from transformTfidfvectorizer - 从变换中获取具有权重的特征
【发布时间】:2019-06-16 07:02:32
【问题描述】:

假设我用于单个文档

text="bla agao haa"
singleTFIDF = TfidfVectorizer(analyzer='char_wb', ngram_range= 
(4,6),preprocessor=my_tokenizer, max_features=100).fit([text])

single=singleTFIDF.transform([text])
query = singleTFIDF.transform(["new coming document"])

如果我理解正确,transform 只使用从 fit 中学习到的权重。因此,对于新文档,查询包含文档中每个特征的权重。看起来像 [[0,,0,0.13,0.4,0]]

当我使用 n-gram 时,我也想为这个新文档获取这些功能。所以我知道新文档中每个特性的权重。

编辑:

在我的情况下,我得到一个并查询以下数组:

single
[[0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125 0.10721125 0.10721125 0.10721125
  0.10721125 0.10721125 0.10721125]]
query
[[0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.57735027 0.57735027 0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.         0.         0.
  0.         0.         0.         0.57735027 0.         0.
  0.         0.         0.        ]]

但这很奇怪,因为从学习的语料库(单个)中,所有特征的权重都是 0.10721125。那么新文档的一个特征怎么会有0.57735027的权重呢?

【问题讨论】:

  • 您要分析什么 - 字符或单词 n-gram?
  • 我用的是char_wb,但你的意思是什么?
  • ‘char_wb’ 仅从单词边界内的文本创建字符 n-gram - 这真的是您想要的吗?
  • 问题不在于这个!
  • 没错,但读它会让人怀疑你是否理解你的代码在做什么。给你举一个单词 n-gram 的例子会更容易,而不是字符 n-gram。

标签: python scikit-learn tfidfvectorizer


【解决方案1】:

新文档具有新的权重,因为 tfidfvectorizer 将权重归一化。 因此将参数norm 设置为Nonenorm 的默认值为l2

要了解更多关于规范的影响,我建议您查看我对this 问题的回答。

【讨论】:

  • 非常感谢!我想验证新文档的特征和权重,以分析相似性是如何创建的。到目前为止,我得到了前 5 个类似的文档,但内部具有大部分功能的文档具有最差的相似性!这很奇怪。我会看你的帖子和规范!
  • Transfom 构建了新的 tfidf 权重,对吧?我认为它会使用从 fit 中学到的权重(可能在大型数据集上)。我宁愿给两个文档打分,这些文档的权重是以前从 fit 训练出来的。你明白我了吗?
  • 我想知道为什么不将新文档的特征权重与从语料库中提取的权重相加以识别最相似的权重,而不是使用余弦 sim。
  • 嘿,我真的很困惑数组 single 的位置的含义,例如从上面。如何从单个索引中获取或从 get_features 或词汇表中查询相应的特征?这真是令人困惑。我在任何地方都找不到任何东西..
【解决方案2】:

Scikit-Learn 如何计算 tfidf 的详细信息可在here 获得,这里是使用单词 n-gram 实现的示例。

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# Train the vectorizer
text="this is a simple example"
singleTFIDF = TfidfVectorizer(ngram_range=(1,2)).fit([text])
singleTFIDF.vocabulary_ # show the word-matrix position pairs

# Analyse the training string - text
single=singleTFIDF.transform([text])
single.toarray()  # displays the resulting matrix - all values are equal because all terms are present

# Analyse two new strings with the trained vectorizer
doc_1 = ['is this example working', 'hopefully it is a good example', 'no matching words here']

query = singleTFIDF.transform(doc_1)
query.toarray() # displays the resulting matrix - only matched terms have non-zero values

# Compute the cosine similarity between text and doc_1 - the second string has only two matching terms, therefore it has a lower similarity value
cos_similarity = cosine_similarity(single.A, query.A)

输出:

singleTFIDF.vocabulary_ 
Out[297]: 
{'this': 5,
 'is': 1,
 'simple': 3,
 'example': 0,
 'this is': 6,
 'is simple': 2,
 'simple example': 4}

single.toarray()
Out[299]: 
array([[0.37796447, 0.37796447, 0.37796447, 0.37796447, 0.37796447,
        0.37796447, 0.37796447]])

query.toarray()
Out[311]: 
array([[0.57735027, 0.57735027, 0.        , 0.        , 0.        ,
        0.57735027, 0.        ],
       [0.70710678, 0.70710678, 0.        , 0.        , 0.        ,
        0.        , 0.        ],
       [0.        , 0.        , 0.        , 0.        , 0.        ,
        0.        , 0.        ]])

np.sum(np.square(query.toarray()), axis=1) # note how all rows with non-zero scores have been normalised to 1.
Out[3]: array([1., 1., 0.])

cos_similarity
Out[313]: array([[0.65465367, 0.53452248, 0.        ]])

【讨论】:

  • 谢谢!我对 ngram 所做的也是如此。像上面的矩阵一样,我可以将查询图中的 ngram 特征映射到相应的权重吗?那么查询是否保存了特征的相应位置?
  • 你的问题不是很清楚。我的示例还使用了 ngram(单和双),但是 ngram 是单词。通过使用 analyzer='char_wb' 您正在确定字符组合的频率,而不是单词的频率。回到我的示例 - singleTFIDF.vocabulary_ 向您显示词汇表和每个术语在 toarray() 输出的矩阵中的位置. "example" 的位置为 0,相应地,single 的 0 位置和前两个查询 stringsquery 是非零的。如果您的新文档不包含矢量化器词汇表中的任何单词,则它们只会得到 0。
  • 好的。通过词汇的位置,我可以找出矩阵中的特征?
  • 准确地说,.vocabulary_ 将术语及其列索引显示为键值对。
猜你喜欢
  • 2011-01-10
  • 2019-08-10
  • 2016-04-27
  • 2021-03-08
  • 2014-02-11
  • 2020-11-19
  • 2018-06-08
  • 2018-05-13
  • 1970-01-01
相关资源
最近更新 更多