2017-11-02 12 views
0

scikit-learn documentationsmooth_idfは重複していますか?

しかし

If smooth_idf=True (the default), the constant “1” is added to the numerator and denominator of the idf as if an extra document was seen containing every term in the collection exactly once, which prevents zero divisions: idf(d, t) = log [ (1 + n)/(1 + df(d, t)) ] + 1.

、なぜdf(d, t) = 0うと言いますか?単語がどのテキストにも出現しない場合、辞書は最初にその単語を持たないだろうか?

+1

"トレーニング" のために本当です。古い辞書の目に見えない新しいテキストを入力すると、divでゼロになりますか? –

+0

@ThomasJungblutしかし、TfidfVectorizerは「トレーニング」でのみ使用されますか? – yhylord

答えて

2

この機能はTfidfVectorizerで有用です。 documentationによれば、このクラスは予め定義されたvocabularyで提供されることができる。語彙からの単語が列車データには見られないが、テストでは単語が含まれていれば、smooth_idfを使用すると、単語が正常に処理されます。

train_texts = ['apple mango', 'mango banana'] 
test_texts = ['apple banana', 'mango orange'] 
vocab = ['apple', 'mango', 'banana', 'orange'] 
from sklearn.feature_extraction.text import TfidfVectorizer 
vectorizer1 = TfidfVectorizer(smooth_idf=True, vocabulary=vocab).fit(train_texts) 
vectorizer2 = TfidfVectorizer(smooth_idf=False, vocabulary=vocab).fit(train_texts) 
print(vectorizer1.transform(test_texts).todense()) # works okay 
print(vectorizer2.transform(test_texts).todense()) # raises a ValueError 

出力:

[[ 0.70710678 0.   0.70710678 0.  ] 
[ 0.   0.43016528 0.   0.90275015]] 
... 
ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). 
関連する問題