Machine learning tutorial: train and evaluate your first classifier
By Gistmax Tutorials · 2026-11-04
Build a classifier that predicts an iris flower species from four measurements. The aim is to understand a reliable experiment: separate training and test data, fit preprocessing only on training data, then evaluate predictions on examples the model did not learn from.
1. Install the tools
Use Python 3.12 or later in a dedicated project environment. Install scikit-learn with `python -m pip install scikit-learn`. Save the program below as `iris_classifier.py`. The dataset is bundled with the library; you do not need a separate download or an API key.
The features are measurements, and the target is a species label. This is supervised classification because the training examples include the answers. It is not a general flower-recognition application: no photographs are involved.
2. Split the examples and build a pipeline
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
data = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.30,
random_state=42, stratify=data.target
)
model = make_pipeline(
StandardScaler(),
LogisticRegression(max_iter=1000)
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
score = accuracy_score(y_test, predictions)
print("Test examples:", len(y_test))
print("Accuracy:", round(score, 3))
print("Rows = actual species; columns = predicted species")
print(data.target_names.tolist())
print(confusion_matrix(y_test, predictions, labels=[0, 1, 2]))
assert len(y_test) == 45
assert 0 <= score <= 13. Read the evaluation
Run `python iris_classifier.py`. You should see 45 test examples, an accuracy between zero and one, and a three-by-three confusion matrix. The diagonal counts correct predictions. Off-diagonal entries show which species were confused with one another.
The fixed random state makes this particular split repeatable. Stratification keeps class proportions represented in the split. Neither setting proves that the result will generalise to another collection of flowers with different measurement conditions.
4. Understand why the pipeline matters
The scaler learns from the training set when `fit` runs. The same learned transformation is then applied to test examples. Fitting the scaler to the full dataset before splitting would let information from the test set influence preparation, weakening the separation between development and evaluation.
Do not keep trying settings until this test set gives the best score. Use training-only cross-validation to compare candidates, then inspect the held-out test set after choosing the procedure. A small dataset also benefits from reporting variation across splits instead of treating one score as definitive.
Troubleshooting and extension
If scikit-learn cannot be imported, check which Python interpreter installed it. If prediction fails because the feature count is wrong, inspect `data.feature_names` and preserve the training column order. For the next exercise, compare a simpler baseline and report errors by species, not just one overall percentage.