One grammar for many models
scikit-learn wraps models and preprocessing steps in objects with a small, consistent API. A predictor learns with fit(X, y), produces outputs with predict(X), and summarizes held-out performance with score(X, y). A transformer learns with fit(X) and applies the learned rule with transform(X).
Python
Fit on the training split, then evaluate on the test split
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Predict first
After model.fit(X_train, y_train), what does model.predict(X_test) return?
Split before you look
The test set stands in for data the model has never seen. If you scale, impute, or select features using the full dataset before splitting, information from the test set leaks into training. Split first, then treat the test features as if they arrive tomorrow.
Python
A reproducible, stratified split
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
- random_state makes the split reproducible.
- stratify=iris.target keeps the class proportions in both splits.
- Keep X_test and y_test untouched until the final evaluation.
Try it
Split the iris data first, then fit a StandardScaler on the training rows only. Report the training and test row counts.
Your solution
ReadyResults
Run the tests to check your solution. The first run downloads Python; later runs reuse the session.
Predictions are not probabilities
predict returns the model's chosen label or value. Many classifiers also expose predict_proba for per-class probabilities. Use predict when you need a decision, and predict_proba when the confidence or ranking matters.
Python
A label plus a vector of class probabilities
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Put the steps in a pipeline
A Pipeline chains transformers and a final estimator. Calling fit on the pipeline fits each transformer on the training data, transforms it, and then fits the next step. Calling predict on the pipeline applies the saved transformations before predicting.
Python
Scaling and prediction as one object
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
The pipeline learns the scaler's mean and standard deviation from X_train only. At prediction time it applies those same training statistics to X_test. That is the behavior you want in production.
Inspect what the model learned
Fitted attributes end in an underscore. A scaler stores mean_ and scale_; a classifier stores classes_; a pipeline exposes named_steps. Inspecting these values is often faster than guessing why a score changed.
Python
Read the learned state from named steps
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Practice
Make it stick
Run the tests as often as you like. Hints and the solution are available when you need them.
Exercise
Split the iris data into 80% training and 20% test rows with random_state=42 and stratification. Fit KNeighborsClassifier(n_neighbors=3), predict the test labels, and store the test accuracy in accuracy.
Your solution
ReadyResults
Run the tests to check your solution. The first run downloads Python; later runs reuse the session.
Quiz
Check your understanding
Lesson progress