The Estimator API
20 min
Model Evaluation's course already covered the STATISTICAL ideas —
metrics, cross-validation, bias-variance. This course covers the
practical API layer real projects use to apply those ideas: scikit-learn.
Every estimator (a model, in scikit-learn's vocabulary) shares the same
shape regardless of algorithm: .fit(X, y) trains it, .predict(X)
produces predictions. That consistency is deliberate — it's what lets
you swap DecisionTreeClassifier for LogisticRegression by changing
one line, with everything else (data prep, evaluation) unchanged.
from sklearn.tree import DecisionTreeClassifier
def train_and_predict(X_train, y_train, X_test):
model = DecisionTreeClassifier(random_state=0)
model.fit(X_train, y_train)
return [int(p) for p in model.predict(X_test)]
random_state=0 matters here for the SAME reason it mattered in Model Deployment and ML Experiment Design -- some parts of tree-building involve tie-breaking choices, and pinning the seed is what makes .fit() produce the exact same tree (and therefore exact same predictions) every single run, which is required for grading.
Write `train_and_predict(X_train, y_train, X_test)`: train a `DecisionTreeClassifier(random_state=0)` on `X_train`/`y_train`, then return its predictions on `X_test` as a plain list of ints.
Why does virtually every scikit-learn model share the exact same `fit(X, y)` / `predict(X)` interface, regardless of the underlying algorithm?
You can train and predict with any scikit-learn estimator via its shared fit/predict interface, and understand why that consistency across algorithms is deliberate.