開始使用免費開始

TED 演講的字數統計

ted 是一個包含 500 場 TED 演講逐字稿的 dataframe。你的任務是計算一個新的特徵 word_count,其內容為每場演講的大致單字數。同時,你也需要計算所有演講的平均字數。逐字稿已作為 tedtranscript 特徵提供。

為了完成這個任務,你需要定義一個函式 count_words,參數為一個字串,並回傳該字串中的單字數。接著,將此函式套用到 tedtranscript 特徵,以建立新的特徵 word_count,並計算其平均值。

本練習屬於課程

Python 中文本特徵工程

檢視課程

練習說明

  • 使用 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[____].____)
編輯並執行程式碼