혼동 행렬
Fashion MNIST
import tensorflow as tf
(x_train, y_train), (x_test, y_test) = \
tf.keras.datasets.fashion_mnist.load_data()
티셔츠(0)와 셔츠(6)만 추출
import numpy as np
def filter_dataset(x, y, neg_cls, pos_cls):
m = (y == pos_cls) | (y == neg_cls)
x = x[m]
y = y[m]
y = np.where(y == neg_cls, 0, 1)
return x, y
x_train2, y_train2 = filter_dataset(x_train, y_train, 0, 6)
모델 훈련
from tensorflow.keras.layers import *
model = tf.keras.Sequential([
Flatten(),
Dense(1, activation='sigmoid'),
])
model.compile(loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x_train2, y_train2)
예측
import numpy as np
x_test2, y_test2 = filter_dataset(x_test, y_test, 0, 6)
prob = model.predict(x_test2)
threshold = 0.5
y_pred = np.where(prob > threshold, 1, 0)
혼동행렬과 지표들
임포트
from sklearn.metrics import *
혼동행렬
confusion_matrix(y_test2, y_pred)
array([[935, 65], [452, 548]], dtype=int64)
정확도
accuracy_score(y_test2, y_pred)
0.7415
정밀도
precision_score(y_test2, y_pred)
0.8939641109298532
재현도
recall_score(y_test2, y_pred)
0.548
ROC 곡선
임포트
from sklearn.metrics import roc_auc_score, roc_curve
import matplotlib.pyplot as plt
시각화
fpr, tpr, threshold = roc_curve(y_test2, prob)
plt.plot(fpr, tpr)
[<matplotlib.lines.Line2D at 0x209d3e31b70>]
AUC
roc_auc_score(y_test2, prob)
0.8695984999999998