CertSafari
    Databricks Certified Machine Learning Associate· Lessons

    Domain 2 · Lesson 25/48

    One-hot encoding categorical features with OneHotEncoder

    Use one-hot encoding for categorical features

    17 min read
    2.11% of exam
    4 sources
    Published 22 Oct 2026
    Docs as of 30 Sep 2026

    What you will be able to do

    • Explain why categorical values are one-hot encoded rather than mapped to integers, and read the binary columns OneHotEncoder produces
    • Fit OneHotEncoder inside a ColumnTransformer and Pipeline so its categories come only from training data
    • Configure handle_unknown so unseen categories at inference produce all-zero columns instead of an error
    • Use min_frequency and max_categories to group rare categories into one infrequent column
    • Use drop='first' or drop='if_binary' to remove the perfectly collinear column, and recognise how drop interacts with handle_unknown

    Key concept

    Fitted category list — OneHotEncoder learns a fixed list of categories per feature when it is fitted, and emits one binary column per learned category. Every later transform is measured against that list, so the training data decides both the output width and which values count as unknown.

    1.From categories to binary columns

    A categorical feature has to become numbers before most estimators can use it, and the obvious way to do that is the wrong one for most models. OrdinalEncoder maps each category to an integer from 0 to n_categories - 1, so uses Firefox, uses Chrome and uses Safari might become 1, 2 and 3. Estimators that expect continuous input then read those integers as a ranking: Safari counts as "more" than Chrome. For browsers, cities or device types, that order is an accident of how the list was sorted.

    One-hot encoding avoids the false order. OneHotEncoder turns a feature with n categories into n binary columns. Each row has a 1 in the column for its own category and 0 in all the others. No category sits numerically above another, so a linear model learns a separate weight for each one.

    Fit on two rows, then transform. Columns run female, male | from Europe, from US | uses Firefox, uses Safaripython
    >>> enc = preprocessing.OneHotEncoder()
    >>> X = [['male', 'from US', 'uses Safari'], ['female', 'from Europe', 'uses Firefox']]
    >>> enc.fit(X)
    OneHotEncoder()
    >>> enc.transform([['female', 'from US', 'uses Safari'],
    ...                ['male', 'from Europe', 'uses Safari']]).toarray()
    array([[1., 0., 0., 1., 0., 1.],
           [0., 1., 1., 0., 0., 1.]])

    Three details in that output matter when you use the encoder for real. First, fit is the step that decides the columns. The encoder infers each feature's categories from the data and stores them in categories_, sorted, which is why female comes before male. If you already know the full vocabulary, you can pass it with the categories parameter. A browser list of four values then produces four columns even when the training rows only contain two of them.

    Second, transform returns a sparse matrix by default. Most of each row is zeros, and the sparse format stores only the ones. That is why the example calls .toarray() to print it. To get a dense NumPy array straight from transform, construct the encoder with sparse_output=False. Third, missing values do not need to be imputed first. OneHotEncoder treats np.nan and None as categories of their own, each with its own column, and keeps the two apart when a feature contains both.

    Checkpoint 1 of 9· Check yourself

    A team encodes a city feature for a linear regression with OrdinalEncoder, which turns London, Paris and Sallisaw into 0, 1 and 2. What goes wrong, and what does switching to OneHotEncoder change?

    Checkpoint 2 of 9· Fill the gap

    By default transform returns a sparse matrix. Which method call turns the result into the dense array printed below?

    >>> enc = preprocessing.OneHotEncoder()
    >>> X = [['male', 'from US', 'uses Safari'], ['female', 'from Europe', 'uses Firefox']]
    >>> enc.fit(X)
    OneHotEncoder()
    >>> enc.transform([['female', 'from US', 'uses Safari'],
    ...                ['male', 'from Europe', 'uses Safari']]). ? ()
    array([[1., 0., 0., 1., 0., 1.],
           [0., 1., 1., 0., 0., 1.]])

    Sources123

    2.Fitting the encoder on training data inside a pipeline

    Real tables mix numeric and categorical columns, and OneHotEncoder should only touch the categorical ones. ColumnTransformer routes each column to its own transformer. In the example below, city is one-hot encoded and title goes to a bag-of-words vectorizer. Setting dtype='int' gives 0/1 integers instead of floats. remainder decides what happens to columns no transformer claimed: 'drop' discards them and 'passthrough' appends them unchanged.

    OneHotEncoder applied to the city column only, inside a ColumnTransformerpython
    >>> column_trans = ColumnTransformer(
    ...     [('categories', OneHotEncoder(dtype='int'), ['city']),
    ...      ('title_bow', CountVectorizer(), 'title')],
    ...     remainder='drop', verbose_feature_names_out=False)

    Look at the brackets. city is passed as ['city'] and title as 'title'. OneHotEncoder expects 2D input, so its column has to be given as a list of strings. CountVectorizer expects 1D input and takes a bare string. If you pass 'city' without the list, the encoder receives the wrong shape. After fitting, get_feature_names_out() names the new columns after the source column and category, such as city_London and city_Paris. Those names make a coefficient table readable.

    Where you fit the encoder matters as much as how you configure it. Preprocessing should learn its statistics from training data only, as covered in Fit preprocessing on training data only (leakage-safe Pipeline). For this encoder, the learned statistic is the category list. Under k-fold cross-validation, put the ColumnTransformer inside a Pipeline and pass the pipeline to the cross-validation routine. The encoder is then refitted on each fold's training split, and the validation rows never add a category. If you encode the full matrix before splitting, every fold sees categories that exist only in its held-out rows. The same thing happens if you fit one encoder on all the training data and reuse its categories_ in every fold. Either way, the validation scores come out higher than they should.

    Checkpoint 3 of 9· Check yourself

    You are about to run 5-fold cross-validation on a logistic regression that uses a categorical city column. Which setup gives validation scores that are not inflated by the encoding step?

    Checkpoint 4 of 9· Exam question

    A team is running k-fold cross-validation on a dataset with both numeric and categorical columns. To avoid inflating their validation scores, they need the OneHotEncoder's learned categories to come only from each fold's training split, never from data outside it. How should they structure the code?

    Sources42

    3.Unseen and infrequent categories

    Fitting on training data has a direct consequence at inference: production data can contain a value the encoder never saw. Suppose a device_type encoder was fitted on phone, tablet and desktop, and a batch arrives containing wearable. The encoder has no column for it. Unless you have configured it otherwise, transforming that row is an error, and the scoring job fails.

    handle_unknown controls what happens instead. With handle_unknown='ignore', an unseen category is encoded as all zeros for that feature. The row still has the same width the model was trained on, and it scores without raising an error. With handle_unknown='infrequent_if_exist', an unseen category goes into the infrequent-category column if infrequent grouping is enabled, and is all zeros otherwise. This option exists only for one-hot encoding.

    Unseen values 'from Asia' and 'uses Chrome' become all zeros instead of raising an errorpython
    >>> enc = preprocessing.OneHotEncoder(handle_unknown='infrequent_if_exist')
    >>> X = [['male', 'from US', 'uses Safari'], ['female', 'from Europe', 'uses Firefox']]
    >>> enc.fit(X)
    OneHotEncoder(handle_unknown='infrequent_if_exist')
    >>> enc.transform([['female', 'from Asia', 'uses Chrome']]).toarray()
    array([[1., 0., 0., 0., 0., 0.]])

    Some workarounds look reasonable but are wrong. Refitting the encoder on each production batch changes its category list. The output columns then no longer line up with the ones the model learned weights for, so a fresh fit can silently shift columns or change their number. Casting the column to numbers turns an unordered category into a ranking, which is the problem one-hot encoding solves. Catching the error and dropping the rows means you never score exactly the data you most need to watch. Make the fitted encoder tolerant instead.

    Rare categories cause a related problem. High-cardinality features are discussed in When one-hot encoding is and is not appropriate. If you do one-hot encode one, two parameters keep its width under control by merging rare categories into a single infrequent output column. Grouping also gives handle_unknown='infrequent_if_exist' a column to send unseen values to.

    OneHotEncoder parameters for grouping infrequent categories
    ParameterAccepted valuesEffect
    min_frequency (integer)1 or more; default 1Categories that appear fewer times than this count are infrequent. The default of 1 encodes every category separately.
    min_frequency (float)Between 0.0 and 1.0, exclusiveCategories that appear in less than this fraction of all samples are infrequent.
    max_categoriesNone or an integer greater than 1Caps the number of output columns per input feature. The cap includes the column that combines infrequent categories.

    Checkpoint 5 of 9· Check yourself

    Last night's scoring job crashed: a batch contained device_type 'wearable', and the encoder was fitted on phone, tablet and desktop only. Which fix keeps the job running without breaking the model's inputs?

    Checkpoint 6 of 9· Match them up

    Match each OneHotEncoder setting to how it treats rare or unseen categories of a feature

    Tap a term, then the definition that fits it.

    Checkpoint 7 of 9· Exam question

    A model is trained with an OneHotEncoder fit on a device_type column that contained phone, tablet, and desktop during training. In production, an inference batch arrives containing a new value, wearable, that the encoder never saw during fitting. The team wants the pipeline to score these rows without crashing. What should they configure on the fitted encoder?

    Sources13

    4.Dropping a column to avoid collinearity

    A full one-hot encoding contains redundant information. Every row has exactly one 1 among a feature's n columns, so those columns always sum to 1. If the model also fits an intercept, which acts as a column of 1s, the encoded columns add up exactly to that intercept column. That is perfect co-linearity. For non-regularized regression such as LinearRegression, it makes the covariance matrix non-invertible. The model then fails or warns about a singular design matrix. Lowering regularization does not fix this, and neither does changing handle_unknown. Switching to a single integer column brings back the false ordering.

    The fix is the drop parameter, which encodes each feature into n - 1 columns. With drop='first', the first category of each feature loses its column. That category is then represented by all zeros, and the model's intercept absorbs it. With three regions (north, south, east), this leaves two columns, and the dependency on the intercept is gone. drop='if_binary' drops a column only for features with exactly two categories. It suits a mix such as a two-valued gender column alongside multi-valued ones.

    drop='first' leaves one column per two-category feature; the dropped categories encode as zerospython
    >>> drop_enc = preprocessing.OneHotEncoder(drop='first').fit(X)
    >>> drop_enc.categories_
    [array(['female', 'male'], dtype=object), array(['from Europe', 'from US'], dtype=object),
     array(['uses Firefox', 'uses Safari'], dtype=object)]
    >>> drop_enc.transform(X).toarray()
    array([[1., 1., 1.],
           [0., 0., 0.]])

    These two settings interact. When handle_unknown='ignore' is combined with drop, an unseen value and the dropped category produce the same all-zero row. A new wearable device would then be scored exactly like whichever device type was dropped. inverse_transform shows the same thing: when a category was dropped, it maps an all-zero row back to that category, and when nothing was dropped it returns None. Before combining the two, decide whether "unknown" and "reference category" can share an encoding for your data.

    Use drop when you build the encoding yourself and the model fits an unregularized linear combination with an intercept, the case where co-linearity breaks the solve. SparkR's GLM sits outside this choice: it one-hot encodes categorical features itself, so there is no encoder for you to configure and no drop setting to pick. When the model does not need drop, keep all n columns. You then get one column per category, and an unknown value stays distinguishable from every known one.

    Checkpoint 8 of 9· Fill the gap

    Fitted on the same two rows as before, this encoder outputs 3 columns instead of 6, and the second row comes out as all zeros. Which parameter produces that result?

    >>> drop_enc = preprocessing.OneHotEncoder( ? ='first').fit(X)
    >>> drop_enc.categories_
    [array(['female', 'male'], dtype=object), array(['from Europe', 'from US'], dtype=object),
     array(['uses Firefox', 'uses Safari'], dtype=object)]
    >>> drop_enc.transform(X).toarray()
    array([[1., 1., 1.],
           [0., 0., 0.]])

    Checkpoint 9 of 9· Exam question

    An analyst one-hot encodes a region column with three levels (north, south, east) using OneHotEncoder's default settings, producing three binary columns, and then fits a logistic regression that includes an intercept term. The model raises a warning about a singular or near-singular design matrix. What is the most likely cause and fix?

    Sources13

    Sources

    (4)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “would interpret the categories as being ordered, which is often not desired”
      ↩︎ From categories to binary columns
      “transforms each categorical feature with n_categories possible values into n_categories binary features, with one of them 1, and all others 0.”
      ↩︎ From categories to binary columns
      “By default, the values each feature can take is inferred automatically from the dataset and can be found in the categories_ attribute”
      ↩︎ From categories to binary columns
      “OneHotEncoder supports categorical features with missing values by considering the missing values as an additional category”
      ↩︎ From categories to binary columns
      “If a feature contains both np.nan and None, they will be considered separate categories”
      ↩︎ From categories to binary columns
      “the resulting one-hot encoded columns for this feature will be all zeros or considered as an infrequent category if enabled”
      ↩︎ Unseen and infrequent categories
      “If min_frequency is a float, categories with a cardinality smaller than this fraction of the total number of samples will be considered infrequent.”
      ↩︎ Unseen and infrequent categories
      “The default value is 1, which means every category is encoded separately.”
      ↩︎ Unseen and infrequent categories
      “Such functionality is useful, for example, when using non-regularized regression (LinearRegression), since co-linearity would cause the covariance matrix to be non-invertible”
      ↩︎ Dropping a column to avoid collinearity
      “This parameter allows the user to specify a category for each feature to be dropped.”
      ↩︎ Dropping a column to avoid collinearity
      “In this case, you can set the parameter drop='if_binary'.”
      ↩︎ Dropping a column to avoid collinearity
      “This means that unknown categories will have the same mapping as the dropped category.”
      ↩︎ Dropping a column to avoid collinearity
      “By default, the values each feature can take is inferred automatically from the dataset and can be found in the categories_ attribute”
      ↩︎ Key concept
      “the resulting one-hot encoded columns for this feature will be all zeros or considered as an infrequent category if enabled”
      ↩︎ Exam trap 1
      “When handle_unknown='ignore' and drop is not None, unknown categories will be encoded as all zeros”
      ↩︎ Exam trap 2
      “max_categories includes the feature that combines infrequent categories.”
      ↩︎ Exam trap 4
      “would interpret the categories as being ordered, which is often not desired (i.e. the set of browsers was ordered arbitrarily)”
      ↩︎ Checkpoint
      “This parameter sets an upper limit to the number of output features for each input feature. max_categories includes the feature that combines infrequent categories.”
      ↩︎ Checkpoint
    2. 2.
      “Automatic feature generation processing, like one-hot encoding for categorical features, also occurs during this stage.”
      ↩︎ From categories to binary columns
      “Validate and prepare the input table by imputing missing values and splitting data into training, validation, and test sets.”
      ↩︎ Fitting the encoder on training data inside a pipeline
    3. 3.
      “SparkR automatically performs one-hot encoding of categorical features so that it does not need to be done manually.”
      ↩︎ From categories to binary columns
      “SparkR automatically performs one-hot encoding of categorical features so that it does not need to be done manually.”
      ↩︎ Unseen and infrequent categories
      “SparkR automatically performs one-hot encoding of categorical features so that it does not need to be done manually.”
      ↩︎ Dropping a column to avoid collinearity
    4. 4.
      “OneHotEncoder as most of other transformers expects 2D data, therefore in that case you need to specify the column as a list of strings”
      ↩︎ Fitting the encoder on training data inside a pipeline
      “within a Pipeline that is safe from data leakage and that can be parametrized”
      ↩︎ Fitting the encoder on training data inside a pipeline
      “Incorporating statistics from test data into the preprocessors makes cross-validation scores unreliable (known as data leakage)”
      ↩︎ Exam trap 3
      “Incorporating statistics from test data into the preprocessors makes cross-validation scores unreliable (known as data leakage)”
      ↩︎ Checkpoint

    Ready to test yourself?

    Practise the 7 questions on this subdomain.

    Spotted a mistake, or was something unclear? Tell us.