Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Regression lessons
Curves and trees · ML-R-R2 · 15–20 MIN

Flexibility retrieval

  1. Retrieval 1Retrieve and applyCurrent exercise
  2. Retrieval 2Retrieve and apply
  3. Retrieval 3Retrieve and apply

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
INTAKE48 · first 8 rows
case_idbacklog_at_opendevice_age_yearscompletion_hoursinvoice_hours
410072.881579.428239.67823
4101118.8748517.258117.5081
410232.28967.746697.99669
4103131.1721814.778515.0285
410434.751785.764346.01434
410563.256848.094768.34476
410624.570924.098194.34819
4107111.4274311.868412.1184
Your task
  1. Retrieve two-feature polynomial expansion for the intake measurements in INTAKE48.
  2. Expand backlog_at_open and device_age_years to degree two without a bias column.
  3. Return answer as a dataframe with all five fitted feature-name columns and the original row index.
  4. Verify that the interaction column contains the product of the two available measurements.
answer
Hints

Think: Expanded terms represent available inputs; neither ID nor invoice belongs in the expansion.

Tools: PolynomialFeatures.fit_transform and get_feature_names_out.

Approach: Keep original observation indices and name each numerical term in fitted feature order.

Explained solution
from sklearn.preprocessing import PolynomialFeatures
expander = PolynomialFeatures(degree=2,include_bias=False)
values = expander.fit_transform(df[['backlog_at_open','device_age_years']])
answer = pd.DataFrame(values,index=df.index,columns=expander.get_feature_names_out())

The interaction term lets a coefficient’s effective contribution depend on the other input. Constructing that term does not establish a useful validation improvement.

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.