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.
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
ColumnTransformeralso 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 withX_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_trainas part of model. Supplied prepare; its definition below specifies the values and role. Fitted onX_train,y_train; reuse without refitting. model- Already fitted on
X_train,y_train. Fitted estimator or pipeline for the requested model family. Fitted onX_train,y_train; reuse without refitting.
Outputs you create
answer- The requested result, with the values, labels and row order described by this task.
distance | weight | service | weekend | duration |
|---|---|---|---|---|
| 15.7052 | 6.75035 | standard | 0 | 61.7704 |
| 9.33869 | 4.81674 | express | 1 | 35.1694 |
| 17.3134 | 5.73931 | economy | 0 | 59.0766 |
| 14.25 | 7.69699 | standard | 1 | 56.5785 |
| 2.78937 | 6.42024 | express | 0 | 19.5577 |
| 19.5368 | 5.62508 | economy | 1 | 77.0289 |
| 15.4617 | 5.68023 | standard | 0 | 56.8109 |
| 15.9352 | 3.17871 | express | 1 | 52.08 |
- Identify the omitted
servicereference from the fittedencoder.
answerHints
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.