시작하기무료로 시작하기

tf/idf 벡터를 사용한 텍스트 분류

이제 volunteer 데이터셋의 title 열을 tf/idf 벡터로 인코딩했으니, 그 벡터를 사용해 category_desc 열을 예측해 보겠습니다.

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

Python으로 배우는 Machine Learning 전처리

강의 보기

연습 안내

  • 클래스 분포가 고르지 않으므로, text_tfidf 벡터와 타깃 변수 y를 학습용과 테스트용으로 분할할 때 stratify 파라미터를 y로 설정하세요. scikit-learn에서 올바른 형식으로 사용하려면 tf/idf 벡터에 대해 .toarray() 메서드를 호출해야 한다는 점에 유의하세요.
  • Naive Bayes 모델 nbX_trainy_train 데이터를 학습(fit)하세요.
  • 테스트 세트 정확도를 출력하세요.

실습형 인터랙티브 연습

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

# Split the dataset according to the class distribution of category_desc
y = volunteer["category_desc"]
X_train, X_test, y_train, y_test = ____(____.toarray(), ____, ____=____, random_state=42)

# Fit the model to the training data
nb.____(____, ____)

# Print out the model's accuracy
print(nb.____(____, ____))
코드 편집 및 실행