A beginner-friendly Decision Tree classifier built with scikit-learn that predicts the flower type (Type1, Type2, Type3) from two simple features: sepal length and petal length.
This was originally an in-class Unit 9 activity used to introduce supervised learning, train/test splits, and tree-based classifiers in a hands-on way.
- Loads a small flower dataset from
flower.csv - Trains a
DecisionTreeClassifierwith a configurable depth - Splits data into train/test sets
- Reports accuracy on the test set
- NEW: Optional tree visualization saved as a PNG (
--save-plot) - NEW: Configurable random seed for reproducible runs
- NEW: All key hyperparameters available as command-line options
| Property | Details |
|---|---|
| File | flower.csv |
| Features | sepal_length, petal_length |
| Label | class (Type1, Type2, Type3) |
| Samples | ~50 rows |
| Task | Multi-class classification |
pip install pandas scikit-learn matplotlib
matplotlibis only needed if you use--save-plot.
Default run (uses flower.csv in the same folder):
python DecisionTree_Unit9.pyWith options:
python DecisionTree_Unit9.py --csv flower.csv --max-depth 4 --test-size 0.30 --seed 42 --save-plot tree.png| Flag | Default | Description |
|---|---|---|
--csv |
flower.csv |
Path to the CSV dataset |
--max-depth |
3 |
Max depth of the decision tree |
--test-size |
0.30 |
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 to the given path |
The dataset:
sepal_length petal_length class
0 5.1 1.5 Type1
1 4.8 1.4 Type1
...
The actual flower type: ['Type2' 'Type1' 'Type3' ...]
The predicted flower type: ['Type2' 'Type1' 'Type3' ...]
My decision tree accuracy = 0.9333
- Read a CSV into a
pandasDataFrame - Separate features from the label
- Use
train_test_splitto create train/test sets - Train a
DecisionTreeClassifierand tunemax_depth - Evaluate the model with
accuracy_score
Decision_Tree_G10/
├── DecisionTree_Unit9.py # Main training script
├── flower.csv # Dataset
├── Unit9_Decision_Tree_Activity.zip # Original classroom activity
└── README.md
Educational use — Credits to CS42.org.