The scikit-learn API
Nearly every scikit-learn model uses the same fit / predict pattern.
Syntax
model.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 pattern
- Create a model:
model = SomeModel(). - Train it:
model.fit(X_train, y_train). - Predict:
model.predict(X_test). - Score it:
model.score(X_test, y_test).
Example
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 fitWhen 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.
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.
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 classTransformer 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.
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