Sklearn preprocessing LabelEncoder

hello0U0·2022년 9월 30일
0

sklearn

목록 보기
1/1

참고자료 : scikit

sklearn.preprocessing.LabelEncoder란

사용 예시

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를 가지고 데이터 전처리를 할 수 있다.

인코더 생성 LabelEncoder()

le = preprocessing.LabelEncoder()

인코딩 진행 fit(y)

  • parameters:

    	y: array로 입력한다.
  • return:

    인코더한 LabelEncoder를 반환한다.
    [1,2,2,6]과 같이 데이터를 넣으면 해당 데이터들을 가지고 인코딩를 진행한다.
le.fit([1, 2, 2, 6])
>>> LabelEncoder()

인코딩값 반환 transform(y)

  • parameters:

    	y: array로 입력한다.
  • return:

    y의 값들을 인코딩한 결과 array를 반환한다.
    [1,2,2,6]과 같이 데이터를 넣으면 해당 데이터들을 LabelEncoder에 넣어 인코딩된 값을 반환한다.
    fit()에서 1 = 0, 2 = 1, 6 =2와 같이 인코딩을 계산한다.
    transform에서는 이 정보들을 가지고 인코딩값을 반환한다.
le.transform([1, 1, 2, 6])
>>> array([0, 0, 1, 2]...)
profile
hello world

0개의 댓글