小编Pri*_*sie的帖子

使用来自sklearn.feature_extraction.text.TfidfVectorizer的TfidfVectorizer计算IDF

我认为函数TfidfVectorizer没有正确计算IDF因子.例如,使用sklearn.feature_extraction.text.TfidfVectorizertf-idf要素权重复制代码:

from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ["This is very strange",
          "This is very nice"]
vectorizer = TfidfVectorizer(
                        use_idf=True, # utiliza o idf como peso, fazendo tf*idf
                        norm=None, # normaliza os vetores
                        smooth_idf=False, #soma 1 ao N e ao ni => idf = ln(N+1 / ni+1)
                        sublinear_tf=False, #tf = 1+ln(tf)
                        binary=False,
                        min_df=1, max_df=1.0, max_features=None,
                        strip_accents='unicode', # retira os acentos
                        ngram_range=(1,1), preprocessor=None,              stop_words=None, tokenizer=None, vocabulary=None
             )
X = vectorizer.fit_transform(corpus)
idf = vectorizer.idf_
print dict(zip(vectorizer.get_feature_names(), idf))
Run Code Online (Sandbox Code Playgroud)

输出是:

{u'is': …
Run Code Online (Sandbox Code Playgroud)

python scikit-learn

8
推荐指数
1
解决办法
9087
查看次数

标签 统计

python ×1

scikit-learn ×1