typestar

The estimator API in Python

Every model in scikit-learn is the same shape: construct, fit, predict.

from sklearn.linear_model import LogisticRegression

X = [[0.5, 1.0], [1.5, 2.0], [2.5, 3.5], [3.0, 4.0]]
y = [0, 0, 1, 1]

model = LogisticRegression()
model.fit(X, y)

print(model.predict([[2.0, 2.5]]))
print(model.score(X, y))
print(model.coef_.shape, model.intercept_)

How it works

  1. fit learns from X and y and returns the estimator itself.
  2. predict maps new rows to labels; score reports accuracy.
  3. That one contract is why models are interchangeable.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
272
Tokens
95
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 57 seconds.

Type this snippet

Step 1 of 3 in The estimator API, step 1 of 25 in Machine learning with scikit-learn.

Next →