Latticework

Command Palette

Search for a command to run...

Scikit-learn

The Estimator API

20 min

Explanation

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)]
Try it

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.

Loading editor…
Exercise

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.

Quiz

Why does virtually every scikit-learn model share the exact same `fit(X, y)` / `predict(X)` interface, regardless of the underlying algorithm?

Checkpoint

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.