Start learning free

LESSON 92 OF 165 · MACHINE LEARNING: ML INTERVIEW QUESTIONS

Write a One-Hot Encoder

Practice

An interviewer asks: "Write a one-hot encoder with fit and transform, using only numpy." A model adds and multiplies numbers, but a column like city holds words. Giving the cities the codes 0, 1 and 2 would invent an order they don't have. One-hot encoding gives each category its own column instead: a row gets a 1 in its own category's column and 0 in all the others.

fit learns the categories from the training values: np.unique(values) gives back each distinct value once, sorted. transform starts from np.zeros((rows, columns), dtype=int), which gives back a table of whole-number 0s, and puts the 1s in. The trick: comparing a numpy array with one value compares every item and gives back a True/False array. Stored in a whole-number array, True becomes 1 and False 0. Here it is on shirt sizes:

sizes = np.unique(["M", "S", "M", "L"])
print(sizes)          # ['L' 'M' 'S']
print(sizes == "M")   # [False  True False], the row [0 1 0]

A value fit never saw, like "XL", equals no category, so its row stays all 0s. That is why fit and transform are separate: every later batch is encoded with the columns learned from the training data, in the same order, and no column is ever added.

With the shop's cities, your code must print the lines below. The check then counts the 1s in each new city's row ([1 0 1]: Paris gets none), encodes the numbers [3, 1, 3, 2], and compares your encoder with scikit-learn's OneHotEncoder(handle_unknown='ignore') on 10 random lists.

Categories: ['Berlin' 'Cluj' 'Rome']
Encoded cities:
[[0 1 0]
 [1 0 0]
 [0 0 1]
 [1 0 0]
 [0 1 0]]
New cities: ['Rome', 'Paris', 'Cluj']
[[0 0 1]
 [0 0 0]
 [0 1 0]]

Your task: the cities, the class with transform and its return result, and the printing are given. Add the method fit, which stores the sorted distinct values in self.categories. Then fill transform: a table of 0s with one row per value and one column per category, and a loop over the positions that sets each row to 1 in its value's column.


Assignment
Follow the Coding Tutorial to write fit and fill transform of the one-hot encoder, one line per step.


Hint
result[i] = … sets a whole row at once. Compare self.categories with the value at position i: the only True lands in that value's own column.


STEP 1 OF 5
    Ctrl + Enter to check