【问题标题】:Texthero TD-IDF CalculationTexthero TD-IDF 计算
【发布时间】:2020-11-05 12:06:36
【问题描述】:

通过Texthero计算TF-IDF有什么区别:

import texthero as hero
s = pd.Series(["Sentence one", "Sentence two"])
hero.tfidf(s, return_feature_names=True)
0    [0.5797386715376657, 0.8148024746671689, 0.0]
1    [0.5797386715376657, 0.0, 0.8148024746671689]
['Sentence', 'one', 'two'])

还有来自 sklearn 的 TD-IDF?鉴于这些例句,我希望 sklearn 的结果。

from sklearn.feature_extraction.text import TfidfVectorizer
...
Sentence    one two
0   0.0 0.346574    0.000000
1   0.0 0.000000    0.346574

【问题讨论】:

    标签: python tf-idf tfidfvectorizer


    【解决方案1】:

    简答

    tfidf 不预处理输入文本,仅应用 TF-IDF 算法,而默认情况下 TfidfVectorizer 预处理输入。

    函数签名

    区别在于您应该处理这两个框架的方式。

    查看函数签名:

    scikit-learn TfidfVectorizer:

    sklearn.feature_extraction.text.TfidfVectorizer(
        *, 
        input='content', 
        encoding='utf-8', 
        decode_error='strict', 
        strip_accents=None, 
        lowercase=True, 
        preprocessor=None, 
        tokenizer=None, 
        analyzer='word', 
        stop_words=None, 
        token_pattern='(?u)\b\w\w+\b', 
        ngram_range=(1, 1), 
        max_df=1.0, 
        min_df=1, 
        max_features=None, 
        vocabulary=None, 
        binary=False, 
        dtype=<class 'numpy.float64'>, 
        norm='l2', 
        use_idf=True, 
        smooth_idf=True, 
        sublinear_tf=False
    )
    

    文字英雄tfidf:

    tfidf(
        s: pandas.core.series.Series, 
        max_features=None, 
        min_df=1, 
        return_feature_names=False
    )
    

    对于 scikit-learn,不同的文本预处理步骤包含在 TfidfVectorizer 中。以Texthero的tfidf为例,没有文字预处理。

    你的例子

    在您的示例中,tf-idf 值在两种情况下是不同的,例如 TfidfVectorizer 默认将所有字符转换为小写。

    哪个更好?

    根据您的任务,两种解决方案中的一种可能更方便。

    如果您正在使用 Pandas Dataframe/Series 处理自然语言预处理任务,并且希望对代码进行精细控制,那么使用 tfidf 可能会很方便。

    另一方面,如果您正在处理更通用的 ML 任务,您还需要处理一些文本并且只想快速表示它,那么您可能会选择使用默认设置的TfidfVectorizer

    【讨论】:

    • 感谢您的回复。我不明白 hero.tfidf 如何按照 TF-IDF 算法获得显示的结果。
    • 您能否分享您使用的所有代码(也适用于TfidfVectorizer 部分)?我将尝试通过在答案中添加一个新部分来比较两者。
    • 原来差异确实是使用的参数设置造成的。
    猜你喜欢
    • 2019-09-09
    • 2012-04-23
    • 2010-12-30
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 2017-11-14
    • 2021-02-08
    • 2015-04-17
    相关资源
    最近更新 更多