【问题标题】:Get full text from TfidfVectorizer从 TfidfVectorizer 获取全文
【发布时间】:2017-04-06 19:09:33
【问题描述】:

我正在绘制一组 2D 文本文档,我注意到一些异常值,我希望能够找出这些异常值是什么。我正在使用原始文本,然后使用 SKLearn 内置的 TfidfVectorizer。

  vectorizer = TfidfVectorizer(max_df=0.5, max_features=None,
                                 min_df=2, stop_words='english',
                                 use_idf=True, lowercase=True)

  corpus = make_corpus(root)
  X = vectorizer.fit_transform(corpus)

为了减少到 2D,我正在使用 TruncatedSVD。

reduced_data = TruncatedSVD(n_components=2).fit_transform(X)

如果我想找出哪个文本文档具有最高的第二个主成分(y 轴),我该怎么做?

【问题讨论】:

  • 您能否分享一些您正在使用的文本,以便我们可以复制?
  • 这是一个非常大的数据集。如果您可以复制任何数据,它应该可以工作。我只需要它遵循上面的过程。然后找到第二个主成分最高的文档。

标签: python scikit-learn tf-idf


【解决方案1】:

因此,据我了解,您想知道哪个文档最大化了特定的主成分。这是我想出的玩具示例:

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import TruncatedSVD
import numpy as np

corpus = [
    'this is my first corpus',
    'this is my second corpus which is longer than the first',
    'here is yet another one, but it is brief',
    'and watch out for number four chuggin along',
    'blah blah blah my final sentence yada yada yada'
]

vectorizer = TfidfVectorizer(stop_words='english',
                             use_idf=True, lowercase=True)

# first get TFIDF matrix
X = vectorizer.fit_transform(corpus)

# second compress to two dimensions
svd = TruncatedSVD(n_components=2).fit(X)
reduced = svd.transform(X)

# now, find the doc with the highest 2nd prin comp
corpus[np.argmax(reduced[:, 1])]

产量:

'and watch out for number four chuggin along'

【讨论】:

  • 是的,正是我想要的!谢谢!
猜你喜欢
  • 2019-06-16
  • 2012-08-09
  • 2019-10-10
  • 2019-06-03
  • 2019-05-06
  • 1970-01-01
  • 1970-01-01
  • 2014-12-04
  • 2016-08-17
相关资源
最近更新 更多