from IPython.display import display, Image
import warnings
warnings.filterwarnings(action='ignore')
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns
## 머신러닝 작업 flow
display(Image(filename='img/model_validation_pixel01.png'))
from sklearn.datasets import load_digits
digits = load_digits()
print(digits.data.shape)
print(digits.keys(), digits.target)
print(np.unique( digits.target ) )
sns.countplot(digits.target)
(1797, 64) dict_keys(['data', 'target', 'frame', 'feature_names', 'target_names', 'images', 'DESCR']) [0 1 2 ... 8 9 8] [0 1 2 3 4 5 6 7 8 9]
<AxesSubplot:ylabel='count'>
X = digits.data # 입력
y = digits.target == 9 # 출력
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
plt.figure(figsize=(15,6))
plt.subplot(1, 2, 1)
# y_train의 값 확인
sns.countplot(y_train)
plt.title("train")
plt.subplot(1, 2, 2)
# y_test의 값 확인
sns.countplot(y_test)
plt.title("test")
Text(0.5, 1.0, 'test')
from sklearn.dummy import DummyClassifier
dummy_model = DummyClassifier(strategy='most_frequent').fit(X_train, y_train)
pred_most_frequent = dummy_model.predict(X_test)
print("예측된 레이블의 고유값: {}".format(np.unique(pred_most_frequent)))
print("테스트 평가 정확도 : {:.2f}".format(dummy_model.score(X_test, y_test)))
예측된 레이블의 고유값: [False] 테스트 평가 정확도 : 0.90
dummy = DummyClassifier(strategy='stratified').fit(X_train, y_train)
pred_dummy = dummy.predict(X_test)
print("예측된 레이블의 고유값: {}".format(np.unique(pred_dummy)))
print("테스트 평가 정확도 : {:.2f}".format(dummy.score(X_test, y_test)))
예측된 레이블의 고유값: [False True] 테스트 평가 정확도 : 0.81
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(max_depth=2).fit(X_train, y_train)
pred_tree = tree.predict(X_test)
print("테스트 평가 정확도: {:.2f}".format(tree.score(X_test, y_test)))
테스트 평가 정확도: 0.92
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression(C=0.1).fit(X_train, y_train)
pred_logreg = logreg.predict(X_test)
print("logreg 점수: {:.2f}".format(logreg.score(X_test, y_test)))
logreg 점수: 0.98
import mglearn
mglearn.plots.plot_binary_confusion_matrix()
from sklearn.metrics import confusion_matrix
confusion = confusion_matrix(y_test, pred_logreg)
print("오차 행렬:\n{}".format(confusion))
오차 행렬: [[402 1] [ 6 41]]
mglearn.plots.plot_confusion_matrix_illustration()
print("빈도 기반 더미 모델:")
print(confusion_matrix(y_test, pred_most_frequent))
print("\n무작위 더미 모델:")
print(confusion_matrix(y_test, pred_dummy))
print("\n결정 트리:")
print(confusion_matrix(y_test, pred_tree))
print("\n로지스틱 회귀")
print(confusion_matrix(y_test, pred_logreg))
빈도 기반 더미 모델: [[403 0] [ 47 0]] 무작위 더미 모델: [[368 35] [ 43 4]] 결정 트리: [[390 13] [ 24 23]] 로지스틱 회귀 [[402 1] [ 6 41]]
## 혼동 행렬
display(Image(filename='img/model_validation01.png'))
from sklearn.metrics import f1_score
# 빈도기반 모델 f1-score
print("무작위 더미 모델의 f1 score: {:.2f}".format(f1_score(y_test, pred_most_frequent)))
# Dummy분류 f1-score
print("무작위 더미 모델의 f1 score: {:.2f}".format(f1_score(y_test, pred_dummy)))
# 의사결정트리
print("트리 모델의 f1 score: {:.2f}".format(f1_score(y_test, pred_tree)))
# 로지스틱
print("로지스틱 회귀 모델의 f1 score: {:.2f}".format(f1_score(y_test, pred_logreg)))
무작위 더미 모델의 f1 score: 0.00 무작위 더미 모델의 f1 score: 0.09 트리 모델의 f1 score: 0.55 로지스틱 회귀 모델의 f1 score: 0.92
from sklearn.metrics import classification_report
print(classification_report(y_test, pred_most_frequent,
target_names=["not 9", "is 9"]))
precision recall f1-score support not 9 0.90 1.00 0.94 403 is 9 0.00 0.00 0.00 47 accuracy 0.90 450 macro avg 0.45 0.50 0.47 450 weighted avg 0.80 0.90 0.85 450
print(classification_report(y_test, pred_dummy,
target_names=["not 9", "is 9"]))
precision recall f1-score support not 9 0.90 0.91 0.90 403 is 9 0.10 0.09 0.09 47 accuracy 0.83 450 macro avg 0.50 0.50 0.50 450 weighted avg 0.81 0.83 0.82 450
print(classification_report(y_test,
pred_tree,
target_names=["not 9", "is 9"]))
precision recall f1-score support not 9 0.94 0.97 0.95 403 is 9 0.64 0.49 0.55 47 accuracy 0.92 450 macro avg 0.79 0.73 0.75 450 weighted avg 0.91 0.92 0.91 450
print(classification_report(y_test,
pred_logreg,
target_names=["not 9", "is 9"]))
precision recall f1-score support not 9 0.99 1.00 0.99 403 is 9 0.98 0.87 0.92 47 accuracy 0.98 450 macro avg 0.98 0.93 0.96 450 weighted avg 0.98 0.98 0.98 450
교육용으로 작성된 것으로 배포 및 복제시에 사전 허가가 필요합니다.
Copyright 2022 LIM Co. all rights reserved.