始める無料で始める

'Cristiano Ronaldo' に似ている記事はどれですか?

この動画では、NMF の特徴量とコサイン類似度を使って、類似する記事を見つける方法を学びました。 これを人気の Wikipedia 記事に対する NMF モデルに適用し、サッカー選手 Cristiano Ronaldo に関する記事に最も似ている記事を見つけてください。以前に作成した NMF の特徴量は nmf_features に、記事タイトルのリストは titles に用意されています。

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

Pythonで学ぶ教師なし学習

コースを見る

演習の手順

  • sklearn.preprocessing から normalize をインポートします。
  • normalize() 関数を nmf_features に適用し、結果を norm_features として保存します。
  • titles をインデックスとして使い、norm_features から DataFrame df を作成します。
  • df.loc[] アクセサを使って、'Cristiano Ronaldo' の行を選択し、結果を article に代入します。
  • df.dot() メソッドを article に適用して、各行と article とのコサイン類似度を計算します。
  • 最も類似した記事を表示するために、similarities.nlargest() の結果を出力します。これはすでに用意されているので、"Submit Answer" を押して結果を確認しましょう!

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

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

# Perform the necessary imports
import pandas as pd
from ____ import ____

# Normalize the NMF features: norm_features
norm_features = ____

# Create a DataFrame: df
df = ____

# Select the row corresponding to 'Cristiano Ronaldo': article
article = df.loc[____]

# Compute the dot products: similarities
similarities = ____

# Display those with the largest cosine similarity
print(similarities.nlargest())
コードを編集して実行