Numeric and category columns together.
Use separate preprocessing paths for numeric and categorical fields without leaking statistics.
Why this workflow exists.
Real tables mix kinds of data. An income field is numeric and may need imputation and scaling. A plan name is categorical and should not be turned into a fake ordered number. ColumnTransformer sends each kind through its own preparation chain, then joins the resulting columns for one classifier.
The script constructs a small illustration with deliberate missing values. It splits before fitting the pipeline. That means imputation statistics, scaling values, and category vocabulary come only from training rows. handle_unknown='ignore' defines what happens if a future row has a plan name absent during fitting. The result is a complete preprocessing contract carried with the model.
Code context. Python 3 with pandas and scikit-learn installed. All rows are defined in the script.
The whole example.
import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
rows = pd.DataFrame({
"income": [30, 42, np.nan, 75, 80, 25, 55, 62, 29, 90, 47, 68],
"plan": ["basic", "basic", "plus", "plus", "pro", "basic",
"plus", "pro", "basic", "pro", "plus", "pro"],
"renewed": [0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1],
})
X = rows[["income", "plan"]]
y = rows["renewed"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, stratify=y, random_state=4
)
numeric = make_pipeline(SimpleImputer(strategy="median"), StandardScaler())
category = make_pipeline(
SimpleImputer(strategy="most_frequent"),
OneHotEncoder(handle_unknown="ignore"),
)
prepare = ColumnTransformer([
("numeric", numeric, ["income"]),
("category", category, ["plan"]),
])
model = make_pipeline(prepare, LogisticRegression(max_iter=1000))
model.fit(X_train, y_train)
print("Held-out accuracy:", model.score(X_test, y_test))
print("New row:", model.predict(pd.DataFrame({
"income": [50], "plan": ["plus"]
})))What each stage does.
The DataFrame keeps named columns together. X excludes renewed, which is the answer. train_test_split divides aligned rows. The numeric chain fills missing income with a training median and standardizes it. The category chain fills missing plan values with the most frequent training category and creates one-hot columns. ColumnTransformer joins both outputs.
The final logistic model receives only the transformed training rows during fit. score applies the entire fitted pipeline to held-out rows, so no test statistic is used to learn the transformation. The final prediction passes a one-row DataFrame with the same named input columns. This is a coherent object from raw fields to a label, not several disconnected preprocessing calls.
What the output means.
The script prints an illustrative held-out accuracy and one label. Twelve rows make that score highly unstable, so it is not evidence for a real renewal decision.
Where the example stops.
A real renewal dataset would require a much larger, representative sample, temporal validation, careful feature definitions, and review of the consequences of using the prediction.