#라이브러리 임포트
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
#랜덤씨드 고정
np.random.seed(5)
dataset = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/diabetes.csv')
X= dataset[['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin',
'BMI', 'DiabetesPedigreeFunction', 'Age']]
y = dataset['Outcome']
# 정규화
from sklearn import preprocessing
X = preprocessing.StandardScaler().fit(X).transform(X)
# 훈련과 테스트 비율 8:2
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state = 777)
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_shapes=True, show_layer_names=True,
rankdir='TB', expand_nested=False, dpi =100)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
history = model.fit(X_train, y_train, epochs=1500, batch_size=128)
scores = model.evaluate(X_train, y_train)
# 손실점수 값과 정확도 두개 값을 주기에 scores
print(model.metrics_names[0], scores[0])
print('%s: %.2f%%' % (model.metrics_names[1], scores[1]*100))
#학습결과 그려보기
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['acc']
# 훈련 및 검증 정확도 그리기
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()
#라이브러리 임포트
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
#랜덤씨드 고정
np.random.seed(5)
#데이터 읽어오기
dataset = pd.read_csv('/content/ThoraricSurgery.csv', header=None)
X = dataset.iloc[:,:-1] #마지막 열이 결과 값이니까 그 열 제외하고 데려와
y = dataset.iloc[:,-1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1,
random_state=7)
model = Sequential()
model.add(Dense(30, input_dim=17, activation = 'relu'))
model.add(Dense(20, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
history = model.fit(X_train, y_train, epochs=100, batch_size=64)
#학습결과 그려보기
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['acc']
# 훈련 및 검증 정확도 그리기
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_train, y_train)
print(model.metrics_names[0], scores[0])
print('%s: %.2f%%' % (model.metrics_names[1], scores[1]*100))
scores = model.evaluate(X_test, y_test)
print(model.metrics_names[0], scores[0])
print('%s: %.2f%%' % (model.metrics_names[1], scores[1]*100))
결과값
14/14 - 0s 2ms/step - loss: 0.4553 - acc: 0.8487
loss 0.45534735918045044
acc: 84.87%
2/2 - 0s 13ms/step - loss: 0.2934 - acc: 0.8936
loss 0.2933797538280487
acc: 89.36%
테스트 정확도가 더 높은 이유: 테스트 데이터 수가 작기 때문이라고 추정 가능