+91 81900 98289

Categorical Data

This Categorical Data lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

Models that expect numbers cannot take a city name as text. One-hot encoding makes a column for each category, with 1 in the matching column.

Drop one of the category columns when the columns would otherwise add up to 1 on every row.

Example 1: Start here

Keep the same column order when you encode a new row later.

cities = ["Chennai", "Madurai", "Chennai"] # a list; the values stay in this order names = sorted(set(cities)) # a new list, sorted rows = [[1 if city == name else 0 for name in names] for city in cities] # a list; the values stay in this order print(names) print(rows)

Example 2: Add two rows

Categorical Data applies one operation to every number. zip pairs the rows, and the list keeps each sum.

left = [1, 2, 32] # a list; the values stay in this order right = [10, 20, 30] # a list; the values stay in this order print([a + b for a, b in zip(left, right)]) # pair the lists, then combine each pair

Example 3: Average a column

The average is the total divided by how many numbers you have. That is the same summary a statistics page starts with.

scores = [70, 32, 90] # a list; the values stay in this order print(sum(scores) / len(scores)) # len counts the items

Example 4: Keep values above a line

A cutoff drops the low values. Change 80 and run the example again to see who remains.

scores = [70, 32, 90] # a list; the values stay in this order print([score for score in scores if score >= 80]) # keep the items that pass the test
Keep the same column order when you encode a new row later.
Call Now 81900 98289