The scikit-learn API: fit, transform, predict
Understand the estimator interface and the train/test split workflow.
The scikit-learn API: fit, transform, predict is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)fit() — Training
estimator.fit(X, y) trains the model on X (features) and y (labels). For unsupervised methods, only X is passed.
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=200, random_state=0)
model = LogisticRegression()
model.fit(X, y)
print("Trained:", model.classes_)predict() and predict_proba()
predict(X) returns class labels; predict_proba(X) returns class probabilities for classifiers.
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
X, y = make_classification(random_state=0)
model = LogisticRegression().fit(X, y)
print(model.predict(X[:5])) # [0 1 0 ...]
print(model.predict_proba(X[:2])) # [[0.7 0.3] ...]Transformers: fit and transform
Transformers (scalers, encoders) have fit(X) + transform(X). Always fit on training data only, then transform both train and test.
from sklearn.preprocessing import StandardScaler
import numpy as np
X_train = np.array([[1,2],[3,4],[5,6]])
X_test = np.array([[2,3],[4,5]])
scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train) # fit + transform
X_test_s = scaler.transform(X_test) # transform only (using train stats)Pipeline
A Pipeline chains transformers and an estimator into a single object. Prevents data leakage by applying transformations within cross-validation correctly.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
pipe = Pipeline([
("scaler", StandardScaler()),
("clf", SVC())
])
pipe.fit(X_train, y_train)
print(pipe.score(X_test, y_test))Accuracy and Other Metrics
Use accuracy_score, f1_score, classification_report to evaluate classifiers.
from sklearn.metrics import accuracy_score, classification_report
y_pred = model.predict(X_test)
print(accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))Cross-Validation
cross_val_score evaluates a model using k-fold cross-validation without manually splitting data.
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
X, y = make_classification(random_state=0)
scores = cross_val_score(LogisticRegression(), X, y, cv=5)
print(f"CV scores: {scores.mean():.3f} ± {scores.std():.3f}")Hyperparameter Tuning with GridSearchCV
GridSearchCV exhaustively searches a parameter grid using cross-validation to find the best hyperparameters.
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.datasets import make_classification
X, y = make_classification(random_state=0)
param_grid = {"C": [0.1, 1, 10], "kernel": ["rbf", "linear"]}
gs = GridSearchCV(SVC(), param_grid, cv=5)
gs.fit(X, y)
print(gs.best_params_, gs.best_score_)Preprocessing: LabelEncoder and OneHotEncoder
Encode categorical labels with LabelEncoder and categorical features with OneHotEncoder or ColumnTransformer.
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
import numpy as np
le = LabelEncoder()
print(le.fit_transform(["cat","dog","cat","fish"])) # [0 1 0 2]
ohe = OneHotEncoder(sparse_output=False)
print(ohe.fit_transform([["red"],["green"],["blue"]]))Saving and Loading Models
Persist trained models with joblib (faster than pickle for NumPy arrays).
import joblib
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
X, y = make_classification(random_state=0)
model = LogisticRegression().fit(X, y)
joblib.dump(model, "model.joblib")
loaded = joblib.load("model.joblib")
print(loaded.predict(X[:3]))Quick Check
Why should you call scaler.fit() only on training data and not on test data?
Recap
scikit-learn's uniform API: fit() trains, predict() infers, transform() pre-processes. Use Pipeline to chain steps. Split data with train_test_split, evaluate with cross_val_score, and tune with GridSearchCV.
Frequently asked questions
Is the “The scikit-learn API: fit, transform, predict” lesson free?
Yes — the full text of “The scikit-learn API: fit, transform, predict” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “The scikit-learn API: fit, transform, predict”?
Understand the estimator interface and the train/test split workflow. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The scikit-learn API: fit, transform, predict” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
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