Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Regression lessonsQUESTIONS · MODELS · EVIDENCE
Several predictors · ML-R04 · 12–18 MIN

Several predictors, conditional comparisons

Interpret one input while holding others fixed.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferExplain the Python result

Understand the idea

Multiple regression fits several coefficients together. Each coefficient describes a conditional comparison with the other model inputs held constant.

Interpret one input while holding others fixed.RowDistanceWeightA52B54Predicted difference = 2 × fitted weight coefficientChange one input while holding the other fixed.
Schematic · Interpret one input while holding others fixed.Scroll the diagram horizontally if needed.

Python skill: Passes two feature columns in a fixed order.

Meet the syntax

LinearRegression().fit(X_train[['distance','weight']], y_train)
X_train[['distance','weight']]
Passes two feature columns in a fixed order.
LinearRegression().fit
Learns both slopes together, making each coefficient conditional on the other supplied inputs.

Follow the code

Use the numbered comments to connect each Python block to the workflow above.

answer=pd.Series(model.coef_,index=['distance','weight'])

This practice: Read and run the Python. Next: Change · Several predictors, conditional comparisons.

Multiple linear regression · from idea to workflow

Question: How do several available inputs jointly predict a quantity?

Mechanism: One coefficient per prepared input adds to an intercept.

Watch for: Correlated inputs destabilise individual coefficients; coefficients do not establish causes.

Interpret or debug · self-review: Explain what holding other inputs fixed means. Would two near-duplicate inputs change coefficient interpretation even if predictions barely change?

Independent full workflow: ML-X03 · ML-X04. First practise complete regression and classification in F11–F12; check readiness in W-K2–W-K3.

Given data · MIX60

60 observations. One synthetic delivery observation generated for practice. The dataframe df is supplied afresh for each Run.

MIX60 · first 8 prepared rows
distanceweightserviceweekendduration
15.70526.75035standard061.7704
9.338694.81674express135.1694
17.31345.73931economy059.0766
14.257.69699standard156.5785
2.789376.42024express019.5577
19.53685.62508economy177.0289
15.46175.68023standard056.8109
15.93523.17871express152.08

Column meanings and units

Distance, weight and duration use the fixture’s numeric units; no kilometres, kilograms, minutes or other physical units are specified. RMSE is reported in the same synthetic duration units as the target.

These deterministic teaching observations do not describe real deliveries. Service effects and the alternating weekend flag are built into the generated response; they do not establish real-world causal effects.

distancefloat64
Numeric delivery-distance inputUnit / values: Synthetic distance units; physical unit unspecified
weightfloat64
Numeric parcel-weight inputUnit / values: Synthetic weight units; physical unit unspecified
servicestr
Delivery-service categoryUnit / values: standard / express / economy
weekendint64
Binary weekend input, alternating in the fixtureUnit / values: 0 / 1 indicator
durationfloat64
Numeric delivery-duration targetUnit / values: Synthetic duration units; physical unit unspecified
Supplied setup · available if you need to inspect it

This code runs before your editor on every Run. These are the objects your exercise uses.

from sklearn.model_selection import train_test_split
X = df[['distance','weight','service','weekend']]
y = df['duration']
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.2,random_state=42)
from sklearn.linear_model import LinearRegression
model=LinearRegression().fit(X_train[['distance','weight']],y_train)

Your task · Follow

Return named distance and weight coefficients from the supplied fitted two-feature line.

Hint 1 — Think

Coefficient labels must follow the order used when fitting the supplied two-feature model.

Hint 2 — Tools

model.coef_ and pd.Series with an index.

Hint 3 — Approach

Pair the fitted coefficients with distance and weight in training-column order.

Explained solution
answer=pd.Series(model.coef_,index=['distance','weight'])

Named coefficients preserve which conditional association each learned value describes.

Helpful prior knowledge: Read a fitted line These links are guidance, not locks.

Sources and API context

Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.

Your task · Follow

Return named distance and weight coefficients from the supplied fitted two-feature line.

answer

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.