σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
scikit-learn skill›Module 1 · The estimator way of working›Topic 1.4·~50 minIntermediate

Custom transformers and column pipelines

Skill positionModule 1 of 4 · Topic 4 of 5
1.4 Custom transformers and column pipelines
—Lab score— / 100Total XP—Streak—

Learn in fit, apply in transform

A transformer is an estimator with a second job. fit(X) learns numbers from the data (a mean, a quantile, a vocabulary), stores them in attributes that end with an underscore, and returns self. transform(X) then applies those stored numbers to any data, new or old, without looking at that data to decide anything. This split is the whole point: the numbers are learned once, on the training rows, and the test rows are only transformed. A winsorizer is a good first example. fit learns one lower and one upper quantile per column, and transform clips with np.clip between them. If transform recomputed the quantiles on the data it receives, every dataset would be clipped to its own range and the training information would never reach the test set. Inherit from TransformerMixin and BaseEstimator and you also get fit_transform and the parameter machinery for free.

Real tables mix numbers and categories, so scikit-learn offers ColumnTransformer([(name, transformer, columns), ...]): each branch handles its columns and the outputs are glued side by side. Here the numeric branch is make_pipeline(Winsorizer(0.02, 0.98), StandardScaler()) and the categorical branch is OneHotEncoder(handle_unknown='ignore'), which turns an unseen category into a row of zeros instead of an error. sparse_threshold=0 forces a dense array. To name the output columns, every step must answer get_feature_names_out, which is why your winsorizer has to implement it: it simply returns the names it was given. The golden rule is to call fit_transform on the training rows and only transform on the test rows. On the stock panel (26 symbols, 10,296 rows) the split at 1 January 2023 leaves 5,564 training rows and 4,732 test rows, and about 2.2% of the test momentum values fall outside the training 2%–98% range and are clipped to the training bounds.

  • Learn once, apply anywhere: fit stores numbers on the training rows, transform only applies them.
  • One preprocessor, two branches: ColumnTransformer sends numbers and categories to their own steps; handle_unknown="ignore" survives new categories.
  • Implement the contract fully: a transformer that cannot name its output columns breaks get_feature_names_out for the whole pipeline.

Lab · what's graded

  • The constructor5 pts
  • Fit and transform20 pts
  • Feature names and pipelines10 pts
  • The two-branch preprocessor15 pts
  • Split by date10 pts
  • Fit on the past only10 pts
  • The panel study30 pts
Explorer · lab-sl-14
▾ lab-sl-14
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sl-14$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sl-14 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub