tf/idf 벡터를 사용한 텍스트 분류
이제 volunteer 데이터셋의 title 열을 tf/idf 벡터로 인코딩했으니, 그 벡터를 사용해 category_desc 열을 예측해 보겠습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Machine Learning 전처리
연습 안내
- 클래스 분포가 고르지 않으므로,
text_tfidf벡터와 타깃 변수y를 학습용과 테스트용으로 분할할 때stratify파라미터를y로 설정하세요. scikit-learn에서 올바른 형식으로 사용하려면 tf/idf 벡터에 대해.toarray()메서드를 호출해야 한다는 점에 유의하세요. - Naive Bayes 모델
nb에X_train과y_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.____(____, ____))