高階文字特徵
當文字已清理並標準化後,你就能開始從資料中建立特徵。對於自由格式文字,最基本可計算的資訊就是它的大小,例如長度與單字數。在本練習(以及本章其餘部分)中,你會聚焦在上一題建立的清理/轉換後文字欄位(text_clean)。
本練習屬於課程
Feature Engineering for Machine Learning in Python
練習說明
- 將每篇演說的字元長度記錄在
char_count欄位。 - 將每篇演說的單字數記錄在
word_count欄位。 - 將每篇演說的平均單字長度記錄在
avg_word_length欄位。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Find the length of each text
speech_df['char_cnt'] = speech_df['text_clean'].____
# Count the number of words in each text
speech_df['word_cnt'] = speech_df['text_clean'].____
# Find the average length of word
speech_df['avg_word_length'] = ____ / ____
# Print the first 5 rows of these columns
print(speech_df[['text_clean', 'char_cnt', 'word_cnt', 'avg_word_length']])