The scikit-learn API: fit, transform, predict
Understand the estimator interface and the train/test split workflow.
What Is scikit-learn?
scikit-learn is the go-to Python library for classical machine learning. It provides a consistent API: every estimator has fit(), and most have predict() or transform().
from sklearn.linear_model import LinearRegression
import numpy as np
X = np.array([[1],[2],[3],[4],[5]])
y = np.array([2, 4, 6, 8, 10])
model = LinearRegression().fit(X, y)
print(model.predict([[6]])) # [12.]Train/Test Split
Always split data before training to evaluate how well the model generalises to unseen data.
from sklearn.model_selection import train_test_split
import numpy as np
X = np.random.rand(100, 5)
y = (X[:,0] > 0.5).astype(int)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
print(X_train.shape, X_test.shape) # (80,5) (20,5)All lessons in this course
- The scikit-learn API: fit, transform, predict
- Linear Models: Regression and Classification
- Tree-Based Models: Decision Trees and Random Forests
- Model Evaluation and Cross-Validation