The scikit-learn API

Nearly every scikit-learn model uses the same fit / predict pattern.

Syntaxmodel.fit(X_train, y_train); model.predict(X_test)

scikit-learn's genius is a consistent API. Once you learn it, every model works the same way.

The machine-learning workflow: raw data flows into training, then evaluation, then prediction on new dataDataTrain(model.fit)Evaluate(metrics)Predict(model.predict)The machine-learning workflowFit on training data, check quality on held-out data, then predict on new inputs.
The core ML loop: data becomes a trained model, which is evaluated and then used to predict.

The pattern

  1. Create a model: model = SomeModel().
  2. Train it: model.fit(X_train, y_train).
  3. Predict: model.predict(X_test).
  4. Score it: model.score(X_test, y_test).

Example

Example · python
from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([[1], [2], [3], [4]])
y = np.array([2, 4, 6, 8])

model = LinearRegression()
model.fit(X, y)                 # learn
print(model.predict([[5]]))    # [10.]  predict
print(model.score(X, y))       # 1.0   perfect fit

When to use it

  • An ML engineer swaps a LogisticRegression for a RandomForestClassifier in one line because both share the same fit/predict interface.
  • A team wraps any scikit-learn estimator in a Pipeline to guarantee preprocessing and prediction always happen together as a single object.
  • A data scientist uses the fit/transform pattern of StandardScaler on train data only, then calls transform alone on the test set.

More examples

Fit and predict pattern

Demonstrates the universal scikit-learn fit/predict pattern: one method to train and one to infer, consistent across every estimator.

Example · python
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True)

clf = RandomForestClassifier(n_estimators=50, random_state=42)
clf.fit(X, y)              # learn
preds = clf.predict(X)     # predict
print(preds[:10])

predict_proba for soft predictions

Calls predict_proba to retrieve class probabilities instead of hard labels, useful when you need confidence scores for ranking or thresholding.

Example · python
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_breast_cancer

X, y = load_breast_cancer(return_X_y=True)
model = LogisticRegression(max_iter=5000).fit(X, y)

probs = model.predict_proba(X[:5])
print(probs)  # probability of each class

Transformer fit_transform pattern

Uses fit_transform on a scaler and then a PCA object, demonstrating the transformer API where fitting and transforming training data is done together.

Example · python
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import numpy as np

X = np.random.randn(100, 10)

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)  # fit + transform in one call

pca = PCA(n_components=3)
X_reduced = pca.fit_transform(X_scaled)
print(X_reduced.shape)

Discussion

  • Be the first to comment on this lesson.