开始使用免费开始使用

TED 演讲的词数统计

ted 是一个包含 500 场 TED 演讲文字稿的数据框。您的任务是计算一个新的特征 word_count,其值为每场演讲的大致词数。随后,您还需要计算所有演讲的平均词数。文字稿在 ted 中的特征名为 transcript

为完成此任务,您需要先定义函数 count_words,该函数接收一个字符串参数并返回该字符串中的词数。然后,将此函数应用到 tedtranscript 特征,创建新特征 word_count,并计算它的均值。

本练习是课程的一部分

Python 中的 NLP 特征工程

查看课程

练习说明

  • 使用 split() 方法将 string 拆分为词列表。
  • 使用 len() 返回 words 中元素的数量。
  • 将您的函数应用到 tedtranscript 列以创建新特征 word_count
  • 使用 mean() 计算所有演讲的平均词数。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Function that returns number of words in a string
def count_words(string):
	# Split the string into words
    words = ____.____
    
    # Return the number of words
    return ____(____)

# Create a new feature word_count
ted['word_count'] = ted[____].apply(____)

# Print the average word count of the talks
print(ted[____].____)
编辑并运行代码