I'm testing TfidfVectorizer with simple example, and I can't figure out the results.
corpus = ["I'd like an apple",
"An apple a day keeps the doctor away",
"Never compare an apple to an orange",
"I prefer scikit-learn to Orange",
"The scikit-learn docs are Orange and Blue"]
vect = TfidfVectorizer(min_df=1, stop_words="english")
tfidf = vect.fit_transform(corpus)
print(vect.get_feature_names())
print(tfidf.shape)
print(tfidf)
output:
['apple', 'away', 'blue', 'compare', 'day', 'docs', 'doctor', 'keeps', 'learn', 'like', 'orange', 'prefer', 'scikit']
(5, 13)
(0, 0) 0.5564505207186616
(0, 9) 0.830880748357988
...
I'm calculating the tfidf of the first sentence and I'm getting different results:
- The first document ("
I'd like an apple") contains just 2 words (after removeing stop words (according to the print ofvect.get_feature_names()(we stay with: "like", "apple") - TF("apple", Doucment_1) = 1/2 = 0.5
- TF("like", Doucment_1) = 1/2 = 0.5
- The word
appleappears 3 times in the corpus. - The word
likeappears 1 time in the corpus. - IDF ("apple") = ln(5/3) = 0.51082
- IDF ("like") = ln(5/1) = 1.60943
so:
tfidf("apple")in document1 = 0.5 * 0.51082 = 0.255 != 0.5564tfidf("like")in document1 = 0.5 * 1.60943 = 0.804 != 0.8308
What am I missing ?