One-hot encoding

Python'da Kategorik Verilerle Çalışma

Kasey Jones

Research Data Scientist

Neden sadece etiket kodlama değil?

used_cars["engine_fuel"] = used_cars["engine_fuel"].astype("category")
codes = used_cars["engine_fuel"].cat.codes
categories = used_cars["engine_fuel"]
dict(zip(codes, categories))
{3: 'gasoline',
 2: 'gas',
 0: 'diesel',
 5: 'hybrid-petrol',
 4: 'hybrid-diesel',
 1: 'electric'}
Python'da Kategorik Verilerle Çalışma

pandas ile one-hot encoding

pd.get_dummies()

  • data: bir pandas DataFrame'i
  • columns: sütun adlarından oluşan liste-benzeri bir nesne
  • prefix: her kategori başına eklenecek bir dize
Python'da Kategorik Verilerle Çalışma

Bir DataFrame üzerinde one-hot encoding

used_cars[["odometer_value", "color"]].head()

Örnek çıktı:

   odometer_value   color
0          190000  silver
1          290000    blue
2          402000     red
3           10000    blue
4          280000   black
...
Python'da Kategorik Verilerle Çalışma

DataFrame üzerinde one-hot encoding (devam)

used_cars_onehot = pd.get_dummies(used_cars[["odometer_value", "color"]])

used_cars_onehot.head()
   odometer_value  color_black  color_brown  color_green ...
0          190000            0            0            0 ...
1          290000            0            0            0 ...
2          402000            0            0            0 ...
3           10000            0            0            0 ...
4          280000            1            0            0 ...
print(used_cars_onehot.shape)
(38531, 13)
Python'da Kategorik Verilerle Çalışma

Kullanılacak sütunları belirtme

used_cars_onehot = pd.get_dummies(used_cars, columns=["color"], prefix="")
used_cars_onehot.head()
      manufacturer_name ...  _black  _blue  _brown
0                Subaru ...       0      0       0
1                Subaru ...       0      1       0
2                Subaru ...       0      0       0
3                Subaru ...       0      1       0
4                Subaru ...       1      0       0
print(used_cars_onehot.shape)
(38531, 41)
Python'da Kategorik Verilerle Çalışma

Kısa notlar

  • Çok fazla özellik oluşturabilir
used_cars_onehot = pd.get_dummies(used_cars)
print(used_cars_onehot.shape)
(38531, 1240)
  • NaN değerlerinin kendi sütunu olmaz
Python'da Kategorik Verilerle Çalışma

One-hot encoding alıştırması

Python'da Kategorik Verilerle Çalışma

Preparing Video For Download...