Data Science/Tensorflow2.0, Pytorch

3. Tensorflow2.0 (y_train) Data Preprocess (using MNIST)

HJChung 2020. 3. 25. 19:57

1. y_train 데이터 들여다보기

지금까지 X_train데이터를 들여다 보았으니 y__train데이터를 보고자 합니다. 

y_train데이터를 본다는 것은 Label하나를 열어서 Image와 비교하여 제대로 들어갔는지, 어떤 식으로 저장 되어있는지를 확인하는 것입니다.

#1. label 하나만 뽑아보기 
label = y_train[0]

#2. label 시각화
#label을 시각화한다는건, 숫자를 시각화할 순 없으니까 train_x[0]의 title로 달아준다는 것을 말합니다. 
plt.title(y_train[0])
plt.imshow(X_train[0], 'gray')
plt.show()

2. OneHot Encoding

컴퓨터가 이해할 수 있는 형태로 변환해서 Label을 주도록 함

예를들어, 개와 고양이 같은 경우, 컴퓨터에게 개는 0이고, 고양이는 1이야 라고 해줘야 컴퓨터가 알아 들을 수 있습니다. 

 

tensorflow의 keras가 제공하는 to_categorical을 사용합니다. 

import tensorflow.keras.utils import to_categorical

to_categorical(label, num_classes = 전체 class 개수)

from tensorflow.keras.utils import to_categorical

label = y_train[0]
label_onehot = to_categorical(label, num_classes = 10)