开始使用免费开始使用

与 "Cristiano Ronaldo" 相似的文章有哪些?

在视频中,您学习了如何使用 NMF 特征和余弦相似度来查找相似的文章。 请将其应用到您针对热门 Wikipedia 文章训练的 NMF 模型上,找出与足球运动员 Cristiano Ronaldo 相关文章最相似的文章。您先前获得的 NMF 特征存放在 nmf_features 中,titles 是文章标题列表。

本练习是课程的一部分

Python 中的无监督学习

查看课程

练习说明

  • sklearn.preprocessing 导入 normalize
  • nmf_features 调用 normalize() 函数,并将结果存为 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())
编辑并运行代码