참고자료 : scikit
사용 예시
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit([1, 2, 2, 6])
>>> LabelEncoder()
le.classes_
>>> array([1, 2, 6])
le.transform([1, 1, 2, 6])
>>> array([0, 0, 1, 2]...)
le.inverse_transform([0, 0, 1, 2])
>>> array([1, 1, 2, 6])
sklearn.preprocessing.LabelEncoder()를 사용하여 범주형 데이터를 숫자로 변환 할 수 있다.
NLP에서 모델은 수치 데이터를 가지고 학습할 수 있기 때문에, 텍스트를 수치로 바꿔줄 필요가 있는데, 이때 LabelEncoder를 가지고 데이터 전처리를 할 수 있다.
le = preprocessing.LabelEncoder()
y: array로 입력한다.
인코더한 LabelEncoder를 반환한다.
[1,2,2,6]과 같이 데이터를 넣으면 해당 데이터들을 가지고 인코딩를 진행한다. le.fit([1, 2, 2, 6])
>>> LabelEncoder()
y: array로 입력한다.
y의 값들을 인코딩한 결과 array를 반환한다.
[1,2,2,6]과 같이 데이터를 넣으면 해당 데이터들을 LabelEncoder에 넣어 인코딩된 값을 반환한다.le.transform([1, 1, 2, 6])
>>> array([0, 0, 1, 2]...)