All materials
pipeline-template.py
pypipeline-template.py
"""
Pipeline Template: scikit-learn Pipeline with ColumnTransformer
This template provides the structure for building a Pipeline that handles
heterogeneous data (numeric, categorical, and text features) with proper
leakage prevention. All preprocessing happens inside the Pipeline, ensuring
fit() is called only on training data during cross-validation.
Fill in the column lists and transformer choices below.
"""
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.model_selection import cross_val_score, train_test_split
# ============================================================
# Step 1: Define your column groups
# ============================================================
# Fill in the column names from your dataset profiling.
# Each group gets a different transformer.
# Numeric columns: continuous values that need scaling
# Example: ["age", "income", "score"]
NUMERIC_COLUMNS = [
# TODO: Fill in numeric column names from placement-data.csv
]
# Categorical columns: discrete values that need encoding
# Example: ["region", "department", "shift"]
CATEGORICAL_COLUMNS = [
# TODO: Fill in categorical column names from placement-data.csv
]
# Text columns: free-text fields that need TF-IDF
# Example: ["bio", "notes"]
TEXT_COLUMNS = [
# TODO: Fill in text column names from placement-data.csv
]
# ============================================================
# Step 2: Build the ColumnTransformer
# ============================================================
# Each transformer handles one column group.
# Use column NAMES, not indices -- indices break when column order changes.
preprocessor = ColumnTransformer(
transformers=[
("numeric", StandardScaler(), NUMERIC_COLUMNS),
("categorical", OneHotEncoder(handle_unknown="ignore"), CATEGORICAL_COLUMNS),
# For text columns, TfidfVectorizer handles one column at a time.
# If you have multiple text columns, add a separate transformer for each.
# ("text_bio", TfidfVectorizer(max_features=500), "column_name"),
# ("text_notes", TfidfVectorizer(max_features=300), "column_name"),
],
remainder="drop", # Drop columns not listed above
)
# ============================================================
# Step 3: Build the full Pipeline
# ============================================================
# The Pipeline chains: preprocessor -> feature selection -> estimator.
# Feature selection MUST be inside the Pipeline for leakage prevention.
pipeline = Pipeline(
steps=[
("preprocessor", preprocessor),
("feature_selection", SelectKBest(f_classif, k=50)), # Adjust k based on feature count
# TODO: Add your estimator here
# ("classifier", LogisticRegression(max_iter=1000)),
]
)
# ============================================================
# Step 4: Train and evaluate with cross-validation
# ============================================================
# Cross-validation ensures feature selection runs on each training fold
# independently -- no information from the test fold leaks in.
# X = your feature DataFrame (all columns listed above)
# y = your target variable
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
# cv_scores = cross_val_score(pipeline, X_train, y_train, cv=5, scoring="f1")
# print(f"CV Scores: {cv_scores}")
# print(f"Mean: {cv_scores.mean():.3f}, Std: {cv_scores.std():.3f}")