앞에 답변해주신 답변 정말 감사합니다. 하면서 의문이 드는게 생겨서 질문드립니다.
먼저 원핫 인코딩하는 함수를 찾아서 사용하던중 100개 정도의 데이터가 있는데 5개 데이터의 정수인코딩한것을 보여드리자면 다음과 같습니다.
print(train_index[0:5][:]) # print indexed datas
[[1], [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [2, 15, 9, 6, 16, 9, 17, 13, 18, 19, 9, 12, 13],
[20, 21, 22, 23, 9, 24, 25, 26, 27, 13, 28, 29, 30, 9, 31, 9, 32, 33, 34, 35, 36, 36, 37, 22, 38, 39],
[40, 41, 42, 43, 9, 17, 13, 12, 13, 44, 45, 46, 47, 48, 49, 47]]
이를 케라스 from tensorflow.keras.utils import to_categorical를 이용해 원 핫 인코딩을 하게 되면
one_hot = to_categorical(train_index[:])
print(one_hot)
TypeError Traceback (most recent call last)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-79-163140ab8dbf> in <module>
----> 1 one_hot = to_categorical(train_index[:])
2 print(one_hot)
C:\Anaconda3\envs\venv\lib\site-packages\tensorflow\python\keras\utils\np_utils.py in to_categorical(y, num_classes)
36 A binary matrix representation of the input.
37 """
---> 38 y = np.array(y, dtype='int')
39 input_shape = y.shape
40 if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:
ValueError: setting an array element with a sequence.
다음과 같은 오류가 발생합니다
하지만 한개의 열만 실행하게 되면
one_hot = to_categorical(train_index[1])
print(one_hot)
[[0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
결과가 나오게 되서 의문입니다. 또한 앞서 답변 주신대로
len(one_hot)
13
len으로 vocab길이를 찾으면 13이 나오는데
이는 정수인코딩에서 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] 개수와 똑같아서 원 핫 인코딩을 한 차이가 잘 이해가 가지 않는데 더불어 설명해주시면 정말 감사하겠습니다.