開始使用免費開始

哪些文章與「Cristiano Ronaldo」相似?

在影片中,你學到如何使用 NMF 特徵與餘弦相似度來找出相似的文章。 把這個方法套用到你針對熱門維基百科文章建立的 NMF 模型上,找出最接近足球員 Cristiano Ronaldo 這篇文章的其他文章。你先前取得的 NMF 特徵可由 nmf_features 取得,而 titles 則是文章標題的列表。

本練習屬於課程

Unsupervised Learning in Python

檢視課程

練習說明

  • sklearn.preprocessing 匯入 normalize
  • nmf_features 套用 normalize() 函式。將結果存為 norm_features
  • norm_features 建立 DataFrame df,並使用 titles 作為索引。
  • 使用 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())
編輯並執行程式碼