TEDトークの単語数
ted は、500本のTEDトークの書き起こしを含むデータフレームです。各トークのおおよその単語数を表す新しい特徴量 word_count を作成し、さらにトークの平均単語数も計算します。書き起こしは ted の transcript という特徴量として利用できます。
この課題を完了するには、文字列を引数に取り、その文字列内の単語数を返す関数 count_words を定義します。次に、この関数を ted の transcript 特徴量に適用して新しい特徴量 word_count を作成し、その平均を計算します。
この演習はコースの一部です
Pythonで学ぶNLPの特徴量エンジニアリング
演習の手順
split()メソッドを使ってstringを単語のリストに分割します。len()を使って、wordsの要素数を返します。- 作成した関数を
tedのtranscript列に適用し、新しい特徴量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[____].____)