TED 강연의 단어 수 세기
ted는 500개 TED 강연의 전사(transcript)를 담은 데이터프레임입니다. 각 강연의 대략적인 단어 수를 담는 새 특성 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[____].____)