시작하기무료로 시작하기

책 제목에 대한 Bag-of-Words

PyBooks에는 이제 추가 분석을 위해 인코딩해야 하는 책 제목 목록이 있어요. 데이터 팀은 Bag of Words(BoW) 모델이 가장 적합한 접근일 수 있다고 보고 있어요.

다음 패키지가 이미 임포트되어 있어요: torch, torchtext.

이 연습은 강의의 일부입니다

PyTorch로 배우는 텍스트 딥러닝

강의 보기

연습 안내

  • bag-of-words를 구현하기 위해 CountVectorizer 클래스를 임포트하세요.
  • 임포트한 클래스의 객체를 초기화한 뒤, 이 객체를 사용해 titles를 행렬 표현으로 변환하세요.
  • get_feature_names_out() 메서드를 사용해 처음 다섯 개의 피처 이름과 인코딩된 제목을 추출해 표시하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Import from sklearn
from sklearn.feature_extraction.text import ____

titles = ['The Great Gatsby','To Kill a Mockingbird','1984','The Catcher in the Rye','The Hobbit', 'Great Expectations']

# Initialize Bag-of-words with the list of book titles
vectorizer = ____()
bow_encoded_titles = ____.fit_transform(____)

# Extract and print the first five features
print(vectorizer.____[:5])
print(bow_encoded_titles.toarray()[0, :5])
코드 편집 및 실행