如何计算tf-idf的dict列表



我有一个文本列表,其中每个文本都以iD为键作为密钥和文本数据作为其值存储。如何计算此数据的TF-IDF。例如:

{1: 'This is cat', 2: 'Is this the first document?', 3: 'And the third one.'}

首先将您的字典转换为字符串列表:

    X_all = list(d.values())

构建tfidfvectoriser的功能为:

    from sklearn.feature_extraction.text import TfidfVectorizer
    tfv = TfidfVectorizer(min_df=3,  max_features=None,
    strip_accents='unicode', analyzer='word',token_pattern=r'w{1,}',
    ngram_range=(1,2), use_idf=1,smooth_idf=1,sublinear_tf=1,
    stop_words = 'english')

然后您可以构建模型为:

    X_all = tfv.transform(X_all)

其中x_all是文本文档列表。

最新更新