For each exercise, start with the KaggleHub download, CSV-loading, and train/test-splitting code from this tutorial. Use the same reviews_train, labels_train, reviews_test, and labels_test lists so you can compare your models on the same held-out test data.
In topic classification, removing English stopwords (stop_words='english') often reduces noise. In this exercise, you will test whether removing stopwords helps or hurts when detecting deceptive hotel reviews.
CountVectorizer(stop_words='english'), TfidfTransformer, and SGDClassifier, and compare its test set accuracy against the default CountVectorizer(stop_words=None).np.argsort and explain why pronouns and function words matter in deception detection.Setting up a vectorizer that strips English stopwords:
vectorizer = CountVectorizer(
stop_words='english', # Remove common English function words and pronouns
ngram_range=(1, 1)
)
In this exercise, you will explore whether character N-grams or multi-word phrases (bigrams and trigrams) better capture the formulaic writing style of fake reviewers.
TfidfVectorizer and SGDClassifier.GridSearchCV to compare word N-grams (analyzer='word', ngram_range=(1, 3)) against character N-grams within word boundaries (analyzer='char_wb', ngram_range=(3, 5)).reviews_test).Using the results of the previous exercises and the pickle module of the standard library, write a command line utility that reads a hotel review provided on stdin and predicts whether the review is 'truthful' or 'deceptive'.
Bonus point if the utility is able to give a confidence level for its predictions (hint: use loss='log_loss' in SGDClassifier or use MultinomialNB to enable .predict_proba()).
AI Programming 1 Course Materials by Brian Bird, written in are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.