빅데이터 서비스 교육/딥러닝

딥러닝 유방암 데이터 분류 예제

Manly 2022. 7. 14. 10:16
반응형
 
목표
  • 환자 데이트를 바탕으로 해당환자가 유방암인지 아닌지 분류해보자
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
 
# 학습용 유방암 데이터 로드
from sklearn.datasets import load_breast_cancer
 
data = load_breast_cancer()
data
 
data.keys()
 
dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename', 'data_module'])
X = data['data']
y = data['target']
 
X.shape, y.shape
 
((569, 30), (569,))
 
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2, random_state=5)
 
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
 
(455, 30)
(114, 30)
(455,)
(114,)

신경망 모델링

  • 1.신경망 구조 설계
  • 2.학습 및 평가방법 설정
  • 3.학습 및 시각화
  • 4.모델 평가
    1. 필요한 라이브러리 임포트
    2.  구조설계
      • 입력층, 출력층 잘 확인할 것
      • 활성화 함수는 sigmoid, relu 둘 다 써보기           
      • 학습 및 평가방법 설정에서 optimizer는 SGD랑 Adam 둘 다 써보기
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
 
model = Sequential()
 
model.add(Dense(30, input_dim=30, activation="sigmoid"))

model.add(Dense(15, activation="sigmoid"))
model.add(Dense(5, activation="sigmoid"))

model.add(Dense(1, activation="sigmoid"))

model.summary()
 
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 30)                930       
                                                                 
 dense_1 (Dense)             (None, 15)                465       
                                                                 
 dense_2 (Dense)             (None, 5)                 80        
                                                                 
 dense_3 (Dense)             (None, 1)                 6         
                                                                 
=================================================================
Total params: 1,481
Trainable params: 1,481
Non-trainable params: 0
model.compile(loss="binary_crossentropy",
              optimizer="SGD",     # Adam : 최근에 가장 많이 사용되는 일반적으로 성능이 좋은 최적화함수
              metrics=['acc'])
 
h = model.fit(X_train,y_train, epochs=100)
 
plt.figure(figsize=(15,5))

plt.plot(h.history['acc'], label='acc')

plt.legend()
plt.show()
 
model.evaluate(X_test, y_test)
4/4 [==============================] - 1s 7ms/step - loss: 0.6830 - acc: 0.5789
[0.6829911470413208, 0.5789473652839661]
model.evaluate(X_test, y_test)         # 활성화 함수 relu / 최적화함수 Adam을 썼을때의 값
4/4 [==============================] - 0s 3ms/step - loss: 0.2237 - acc: 0.8947
[0.22373051941394806, 0.8947368264198303]

 

반응형