本文整理匯總了Python中sklearn.feature_extraction.text.TfidfVectorizer.vocabulary方法的典型用法代碼示例。如果您正苦於以下問題:Python TfidfVectorizer.vocabulary方法的具體用法?Python TfidfVectorizer.vocabulary怎麽用?Python TfidfVectorizer.vocabulary使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.feature_extraction.text.TfidfVectorizer
的用法示例。
在下文中一共展示了TfidfVectorizer.vocabulary方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_tfidf_matrix
# 需要導入模塊: from sklearn.feature_extraction.text import TfidfVectorizer [as 別名]
# 或者: from sklearn.feature_extraction.text.TfidfVectorizer import vocabulary [as 別名]
def get_tfidf_matrix(docs):
vocab, first_occurrence_all, entropy_all = learn_vocabulary(docs)
vectorizer = TfidfVectorizer(decode_error='ignore', preprocessor=preprocess, ngram_range=(1, 3), tokenizer=tokenize)
print "--transforming tfidf matrix"
vectorizer.vocabulary = list(vocab)
X = vectorizer.fit_transform(docs)
# get list of phrases in the order of the feature vector
vocab_list = [phrase for phrase, idx in sorted(vectorizer.vocabulary_.items(), key=operator.itemgetter(1))]
assert(len(vocab_list) == X.shape[1])
return X, vocab_list, first_occurrence_all, entropy_all, vectorizer.idf_.tolist()