> ## Content Index
> Fetch the complete content index at: https://bongholee.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# 학습시 "존재"하는 고유범주값을 고유정수값으로 치환하는 Label Encoder
- URL: https://bongholee.com/label-encoderneun-jonjaehaneun-goyubeomjugabseul-goyujeongsugabseuro-cihwanhanda/
- Published: 2024-06-10T05:23:14.000Z
- Updated: 2024-06-10T05:23:14.000Z
- Author: Bongho Lee
- Tags: Data, #Import 2026-11-22 06:32

## Background

- 가게별로 예측치를 제공할 때 가게에 대한 식별자를 어떻게 전달할 것인가에 대해서 고민한 적이 있고, Label Encoder를 사용했던 적이 있다.

## Label Encoder is.

- Label Encoder는 범주형 데이터를 수치형으로 변환하는 Encoder

## Motivation

- Categorical Data를 ML이 이해할 수 있는 형태인 Numeric으로 변경

## Pros & Cons

### Pros

- 매우 구현이 간편하다.
- 데이터셋이 크더라도 빠르게 변환할 수 있다.
- 동일한 범주형 데이터에 대해서는 일관되게 동일한 정수값을 부여한다.

### Cons

- Numeric은 크기에 따른 순서가 있으나 Categorical Data와 동일한 형태의 순서를 보장하지 않기 때문에 순서 정보가 손실될 수 있다.
- 모델의 출력이 정수값으로 표현되기 때문에, 다시 원래 범주형 값으로 해석해야 하는 추가적인 작업이 필요하다.
- 다중 클래스 문제에서 각 클래스에 숫자를 할당하므로, 이러한 숫자가 모델에 잘못된 중요도를 부여할 수 있습니다. 모델은 숫자에 우리가 생각하지 않은 의미를 부여할 수 있다.
- **학습당시와 다른 Categorical Data가 들어올 경우 Encoded Value가 잘못 매칭되어서 전체적으로 데이터가 흐트러질 수 있다. Cold Start에 대한 별도의 대응이 필요할 수 있다.**

## Alternatives

- One Hot Encoding: 이진 벡터 형태로 변환하여 각 열에 따라 구분함
- Ordinal Encoding: Categorical Data에 순서가 있는 경우 순서에 맞춰 정렬 후 수치형으로 변환
- Target Encoding: Target Variable과의 관계를 고려하여 Encoding

## Sample

```python
from sklearn.preprocessing import LabelEncoder  
  
# 데이터 생성  
fruits = ['사과', '바나나', '체리', '바나나', '체리', '사과']  
  
# LabelEncoder 인스턴스 생성  
label_encoder = LabelEncoder()  
  
# 데이터 변환  
encoded_fruits = label_encoder.fit_transform(fruits)  
  
# 결과 출력  
print("원본 데이터:", fruits)  
print("변환된 데이터:", encoded_fruits)

```

![](https://i.imgur.com/Za33HCV.png)