반응형
문제 정의 : 피마족 인디언 당뇨병 발병 유무를 예측하는 이진 분류 문제 (당뇨병 1, 정상 0)
데이터 준비하기
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import tensorflow as tf
from keras.models import Sequential
from keras.layers ipmort Dense
# 랜덤 시드 고정
np.random.seed(5)
dataset = pd.read_csv('/content/diabetes/csv')
dataset.head()
dataset.info()
데이터 분리하기
dataset.columns
X = dataset[['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin',
'BMI', 'DiabetesPedigreeFunction', 'Age']]
y = dataset['Outcome']
from sklearn import preprocessiong
# 정규화하기
X = preprocessing.StandardScaler().fit(X).transform(X)
# train:test => 80:20 -> 614:154
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=777)
X_train.shape
모델 구성하기
model = Sequential()
model.add(Dense(32, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid))
model.summary()
tf.keras.utils.plot_model(model, show_shape=True, show_layer_names=True,
rankdir='TB', expand_nested=False, dpi=100)
모델 설정하기
model.compile(loss='binary_crossentropy', optimize='adam', metric=['accuracy'])
모델 학습하기
history = model.fit(X_train, y_train, epochs=100, batch_size=128)
학습 결과
import matplotlib.pyplot as plt
his_dict = history.history
loss = his_dict['loss']
epochs = range(1, len(loss)+1)
fig = plt.figure(figsize = (10, 5))
# 훈련 및 검증 손실 그리기
ax1 = fig.add_subplot(1, 2, 1)
ax1.plot(epochs, loss, color = 'orange', label = 'train_loss')
ax1.set_title('train loss')
ax1.set_xlabel('epochs')
ax1.set_ylabel('loss')
ax1.legend()
acc = his_dict['accuracy']
# 훈련 및 검증 정확도 그리기
ax2 = fig.add_subplot(1, 2, 2)
ax2.plot(epochs, acc, color = 'blue', label = 'train_accuracy')
ax2.set_title('train accuracy')
ax2.set_xlabel('epochs')
ax2.set_ylabel('accuracy')
ax2.legend()
plt.show()
모델 평가하기
scores = model.evaluate(X_test, y_test)
print('%s : %.2f%%' %(model.metrics_names[1], scores[1]*100))
print(model.metrics_names[0], scores[0])
scores = model.evaluate(X_train, y_train)
print('%s: %.2f%%' %(model.metrics_names[1], scores[1]*100))
LIST
'대외활동 > ABC 지역주도형 청년 취업역량강화 ESG 지원산업' 카테고리의 다른 글
[220928 ABC] 다층 퍼셉트론 모델 만들기 (0) | 2022.12.03 |
---|---|
[220928 ABC] 신경망과 케라스 정리 (0) | 2022.10.30 |
[220928 ABC] 케라스에서의 개발 과정 (1) | 2022.10.30 |
[220928 ABC] 신경망의 기본 개념 (1) | 2022.10.29 |
[ABC 220927] 신경망 (0) | 2022.10.24 |