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:
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 0The new columns join the end, one per value in alphabetical order. The cost is width: 500 different values would add 500 columns.
Challenge
MediumA 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.
- Replace its lines: one-hot encode the column (0 and 1).
- 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 cabinsTry 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)}")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler