始める無料で始める

商品のレビューの頻度分析

より大きな TechZone の商品レビューのデータセットを使えるようになりました。これまでと同様に、レビューは前処理を行い、BoW 表現 X に変換済みです。ここからは、単語の出現頻度を分析し、データセットで最も一般的な用語を特定しましょう。

分析を助けるために、get_top_ten() というヘルパー関数が用意されています。これは単語のリストとそれに対応するカウントを受け取り、最も頻度の高い10個の単語とそのカウントを返します。

この演習はコースの一部です

Pythonで学ぶ自然言語処理(NLP)

コースを見る

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

def preprocess(text):
    text = text.lower()
    tokens = word_tokenize(text)
    tokens = [word for word in tokens if word not in string.punctuation]
    return " ".join(tokens)
  
cleaned_reviews = [preprocess(review) for review in product_reviews]
X = vectorizer.fit_transform(cleaned_reviews)

# Get word counts
word_counts = np.____(X.____, axis=0)
# Get words
words = vectorizer.____

top_words_with_stopwords, top_counts_with_stopwords = get_top_ten(words, word_counts)
print(top_words_with_stopwords, top_counts_with_stopwords)
コードを編集して実行