与 "Cristiano Ronaldo" 相似的文章有哪些?
在视频中,您学习了如何使用 NMF 特征和余弦相似度来查找相似的文章。
请将其应用到您针对热门 Wikipedia 文章训练的 NMF 模型上,找出与足球运动员 Cristiano Ronaldo 相关文章最相似的文章。您先前获得的 NMF 特征存放在 nmf_features 中,titles 是文章标题列表。
本练习是课程的一部分
Python 中的无监督学习
练习说明
- 从
sklearn.preprocessing导入normalize。 - 对
nmf_features调用normalize()函数,并将结果存为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())