Get Your CertDatabricks Certified Machine Learning AssociatePlayCompete
‹ All Databricks Certified Machine Learning Associate questions

Databricks ML Associate Practice Question 15: String Indexer Encoder Order

Databricks Certified Machine Learning Associate · Test 2 · question 5 of 10

ML Workflowsmedium

A Spark ML pipeline ends in a LogisticRegression estimator. One input, region, is a string column with six distinct values such as "north" and "west". VectorAssembler rejects the column because it is not numeric.

Which stage sequence correctly prepares region for this model?

Select one answer
Show answer and explanation

Answer:

  • B. StringIndexer on region, then OneHotEncoder on the resulting index, then VectorAssembler

Spark ML splits categorical encoding into two stages, and the order is fixed by their input types. StringIndexer maps string labels to numeric indices, and OneHotEncoder consumes a numeric index column and emits a sparse binary vector. So strings must be indexed before they can be encoded, and VectorAssembler combines the encoded vector with the other numeric features last.

Option A inverts the two stages. OneHotEncoder cannot accept a raw string column, so the pipeline fails at the first stage.

Option C asks VectorAssembler to consume the string column, which is the error the question starts from. StringIndexer also operates on a string column, not on an assembled vector.

Option D is the subtle one and is wrong specifically because the model is linear. Feeding the raw index to LogisticRegression asserts that "north" = 0 is less than "west" = 5 and that the distances between regions are meaningful, so the model fits a single coefficient across an arbitrary ordering. Skipping the encoder is defensible for decision trees, which split on the index rather than scaling it, but not here.

Reference: Extracting, transforming and selecting features (Spark ML)

Practice Test 2 ›

Study guides for Databricks Certified Machine Learning Associate