Я пытаюсь собрать TfidfVectorizer с нуля, и я собрал почти тот же векторизатор, что и склеарн, но я не могу получить те же оценки tf-idf, что и TfidfVectorizer.
Вот мой код:
def vocab(corpus):
entire_corpus = ' '.join([i for i in corpus]).split()
values = Counter(entire_corpus)
return dict(values)
def tfidf(corpus, vocab):
row = 0
vocabs = vocab(corpus)
for sentence in corpus:
col = 0
word_freq = Counter(sentence.split())
for word, freq in word_freq.items():
tf = freq/len(sentence)
n = vocabs.get(word, -1)
if n != -1:
idf = 1.0 + math.log((len(corpus)+1)/(n+1))
print((row, col), tf*idf)
col = col+1
row = row + 1
vocabs = vocab(corpus)
tfidf(corpus, vocabs)
для первой строки:
(0, 0) 0.038461538461538464
(0, 1) 0.038461538461538464
(0, 2) 0.038461538461538464
(0, 3) 0.05810867783715349
(0, 4) 0.038461538461538464
Принимая во внимание, что выход из TfIDFvectorizer Склеарна равен
(0, 8) 0.38408524091481483 10 *
(0, 6) 0,38408524091481483
(0, 3) 0,38408524091481483
(0, 2) 0,5802858236844359
(0, 1) 0,46979138557992045
1029 * Можете ли вы сказать мне, где я неправильно понимаю? Спасибо.