Start learning free

LESSON 51 OF 165 · MACHINE LEARNING: MODELS

Words into Numbers: One-Hot in Python

Practice

A model only does arithmetic, so every column you give it must hold numbers. The shop's city holds words like Rome and Madrid, and newsletter holds yes and no. Fit a model on them as they are and scikit-learn stops: could not convert string to float: 'Rome'.

Numbering the cities 1, 2, 3 doesn't work: the model would read Lyon as three times Berlin. One-hot encoding gives each city its own column instead, with 1 where the customer lives there and 0 elsewhere. pd.get_dummies(table, columns=[...], dtype=int) gives back a new table where each listed column is replaced by one column per value, named like city_Rome. dtype=int writes 1 and 0 instead of True and False:

sizes = pd.DataFrame({"size": ["S", "L", "S"]})
print(pd.get_dummies(sizes, columns=["size"], dtype=int).to_string())
#    size_L  size_S
# 0       0       1
# 1       1       0
# 2       0       1

Clean first: every different spelling gets its own column, so " cluj " and "Cluj" would be two cities. .str.strip() gives back the text without spaces at both ends, .str.title() gives it back with only the first letter capital ("BERLIN" becomes "Berlin"), and .fillna("Unknown") puts "Unknown" in the 9 blank cells, so those customers get a column too. A yes/no column needs just one column of 1s and 0s: .map(a_dict) gives back the column with each value swapped for its value in the dict.

Your program must print this. The last two lines come from the lesson's check, which runs after your code:

     city newsletter  visits
0    Rome        yes      11
1  Madrid        yes       7
2  Madrid         no       5
['newsletter', 'visits', 'city_Berlin', 'city_Cluj', 'city_Lyon', 'city_Madrid', 'city_Porto', 'city_Rome', 'city_Unknown']
   newsletter  visits  city_Berlin  city_Cluj  city_Lyon  city_Madrid  city_Porto  city_Rome  city_Unknown
0           1      11            0          0          0            0           0          1             0
1           1       7            0          0          0            1           0          0             0
2           0       5            0          0          0            1           0          0             0
City columns: 7 - customers with exactly one of them at 1: 300
A model can learn from X: all 9 columns are numbers

Your task: X with city, newsletter and visits is ready, and its first 3 rows are printed. Clean city, turn newsletter into 1 and 0, one-hot city with pd.get_dummies, then print the column names as a list and the first 3 rows again.


Assignment
Follow the Coding Tutorial to turn the city and newsletter words into number columns a model can learn from.


Hint
Store every result back: the cleaned column into X["city"], the 1/0 column into X["newsletter"], and the table pd.get_dummies gives back into X. None of them changes X by itself.


STEP 1 OF 5
    Ctrl + Enter to check

    Ml One Hot in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free