The goal of this guide is to explore the main scikit-learn text processing tools on a single practical task: analyzing a collection of hotel reviews to detect whether each review is truthful (real) or deceptive (fake). Unlike topic classification—where specific subject nouns like "gpu" or "atheism" reveal the category—deception detection requires recognizing subtle stylistic patterns, pronoun usage, and hyperbolic phrasing.
In this section we will see how to:
Load the review text and labels from a CSV file and split them into training and test sets.
Extract numerical feature vectors suitable for machine learning while avoiding data leakage1.
Train probabilistic (Naïve Bayes) and linear (Support Vector Machine) models to perform classification.
Interpret model decisions by inspecting feature weights and confusion matrices.
Use a grid search strategy to find a good configuration of both the feature extraction components and the classifier.
Tutorial setup
To get started with this tutorial, create a virtual environment and install KaggleHub, NumPy, and scikit-learn. Run these commands from the tutorial folder:
uv venv --python 3.14
uv pip install kagglehub numpy scikit-learn
# Activate the venv using commands for your os and shell
In VS Code, select the .venv environment as the Python interpreter.
Finding Deceptive Opinion Spam by Any Stretch of the Imagination. M. Ott, Y. Choi, C. Cardie, and J.T. Hancock. 2011. In Proceedings of the 49th Annual Meeting of the Association for Computational Linguistics: Human Language Technologies.
Negative Deceptive Opinion Spam. M. Ott, C. Cardie, and J.T. Hancock. 2013. In Proceedings of the 2013 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies.
This tutorial was patterned after “Working with Text Data” in the original scikit-learn Tutorials written for V 1.4 by scikit-learn developers under a BSD License.
The first draft of this tutorial was generated by Gemini 4.0 Argon and revised by Brian Bird.
Data leakage is when information from the test data gets into the training process, for example by building the vocabulary or the IDF weights from all the reviews instead of only the training reviews. It makes a model look more accurate on the test set than it will be on new reviews. ↩