Project: Build a Model End to End
Put it all together: load a real dataset, split it, train, evaluate, and improve a model.
What you will learn
- Run the full ML workflow on a real dataset
- Compare two models fairly
- Evaluate and improve the result
What you will build
You will build a complete classifier on the famous iris flower dataset, which ships with scikit-learn. Given four flower measurements, your model will predict which of three species a flower is. You will walk every step of the workflow from Unit 1 — and feel how the pieces fit together.
Step 1 — Load and look at the data
First, load the data and check its shape so you know what you are working with.
from sklearn.datasets import load_iris
data = load_iris()
X = data.data # 150 flowers, 4 measurements each
y = data.target # species: 0, 1, or 2
print('Shape of X:', X.shape)
print('Species names:', list(data.target_names))
print('First flower:', X[0], '-> species', y[0])Note: Output: Shape of X: (150, 4) Species names: ['setosa', 'versicolor', 'virginica'] First flower: [5.1 3.5 1.4 0.2] -> species 0 We have 150 flowers, each with 4 features, and 3 possible species. The first flower is a setosa. The data is clean, so we can move straight on.
Step 2 — Split into train and test
Hold back 30% so we can grade the model honestly at the end.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42)
print('Training flowers:', len(X_train))
print('Testing flowers: ', len(X_test))Note: Output:
Training flowers: 105
Testing flowers: 45
105 flowers to learn from, 45 kept hidden for testing. random_state=42 means you get this same split every run, so your results are reproducible.
Step 3 — Train a first model
Start simple with a single decision tree as our baseline.
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(random_state=42)
tree.fit(X_train, y_train)
print('Tree trained!')Note: Output:
Tree trained!
One .fit() call and the tree has learned its flowchart of questions from the 105 training flowers. Now we find out how good it is.
Step 4 — Evaluate it
Score the tree on the hidden test set and look at the full report.
from sklearn.metrics import accuracy_score, confusion_matrix
pred = tree.predict(X_test)
print('Accuracy:', round(accuracy_score(y_test, pred), 3))
print('Confusion matrix:')
print(confusion_matrix(y_test, pred))Note: Output: Accuracy: 0.978 Confusion matrix: [[19 0 0] [ 0 12 1] [ 0 1 12]] The tree got about 97.8% of the hidden flowers right. The matrix shows almost everything on the diagonal (correct); just two flowers were confused between the two similar species.
Step 5 — Improve with a random forest
Now try to beat the baseline. A random forest (many trees voting) is an easy upgrade — and we compare the two fairly on the same test set.
from sklearn.ensemble import RandomForestClassifier
forest = RandomForestClassifier(n_estimators=100, random_state=42)
forest.fit(X_train, y_train)
forest_pred = forest.predict(X_test)
print('Tree accuracy: ', round(accuracy_score(y_test, pred), 3))
print('Forest accuracy:', round(accuracy_score(y_test, forest_pred), 3))Note: Output: Tree accuracy: 0.978 Forest accuracy: 1.0 The forest edged out the single tree, getting every hidden flower right. By averaging many trees it cleaned up the two mistakes — a real, measured improvement on the same test set.
Your tasks
- Run all five steps and confirm you get an accuracy score for each model.
- Add a third model — a
KNeighborsClassifier— and compare its accuracy too (remember KNN likes scaled features). - Print a confusion matrix for your best model and say which species it confuses.
- Use
cross_val_scoreon your best model for a more trustworthy score. - Predict the species of a made-up flower, e.g.
[[6.0, 2.9, 4.5, 1.5]], and print the name.
Watch out: Keep the same X_test and y_test for every model, and never let a model train on the test set. That is the only way the comparison is fair.
Tip: Work in small steps and run after each one. Get Step 1 printing before you write Step 2 — a finished, working pipeline beats a big broken one.
Note: When this runs, you have completed a full machine-learning project: load, split, train, evaluate, and improve — the exact workflow used on real jobs. Add it to your portfolio and GitHub!
Q. In this project, why must the tree, forest and KNN all be scored on the same Xtest and ytest?
✍️ Practice
- Add a
KNeighborsClassifier(withStandardScaler) and compare its accuracy to the tree and forest. - Predict the species of the flower
[[6.0, 2.9, 4.5, 1.5]]and print the species name, not just the number.
🏠 Homework
- Repeat the full five-step project on another built-in dataset such as
load_wine, and write 4–5 lines on which model worked best and how you improved it.