감성 분석을 위한 고차 n-그램
이전 연습 문제와 유사하게, 특정 영화 리뷰가 긍정적인지 부정적인지 판별하는 분류기를 만들어 보겠습니다. 다만 이번에는 n=2까지의 n-그램을 사용합니다.
n-그램으로 전처리된 학습용 리뷰는 X_train_ng에, 대응하는 테스트 리뷰는 X_test_ng에 제공됩니다. 마지막으로 y_train과 y_test를 사용해 각각 학습 및 테스트용 감성 레이블에 접근하세요.
이 연습은 강의의 일부입니다
Python으로 배우는 NLP 피처 엔지니어링
연습 안내
- MultinomialNB 인스턴스를 정의하고 이름을
clf_ng로 지정하세요. X_train_ng와y_train으로 분류기를 학습(fit)하세요.score()메서드를 사용하여X_test_ng와y_test에서accuracy를 측정하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Define an instance of MultinomialNB
clf_ng = ____
# Fit the classifier
clf_ng.____(____, ____)
# Measure the accuracy
accuracy = ____
print("The accuracy of the classifier on the test set is %.3f" % accuracy)
# Predict the sentiment of a negative review
review = "The movie was not good. The plot had several holes and the acting lacked panache."
prediction = clf_ng.predict(ng_vectorizer.transform([review]))[0]
print("The sentiment predicted by the classifier is %i" % (prediction))