Skip to content

About

Grade 10 classroom activity (Unit 9): a beginner Decision Tree that classifies flower type from sepal and petal length with scikit-learn. Includes the original activity pack and CLI options for depth, seed, and tree plots.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

🌳 Decision Tree — Flower Type Classifier (Unit 9)

🔍 Overview

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.


📦 Features

  • Loads a small flower dataset from flower.csv
  • Trains a DecisionTreeClassifier with 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

🧠 Dataset

Property Details
File flower.csv
Features sepal_length, petal_length
Label class (Type1, Type2, Type3)
Samples ~50 rows
Task Multi-class classification

🧰 Requirements

pip install pandas scikit-learn matplotlib

matplotlib is only needed if you use --save-plot.


⚙️ How to Run

Default run (uses flower.csv in the same folder):

python DecisionTree_Unit9.py

With options:

python DecisionTree_Unit9.py --csv flower.csv --max-depth 4 --test-size 0.30 --seed 42 --save-plot tree.png

🛠️ Command-Line Options

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

📊 Example Output

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

🎓 Learning Goals

  1. Read a CSV into a pandas DataFrame
  2. Separate features from the label
  3. Use train_test_split to create train/test sets
  4. Train a DecisionTreeClassifier and tune max_depth
  5. Evaluate the model with accuracy_score

📁 Project Structure

Decision_Tree_G10/
├── DecisionTree_Unit9.py     # Main training script
├── flower.csv                # Dataset
├── Unit9_Decision_Tree_Activity.zip  # Original classroom activity
└── README.md

📄 License

Educational use — Credits to CS42.org.

About

Grade 10 classroom activity (Unit 9): a beginner Decision Tree that classifies flower type from sepal and petal length with scikit-learn. Includes the original activity pack and CLI options for depth, seed, and tree plots.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages