Databricks Certified Machine Learning Professional free question 2 / 10 · Domain 1: Model Development
A StringIndexer with default settings is fit on a plan column that contains "basic" 700 times, "premium" 250 times and "enterprise" 50 times. Which indices does it assign?
- basic = 0, premium = 1, enterprise = 2
- basic = 0, enterprise = 1, premium = 2
- premium = 0, enterprise = 1, basic = 2
- enterprise = 0, premium = 1, basic = 2
Show the answer and explanation
Answer: A
Why this is correct
StringIndexer encodes a string column of labels to indices in [0, numLabels) and supports four ordering options: frequencyDesc, frequencyAsc, alphabetDesc and alphabetAsc. The default is frequencyDesc, which orders by descending label frequency so that the most frequent label gets 0. Here basic (700) gets 0, premium (250) gets 1 and enterprise (50) gets 2. Ties under a frequency ordering are further sorted by alphabet.
Why the other options are wrong
"basic = 0, enterprise = 1, premium = 2" … This is alphabetical order, ascending (alphabetAsc). It is one of the four supported ordering options, not the default.
"enterprise = 0, premium = 1, basic = 2" … This is ascending order by label frequency (frequencyAsc), where the least frequent label gets 0. It is not the default.
"premium = 0, enterprise = 1, basic = 2" … This is descending alphabetical order (alphabetDesc). It is not the default.
Default ordering is by frequency
Choose another of the four ordering options when the indices must follow alphabetical order instead.