Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Regression lessons
Several predictors · ML-R05 · 12–18 MIN

Read coefficients after encoding

Align coefficients with encoded feature names and a reference category.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferCompute and inspect the evidence

Understand the idea

Dropping one category gives an explicit reference for linear coefficients with an intercept. Category coefficients compare with that reference, conditional on other inputs. The fitted encoder exposes categories_ and drop_idx_. Read its category list at the dropped index to find the reference; do not guess it from frequency or original row order.

Align coefficients with encoded feature names and a reference category.Serviceexpressstandardeconomy00express10standard01Economy is the omitted reference in this example.
Align coefficients with encoded feature names and a reference category.Scroll the diagram horizontally if needed.

What the syntax does

model.named_steps['prepare']
  • named_steps maps the authored pipeline names to their fitted objects.
  • Reading a contained estimator’s fitted coefficients or preparation schema does not fit again.
  • Use the exact step names from the supplied recipe.
  • The prepare step contains fitted preparation; model contains the prediction estimator.
  • A ColumnTransformer also exposes its named_transformers_ when inspecting encoded categories.
drop='first'
  • Omits one category indicator so coefficients compare other categories with that reference.
encoder.categories_[0]
  • The first categorical input’s fitted category list, in encoder order.

Supplied for this practice

These objects are prepared afresh before your editor runs.

X
Feature dataframe for the declared population, preserving row indices.
X = df[['distance', 'weight', 'service', 'weekend']]
y
Target series, aligned with X.
y = df['duration']
X_train / X_test / y_train / y_test
Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with X_train. Final-test targets, aligned with X_test.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
prepare
Already fitted on X_train, y_train as part of model. Supplied prepare; its definition below specifies the values and role. Fitted on X_train, y_train; reuse without refitting.
model
Already fitted on X_train, y_train. Fitted estimator or pipeline for the requested model family. Fitted on X_train, y_train; reuse without refitting.

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
MIX60 · first 8 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
Your task
  1. Identify the omitted service reference from the fitted encoder.
answer
Hints

Think: The omitted indicator establishes the category against which the encoded coefficient is compared.

Tools: named_transformers_, categories_ and drop_idx_.

Approach: Reach the fitted service encoder and use its dropped-position metadata to identify the reference.

Explained solution
encoder=model.named_steps['prepare'].named_transformers_['service']
answer=encoder.categories_[0][encoder.drop_idx_[0]]

Reading fitted metadata avoids assuming an alphabetical reference or guessing from input order.

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.