Skip to content

Latest commit

 

History

History
309 lines (236 loc) · 4.77 KB

LabelEncoder.md

File metadata and controls

309 lines (236 loc) · 4.77 KB

sklearn.preprocessing.LabelEncoder

import pandas as pd

tr = pd.read_csv("train.csv", index_col = 0)
tr.shape
(100, 6)
tr.head(3)
click adset_id campaign_id device_model device_os device_country
1b1Yz4S9wG 0 GdBSlETcLy taRA9jVfVL AsY5LC0NLu TG14pLUXCY PCCn9Q1m20
eCYeFjnExb 0 GlheP2trvZ jWRKzxzhyX nz5kFLSj4p TG14pLUXCY PCCn9Q1m20
QHcMnYqF3h 0 WGJnvetv2a DW5C3As8ij nz5kFLSj4p TG14pLUXCY PCCn9Q1m20

데이터는 철저히 암호화되어 있으며 완전히 범주형 자료로 해석해도 무방함을 알 수 있다. 따라서 이 경우에 단순한 LabelEncoding을 해주다면 메모리 용량을 줄이는 한편, 처리 속도를 높일 수 있을 것이다.

tr.agg(["nunique"])
click adset_id campaign_id device_model device_os device_country
nunique 2 57 40 26 1 1

한 번에 모든 데이터를 LabelEncoding하기

from sklearn.preprocessing import LabelEncoder
from collections import defaultdict
d = defaultdict(LabelEncoder)
d
defaultdict(sklearn.preprocessing.label.LabelEncoder, {})
# Encoding the variable
fit = tr.apply(lambda x: d[x.name].fit_transform(x))
fit.head()
click adset_id campaign_id device_model device_os device_country
1b1Yz4S9wG 0 9 35 3 0 0
eCYeFjnExb 0 12 27 20 0 0
QHcMnYqF3h 0 26 8 20 0 0
p5v9KCdjS6 1 8 32 8 0 0
aAEDD9AeIv 0 22 1 20 0 0
d
defaultdict(sklearn.preprocessing.label.LabelEncoder,
            {'click': LabelEncoder(),
             'adset_id': LabelEncoder(),
             'campaign_id': LabelEncoder(),
             'device_model': LabelEncoder(),
             'device_os': LabelEncoder(),
             'device_country': LabelEncoder()})

임의의 일부분을 원래 데이터로 다시 살펴보기 위해서

test = fit.iloc[50,:]
test = pd.DataFrame(test).T
test
click adset_id campaign_id device_model device_os device_country
MhUDSdQoqp 0 9 35 19 0 0
test.apply(lambda x: d[x.name].inverse_transform(x))
click adset_id campaign_id device_model device_os device_country
MhUDSdQoqp 0 GdBSlETcLy taRA9jVfVL fiJqINpRUy TG14pLUXCY PCCn9Q1m20