'Cristiano Ronaldo' に似ている記事はどれですか?
この動画では、NMF の特徴量とコサイン類似度を使って、類似する記事を見つける方法を学びました。
これを人気の Wikipedia 記事に対する NMF モデルに適用し、サッカー選手 Cristiano Ronaldo に関する記事に最も似ている記事を見つけてください。以前に作成した NMF の特徴量は nmf_features に、記事タイトルのリストは titles に用意されています。
この演習はコースの一部です
Pythonで学ぶ教師なし学習
演習の手順
sklearn.preprocessingからnormalizeをインポートします。normalize()関数をnmf_featuresに適用し、結果をnorm_featuresとして保存します。titlesをインデックスとして使い、norm_featuresから DataFramedfを作成します。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())