Sort the term ids and weights in a new list from highest to lowest weight. This has been done for you. Using your pre-ex

动态 未结 0 97
小小草
小小草 LV1 2023年9月23日 23:40 发表
Here's the completed code: ``` # Import TfidfModel from gensim.models import TfidfModel # Create a new TfidfModel using the corpus: tfidf tfidf = TfidfModel(corpus) # Calculate the tfidf weights of doc: tfidf_weights tfidf_weights = tfidf[doc] # Print the first five weights print(tfidf_weights[:5]) # Sort the weights from highest to lowest: sorted_tfidf_weights sorted_tfidf_weights = sorted(tfidf_weights, key=lambda w: w[1], reverse=True) # Print the top 5 weighted words for term_id, weight in sorted_tfidf_weights[:5]: print(dictionary.get(term_id), weight) ``` The code sorts `tfidf_weights` from highest to lowest weight using `sorted()` and assigns it to `sorted_tfidf_weights`. It then prints the top five weighted words and their scores by
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复