[ padding ]
padding 사용하는 이유
각 문장마다 사용하는 단어의 개수 다르기 때문에 모든 문장의 길이를 맞추기 위해 사용
padding 방법
1. Numpy로 패딩
import numpy as np
from tensorflow.keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer()
tokenizer.fit_on_texts(preprocessed_sentences) #corpas로 단어집합 생성
encoded = tokenizer.text_to_sequences(preprocessed_sentences) #corpas의 각 단어를 정수로변환
for sentence in encoded:
while len(sentence) < max_len: #각 sentence의 길이를 가장 긴 문장의 길이로 맞춤
sentence.append(0)
2. Keras의 전처리 도구 사용
from tensorflow.keras.preprocessing.sequence import pad_sequences
encoded = tokenizer.texts_to_sequences(preprocessed_sentences)
#pad_sequences 모듈 사용하여 패딩
pre_padded = pad_sequences(encoded) #패딩값을 앞쪽에 채움
post_padded = pad_sequences(encoded, padding = "post") #패딩값을 뒤쪽에 채움
[ one-hot-encoding ]
one-hot-encoding 사용 이유
숫자로 바뀐 단어들을 벡터로 만들기 위해 사용
one-hot-encoding 방법
케라스의 전처리 도구 사용
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.utils import to_categorical
text = "나랑 점심 먹으러 갈래 점심 메뉴는 햄버거 갈래 갈래 햄버거 최고야"
tokenizer = Tokenizer()
tokenizer.fit_on_texts([text]) #단어집합 생성
encoded = tokenizer.texts_to_sequence([text]) #단어를 정수로 변환
# one_hot_encoding 수행
one_hot = to_categorical(encoded)
'머신러닝 > NLP' 카테고리의 다른 글
데이터 분리 (0) | 2024.05.14 |
---|---|
Keras(케라스)의 텍스트 전처리 (0) | 2024.05.14 |
Integer Encoding (정수 인코딩) (0) | 2024.05.14 |
표제어 추출, 어간 추출 (0) | 2024.05.13 |
cleaning(정제) , normalization(정규화), Stopword(불용어) (0) | 2024.05.13 |