scikit-learn’s power is one consistent interface: every model has fit and predict. Learn it once and you can train, use, and swap any algorithm in a single line.
Why: every scikit-learn estimator shares the same interface — fit to learn, predict to use, score to evaluate — so switching algorithms is a one-line change. When: learn this pattern once; it holds for regressors, classifiers, and transformers alike. Where: this uniformity is why sklearn is the default for classical ML.
pip install scikit-learnWhy: training is fit(X, y), using the model is predict(X), and measuring it is score — three methods that never change. When: X is a 2-D array of shape (samples, features) and y is the target; every model expects this. Where: because the API is identical, you can drop in a different estimator without touching the surrounding code.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True) # X: (150, 4), y: (150,)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y)
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train) # learn
preds = model.predict(X_test) # use
print("accuracy:", model.score(X_test, y_test)) # evaluateWhy: because the API is uniform, comparing algorithms is trivial — instantiate a different class and the rest of your code is unchanged. When: always try several models on the same split before committing to one. Where: this is the fastest way to find a strong baseline.
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
for model in [LogisticRegression(max_iter=1000),
RandomForestClassifier(random_state=42),
SVC()]:
model.fit(X_train, y_train)
print(type(model).__name__, round(model.score(X_test, y_test), 3))