Menu
CoddyTech

One-Hot Encoding

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 53 of 64.

Instruments have no order: numbered 1, 2, 3, piano would count as more than drums. One-hot encoding gives each category its own 0/1 column instead:

Four pupils' instruments, piano, drums, piano, cello, become three columns named cello, drums and piano: each row has a single 1, in its instrument's column, and 0 in the others, while the weeks column passes through

pd.get_dummies does it, and dtype=int asks for 1 and 0 instead of True and False:

pupils = pd.DataFrame({"instrument": ["piano", "drums", "piano", "cello"],
                       "weeks": [4, 3, 6, 10]})
encoded = pd.get_dummies(pupils, columns=["instrument"], dtype=int)
print(encoded)

This prints:

   weeks  instrument_cello  instrument_drums  instrument_piano
0      4                 0                 0                 1
1      3                 0                 1                 0
2      6                 0                 0                 1
3     10                 1                 0                 0

The new columns join the end, one per value in alphabetical order. The cost is width: 500 different values would add 500 columns.

challenge icon

Challenge

Medium

A holiday site lists cabins in cabins.csv. The program reads a column name, area or heat. The starter numbers it to correlate with price, but neither column has an order: that r is made up.

  1. Replace its lines: one-hot encode the column (0 and 1).
  2. Print how many columns it became, then each new column with its count of 1s.

For five cabins and the input heat:

heat became 2 columns:
heat_gas: 3 cabins
heat_wood: 2 cabins

Try it yourself

import pandas as pd

cabins = pd.read_csv("cabins.csv")
column = input()

# Write your code below
# These lines number the areas and heating types, an order they do not have: replace them
numbers = {"coast": 1, "hills": 2, "lake": 3, "woods": 4, "gas": 1, "solar": 2, "wood": 3}
cabins[column] = cabins[column].map(numbers)
print(f"{column} against price: r = {round(cabins[column].corr(cabins['price']), 2)}")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler