找出最正面和最负面的词
在本练习中,您将尝试解读一个在电影评论情感数据集上训练的逻辑回归模型的系数。模型对象已实例化并保存在变量 lr 中。
此外,与各个特征对应的词已加载到变量 vocab 中。例如,如果 vocab[100] 是 "think",则表示第 100 个特征对应于该电影评论中单词 "think" 出现的次数。
本练习是课程的一部分
Python 中的线性分类器
练习说明
- 找出对应于 5 个最大系数的词。
- 找出对应于 5 个最小系数的词。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Get the indices of the sorted cofficients
inds_ascending = np.argsort(lr.coef_.flatten())
inds_descending = inds_ascending[::-1]
# Print the most positive words
print("Most positive words: ", end="")
for i in range(5):
print(____, end=", ")
print("\n")
# Print most negative words
print("Most negative words: ", end="")
for i in range(5):
print(____, end=", ")
print("\n")