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
fitlearns fromXandyand returns the estimator itself.predictmaps new rows to labels;scorereports accuracy.- That one contract is why models are interchangeable.
Keywords and builtins used here
print
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.
Step 1 of 3 in The estimator API, step 1 of 25 in Machine learning with scikit-learn.