๐ ๋จธ์ ๋ฌ๋ ๋ชจ๋ธ์ ๋ชจ๋ input๊ณผ output์ด numeric ์์นํ์ด์ด์ผ ํ๋ค๋ ์ ์ ์กฐ๊ฑด์ด ๊น๋ ค ์๋ค! ์ฆ, ์ฐ๋ฆฌ๊ฐ ๋ง์ฃผํ data๊ฐ categorical variable์ด๋ผ๋ฉด ๋ชจ๋ธ์ ์ง์ด๋ฃ๊ธฐ ์ ์ ๋ฏธ๋ฆฌ numeric ์์นํ์ผ๋ก ๋ฐ๊พธ์ด์ฃผ๋ ์์ ์ด ํ์ํ ๊ฒ์ด๋ค.
๐ต๏ธโ๏ธ ๊ทธ ์ค ๋ํ์ ์ธ ์๋ก One-Hot Encoding ๊ธฐ๋ฒ์ ๋ํด ๋ฐฐ์ ๋ค
One-Hot encoding
โซ ML ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ part์์ model์ด ์ดํดํ ์ ์๋ data๋ก ๋ณํํ๊ธฐ ์ํด ์ฌ๋ฌ encoding ๊ธฐ๋ฒ๋ค์ด ์ ์ฉ๋๋ค๊ณ ํ์๊ณ , ์ค๋์ ๊ทธ ์ค ํ๋์ธ 'One-Hot encoding' ๊ธฐ๋ฒ์ ๋ํด์ ๋ฐฐ์ฐ๋ ค๊ณ ํ๋ค intro. Machine L.
sh-avid-learner.tistory.com
- ์ด 6๊ฐ์ง data type ์ค ๋ ๊ฐ์ง ์ข ๋ฅ์ categorical data type -
→ ๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ qualitative categorical variable ์ค unordered, ์ฆ ์์๊ฐ ์ ํด์ง์ง ์์ data์ ๊ฒฝ์ฐ ์ํซ์ธ์ฝ๋ฉ ๊ธฐ๋ฒ์ด ์ ์ผ ์ ์ ํ๋ค๊ณ ๋ฐฐ์ ๋ค.
!-- ๊ทธ๋ ๋ค๋ฉด ์์๊ฐ ์๋ qualitative categorical ordered variable์? --!
→ ์์ one-hot encoding์ ํ๊ณ์ ์ผ๋ก ์ธ๊ธํ, '์์๊ฐ ์๋ data'์ ๊ฒฝ์ฐ ordinal encoding์ด๋ผ๋ ๊ธฐ๋ฒ์ ๋ฐ๋ก ์ฐ๋ฉด ๋๋ค!
> ordinal-encoding ๊ธฐ๋ฒ์ ๋ํด ์์ธํ ์์๋ณดza <
w/scikit-learn
๐จ๐ซ ordinal encoding์์๋ data์ ๋ด์ฌ๋ ์์๋๋ก ์ ์ ํฌ๊ธฐ๊ฐ ์ปค์ง๋ ์ ์๋ค์ด ํ ๋น๋๋ค. category_enocders library๋ฅผ ์ด์ฉํด์ ์์๋ฅผ ํตํด ์ดํดํด๋ณด์
category_encoders library
** OrdinalEncoder docu **
https://contrib.scikit-learn.org/category_encoders/
https://contrib.scikit-learn.org/category_encoders/ordinal.html
classcategory_encoders.ordinal.OrdinalEncoder(verbose=0, mapping=None, cols=None, drop_invariant=False, return_df=True, handle_unknown='value', handle_missing='value')
'Encodes categorical features as ordinal, in one ordered feature. Ordinal encoding uses a single column of integers to represent the classes. An optional mapping dict can be passed in; in this case, we use the knowledge that there is some true order to the classes themselves. Otherwise, the classes are assumed to have no true order and integers are selected at random.'
๐งค one-hot encoder์ ๋ง์ฐฌ๊ฐ์ง๋ก ordinal encoder๋ ์ฒซ data์๋ fit_transform ์ํ ํ ์ดํ data๋ ์ฐ์ด์ด์ transform method ์ ์ฉ
1> import
from category_encoders import OrdinalEncoder
2> OrdinalEncoder ๊ฐ์ฒด ์์ฑ
enc = OrdinalEncoder()
→ ์ ๊น. ์ฌ๊ธฐ์! OrdinalEncoder() ๋ค์ํ ์ธ์๋ค ์์๋ณด๊ธฐ ←
โ cols = default๋ก๋ encoder์ ์ง์ด๋ฃ์ ๋ชจ๋ data๋ฅผ ์ธ์ฝ๋ฉํด์ฃผ๋๋ฐ, ํน์ column๋ค๋ง encodingํ๊ณ ์ถ์ผ๋ฉด ์ํ๋ column name์ list๋ก ๋ฌถ์ด์ cols์ ํ ๋นํด์ฃผ๋ฉด ๋๋ค.
'a list of columns to encode, if None, all string columns will be encoded.'
โก return_df = encoding๋ ๊ฒฐ๊ณผ๋ฌผ์ ๋ฐํํ์ ๊ฒฐ์ ํด์ค๋ค. default๋ true๋ก encoding๋ ๊ฒฐ๊ณผ๋ฌผ์ด dataframe์ ํํ๋ก ๋์จ๋ค. false๋ก ์ค์ ํ๋ฉด numpy array๋ก ๋์จ๋ค.
'boolean for whether to return a pandas DataFrame from transform (otherwise it will be a numpy array).'
โข mapping = ๋ด์ฌ๋ ์์๊ฐ ์๋๋ผ ๋ช ์์ ์ผ๋ก ์ฌ๋ฌ data ์์๋ฅผ ์์์ ์ผ๋ก ์ ํ๋ค. ์ํ๋ column์ ๋ชจ๋ data์ ์ํ๋ ์์๋ฅผ ์ผ์ผ์ด ๋ถ์ฌ ๊ฐ๋ฅ!
'a mapping of class to label to use for the encoding, optional. the dict contains the keys ‘col’ and ‘mapping’. the value of ‘col’ should be the feature name. the value of ‘mapping’ should be a dictionary of ‘original_label’ to ‘encoded_label’. example mapping:
[{‘col’: ‘col1’, ‘mapping’: {None: 0, ‘a’: 1, ‘b’: 2}}, {‘col’: ‘col2’, ‘mapping’: {None: 0, ‘x’: 1, ‘y’: 2}}]'
โฃ handle_unknown = ๊ฒฐ์ธก์น๊ฐ ์์ ๊ฒฝ์ฐ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง ์ค์ ํ๋ ์ธ์์ด๋ค. default๋ 'value'๋ก ๊ฒฐ์ธก์น๊ฐ ์์ผ๋ฉด -1์ ๋ฆฌํดํจ
'options are ‘error’, ‘return_nan’ and ‘value’, defaults to ‘value’, which will impute the category -1.'
3> fitting
๐ ์ฌ๊ธฐ์ fit_transform์ด ์๋ fit ํจ์๋ง ์ฌ์ฉํ ๊ฒฝ์ฐ encodingํ ์์๋ฅผ ๊ฐ์ ธ์ 'ํด๋น column์ ์ด๋ฐ data๊ฐ ์ค๋ฉด ์ด๋ ๊ฒ encoding์ ํ๊ฒ ๋ค'๋ผ๊ณ ๋ง๋ค์ด์ง encoder ๊ฐ์ฒด์๊ฒ ์๋ ค์ฃผ๋ ์ฉ๋๋ก๋ง ์ฐ์.
(์ค์ ๋ก ํด๋น data๋ฅผ encodingํ ๊ฒฐ๊ณผ๊น์ง ๊ฐ์ ธ์ค๊ณ ์ถ์ผ๋ฉด ์์์ ์ธ๊ธํ fit_transform์ ์ฌ์ฉํ๋ฉด ๋๋ค.)
X = [['Male', 1, 'Yes'], ['Female', 3, 'No'], ['Female', 2, 'None']]
enc.fit(X)
4> transforming
๐ ์ค์ encodingํ๊ณ ์ถ์ ์ด์ค list, dict, ๋๋ dataframe, series ๋ฑ data๋ฅผ ๋ฃ์ผ๋ฉด encoded๋ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋๋ค!
enc.transform([['Male',1,'No'],['Female', 10]]) #๊ฒฐ์ธก์น๋ -1๋ก encoded๋จ
- encoded๋จ! -
5> (์ถ๊ฐ) category_mapping attribute
> ์ด ์์ฑ์ ์ฌ์ฉํ๋ฉด ํด๋น encoder๊ฐ ์ด๋ค column์ ์ด๋ค data๋ฅผ ๋ฌด์์ผ๋ก encodeํ๋์ง์ ์์ธ ์ ๋ณด๋ฅผ ์ ์ ์์!
enc.category_mapping
- ์์ธ ์ ๋ณด -
- ๋งค์ฐ ๊ฐ๋จํ๊ฒ. ๊ฐ๋ ์ค๋ช ๋! ๐ -
Q&A
Q. ๊ทธ๋ฌ๋ฉด ์ด๋จ ๋ One-hot์ ์ฐ๊ณ ์ด๋จ ๋ Ordinal encoding ๊ธฐ๋ฒ์ ์ ์ฉํด์ผ ํ ๊น?
A. ์ฐ๋ฆฌ๋ ์ฃผ์ด์ง data๊ฐ์ ๋ด์ฌ๋ ์์๊ฐ ์๋ ์ง๋ฅผ ๋ฐ์ ธ์ผ ํ๋ค. ์ฆ, data ๊ฐ์ ์ด๋ค data ์์ ์ข ๋ ๋ฐ์ ํ ์ฐ๊ด์ด ์๊ณ , ์ด๋ค ์์ ์ฐ๊ด ์ ๋๊ฐ ์ข ๋ํ๋ค๊ณ ์๊ฐํ๋ฉด ordinal encoding์ ์ฐ๋ ๊ฒ์ด ๋ฐ๋์งํ๋ค. ์๋ฅผ ๋ค์ด ์ํํ์ , ์นจ๋ ์ฌ์ด์ฆ, ์ํ ํ๋ฆฌํฐ ์ ์์ ๊ฐ์ data๋ ๋ถ๋ช ํ ๋๊ณ ๋ฎ์ - ์ฆ ๋ด์ฌ๋ ์์๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ordinal encoding. ๋ฐ๋๋ก ๋จ์ํ ๋์์ ๋์ด (๋์๊ฐ์ ์ง๋ฆฌ์ ๊ทผ์ ์ฑ ๊ณ ๋ ค ์ํ๋ค๋ฉด)์ด๋ ์๊น ์ข ๋ฅ์ ๋์ด๊ณผ ๊ฐ์ data๋ one-hot encoding์ด ๋ ์ ํฉํ๋ค.
(์ ์ด๋ ๊ฒ ํด์๋ ๊ตฌ๋ถ์ด ์ด๋ ต๋ค๋ฉด ํด๋น data๊ฐ ๋ฐฉํฅ์ฑ์ ๊ฐ๋ ์ง ๋จ์ํ๊ฒ ์๊ฐํ๋ค๋ฉด ๊ตฌ๋ณ์ด ๋ ์ฌ์ธ ๊ฒ์ด๋ค!)
Q. ์ค์ ์์๊ฐ ๋ด์ฌ๋ data์ธ๋ฐ ์คํ๋ ค one-hot encodingํ ๊ฒฐ๊ณผ๋ก ๋ชจ๋ธ ์ฑ๋ฅ์ด ๋ ์ข์์ก๊ฑฐ๋, ๊ทธ ๋ฐ๋์ธ ์ฌ๋ฌ case๋ค์ด ์์๊น?
A. ์ดํ ํด๋น ์คํ์ ๋ํ posting ์ฌ๋ฆด ์์
* ์ธ๋ค์ผ ์ถ์ฒ) https://www.shareicon.net/silhouettes-stand-up-standing-firm-order-row-people-700053
* ์ถ์ฒ1) https://stackoverflow.com/questions/69052776/ordinal-encoding-or-one-hot-encoding
* ์ถ์ฒ2) https://machinelearningmastery.com/one-hot-encoding-for-categorical-data/
'Machine Learning > Fundamentals' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Feature Selection vs. Feature Extraction (0) | 2022.05.18 |
---|---|
feature selection (1) - selectKBest (+jointplot) (0) | 2022.04.20 |
train vs. validation vs. test set (0) | 2022.04.18 |
Gradient Descent (concepts) (+momentum) (0) | 2022.04.18 |
One-Hot encoding (0) | 2022.04.17 |
๋๊ธ