A classroom Decision Tree activity that predicts whether a small business location will be successful based on three intuitive features:
| Feature | Meaning |
|---|---|
| Person | Foot traffic — number of people passing per hour |
| Income | Average income in the area (in thousands) |
| Shops | Number of competing shops nearby |
The label is Successful → Yes / No.
- Built-in sample dataset (15 rows) — no external data needed to start
- Auto-creates
location.csvon first run - Trains a
DecisionTreeClassifierwith a configurablemax_depth - Reports test-set accuracy
- NEW: All key parameters exposed as command-line options
- NEW: Optional tree visualization saved as a PNG (
--save-plot) - NEW:
--regenerateflag to refresh the CSV from the built-in sample - 🐛 Bug fix: previous version used nested-list column headers (
[["Person",...]]) which broke pandas indexing — fixed to a flat list
| Person | Income | Shops | Successful |
|---|---|---|---|
| 20 | 22 | 3 | Yes |
| 30 | 17 | 8 | No |
| 10 | 30 | 2 | Yes |
| 50 | 15 | 3 | Yes |
| 60 | 16 | 5 | Yes |
| 5 | 20 | 4 | No |
| ... | ... | ... | ... |
15 rows total. Saved to location.csv automatically the first time you run the script.
pip install -r requirements.txtDefault run (creates location.csv if missing, trains, and prints accuracy):
python Location_DT_Activity.pyCustom run:
python Location_DT_Activity.py --csv location.csv --max-depth 3 --test-size 0.25 --seed 42 --save-plot tree.pngRegenerate the CSV from the built-in sample:
python Location_DT_Activity.py --regenerate| Flag | Default | Description |
|---|---|---|
--csv |
location.csv |
Path to the CSV dataset (auto-created if missing) |
--max-depth |
2 |
Max depth of the decision tree |
--test-size |
0.25 |
Fraction of data used for testing (0.0–1.0) |
--seed |
None |
Random seed for reproducible splits |
--save-plot |
off | If set, saves a tree visualization PNG |
--regenerate |
off | Overwrite the CSV with the built-in sample before training |
Created sample dataset at: location.csv
The dataset:
Person Income Shops Successful
0 20 22 3 Yes
1 30 17 8 No
...
The predictions of the model are:
['Yes' 'No' 'Yes' 'Yes']
Accuracy = 0.7500
- Build a DataFrame from a Python list of records
- Save / load CSV files with pandas
- Separate features from the label
- Use
train_test_splitfor a fair evaluation - Train a
DecisionTreeClassifierand reason aboutmax_depth - Interpret the resulting tree visually (
--save-plot)
Location-Decision-Tree-/
├── Location_DT_Activity.py # Main script
├── location.csv # Auto-generated on first run
├── requirements.txt # Python dependencies
└── README.md
Educational use — Credits to CS42.org.