σμ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.1·~40 minBeginner

The estimator API

Skill positionModule 1 of 4 · Topic 1 of 5
1.1 The estimator API
—Lab score— / 100Total XP—Streak—

A model is an object with a contract

Everything in scikit-learn is an estimator, and every estimator follows the same contract. You create it with its settings, called hyperparameters, for example the strength of a penalty. You call fit(X,y)\mathrm{fit}(X,y)fit(X,y) so that it learns from data. What it learned is stored in attributes whose names end with an underscore, such as the mean or the coefficients. Then you use it: a preprocessor offers transform(X)\mathrm{transform}(X)transform(X), a model offers predict(X)\mathrm{predict}(X)predict(X). The call fit_transform\mathrm{fit\_transform}fit_transform is fit followed by transform. The table XXX has one row per sample and one column per feature, and yyy is the target. Because the contract is the same everywhere, you can swap a model for another and keep the rest of your code.

z=x−μ^σ^,y^=a^+b^ xz=\frac{x-\hat\mu}{\hat\sigma},\qquad \hat y=\hat a+\hat b\,xz=σ^x−μ^​​,y^​=a^+b^x(1)

The golden rule is that fit\mathrm{fit}fit may only see training data. The numbers a preprocessor learns, such as μ^\hat\muμ^​ and σ^\hat\sigmaσ^, are then applied unchanged to validation and test data by transform\mathrm{transform}transform. If you fitted the scaler on all days, the test days would have shaped the scaling, which is a quiet leak of the future. Settings such as random_state\mathrm{random\_state}random_state make runs repeatable, and clone\mathrm{clone}clone builds a fresh unfitted copy of an estimator, which is what cross-validation does for every fold. In the market-data labs of this skill, rows are stock-months and columns are features such as momentum and volatility.

Two tiny fits by handFit a standard scaler on the training values 2,4,6,82,4,6,82,4,6,8. It learns the mean 555 and the standard deviation 5=2.236\sqrt5=2.2365​=2.236. A new value 777 becomes (7−5)/2.236=0.894(7-5)/2.236=0.894(7−5)/2.236=0.894. Now fit a linear regression on (x,y)=(1,2),(2,4),(3,7)(x,y)=(1,2),(2,4),(3,7)(x,y)=(1,2),(2,4),(3,7). It learns the slope 2.52.52.5 and the intercept −0.667-0.667−0.667, so a prediction for x=4x=4x=4 is −0.667+2.5×4=9.333-0.667+2.5\times4=9.333−0.667+2.5×4=9.333.
  • Settings in, numbers learned: hyperparameters go into the constructor, learned numbers end with an underscore.
  • Fit on training only: transform and predict reuse the learned numbers on any data.
  • Same contract everywhere: swapping a model for another keeps the rest of your code.

Lab · what's graded

  • What the scaler learns: the mean10 pts
  • What the scaler learns: the spread10 pts
  • Transform a test value10 pts
  • What the regression learns: the slope15 pts
  • A prediction10 pts
  • Which attributes are learned10 pts
  • A safe workflow10 pts
  • Match the vocabulary10 pts
  • Why fit the scaler on training data only15 pts
0/ 9 answered
Tasks
1Numeric answer10 ptsNot answered

A standard scaler is fitted on the training values 2,4,6,82,4,6,82,4,6,8. What is the learned mean?

m=m =m=accepted: ± 0.01
2Numeric answer10 ptsNot answered

The values 2,4,6,82,4,6,82,4,6,8 have mean 555 and squared deviations 9,1,1,99,1,1,99,1,1,9. The scaler uses the population standard deviation (divide by n=4n=4n=4). What is it? Give three decimals.

s=s =s=accepted: ± 0.003
3Numeric answer10 ptsNot answered

The scaler learned mean 555 and standard deviation 2.2362.2362.236. A test value 777 arrives. What does transform return, (7−5)/2.236(7-5)/2.236(7−5)/2.236? Give three decimals.

z=z =z=accepted: ± 0.003
4Numeric answer15 ptsNot answered

A linear regression is fitted on (x,y)=(1,2),(2,4),(3,7)(x,y)=(1,2),(2,4),(3,7)(x,y)=(1,2),(2,4),(3,7). The slope is ∑(x−xˉ)(y−yˉ)/∑(x−xˉ)2\sum(x-\bar x)(y-\bar y)/\sum(x-\bar x)^{2}∑(x−xˉ)(y−yˉ​)/∑(x−xˉ)2 with xˉ=2\bar x=2xˉ=2, yˉ=4.333\bar y=4.333yˉ​=4.333. What is the learned slope? Give one decimal.

b=b =b=accepted: ± 0.05
5Numeric answer10 ptsNot answered

The regression learned slope 2.52.52.5 and intercept −0.667-0.667−0.667. What does predict return for x=4x=4x=4? Give three decimals.

y=y =y=accepted: ± 0.005
6Choice10 ptsNot answered

Which of these are numbers learned by fit rather than settings you choose? Pick all that apply.

7Order the steps10 ptsNot answered

Put the steps of a safe modelling workflow in order.

Drag to reorder, or focus an item, press Space to lift it, the arrow keys to move it and Space to drop it.

  1. 1Fit the model on the transformed training data
  2. 2Split the days into training and test by time
  3. 3Predict on the transformed test data and score
  4. 4Apply transform to the training and the test data
  5. 5Fit the scaler on the training data only
8Match10 ptsNot answered

Match each call with what it does.

Pick the partner of each item on the left.

fit(X, y)
transform(X)
predict(X)
clone(estimator)
9Explain in words15 ptsNot answered

In one or two sentences, explain why a scaler should be fitted on the training days only and not on all the days.

Math modeKeyboard or LaTeX inputTask 1 of 9 · Ctrl ⏎ to check

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