분류 모델의 마지막 단계
이제 타이타닉 데이터셋을 사용해 분류 모델을 만들어 보겠습니다. 데이터셋은 이미 df라는 DataFrame으로 로드되어 있어요. 승객 정보를 바탕으로 누가 생존했는지를 예측할 거예요.
예측 변수는 NumPy 배열 predictors에 저장되어 있습니다. 예측할 타깃은 df.survived에 있지만, Keras에 맞게 변환해야 합니다. 예측 변수의 개수는 n_cols에 저장되어 있어요.
여기서는 'sgd' 옵티마이저를 사용합니다. 이는 Stochastic Gradient Descent의 약자예요. 자세한 내용은 다음 장에서 배웁니다!
이 연습은 강의의 일부입니다
Python으로 시작하는 Deep Learning
연습 안내
to_categorical()함수를 사용해df.survived를 범주형 변수로 변환하세요.model이라는 이름의Sequential모델을 지정하세요.- 노드가
32개인Dense레이어를 추가하세요.activation은'relu',input_shape는(n_cols,)로 지정하세요. Dense출력 레이어를 추가하세요. 결과가 두 가지이므로 유닛 수는 2여야 하며, 분류 모델이므로activation은'softmax'로 지정하세요.- 모델을 컴파일하세요.
optimizer는'sgd', 손실 함수는'categorical_crossentropy', 그리고 각 epoch가 끝날 때 정확도를 확인하려면metrics=['accuracy']를 사용하세요. predictors와target을 사용해 모델을 학습(fit)하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import necessary modules
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from tensorflow.keras.utils import to_categorical
# Convert the target to categorical: target
target = ____
# Set up the model
model = ____
# Add the first layer
____
# Add the output layer
____
# Compile the model
____
# Fit the model
____