Your First Machine Learning Model in 30 Minutes
We'll train a classifier on the classic Iris dataset. By the end you'll have predicted flower species from petal measurements — and understood every step.
Step 1 — Load the data
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X, y = load_iris(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)We split 80/20 so we can evaluate the model on data it has never seen. random_state=42 makes the split reproducible.
Step 2 — Train
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)A Random Forest is a great default: it works out of the box, handles non-linearities, and rarely overfits with sensible defaults.
Step 3 — Evaluate
from sklearn.metrics import accuracy_score, classification_report
y_pred = model.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, y_pred):.2%}")
print(classification_report(y_test, y_pred))You should see ~97% accuracy. The classification report breaks it down per species — precision, recall and F1.
What you just did
- Loaded a real dataset and split it into train/test.
- Trained a tree-based classifier with one line.
- Measured how well it generalizes to new data.
Every supervised ML project follows this exact loop. The hard parts are choosing the right metric, cleaning real-world data, and tuning the model — not the syntax.
Recommended Reading

Python for Data Analysis
Wes McKinney (3rd Edition, O'Reilly)
The definitive guide to pandas, NumPy, and the modern Python data stack — written by the creator of pandas himself.
View on Amazon
Hands-On Machine Learning
Aurélien Géron (3rd Edition, O'Reilly)
From linear regression to deep neural nets with Scikit-Learn, Keras and TensorFlow. The most recommended ML book of the decade.
View on Amazon