Home [LG Aimers] 머신러닝 2. 분류 실습 II(Python)
Post
Cancel

[LG Aimers] 머신러닝 2. 분류 실습 II(Python)

LG Aimers 5기 AI 실습 교육 수강 내용을 정리합니다. 모든 자료는 교육 플랫폼 앨리스로부터 제공받았습니다.

[ Confusion Matrix를 통한 문자 인식률 확인하기 - 다중 클래스 분석 ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_digits

from sklearn.svm import SVC

from sklearn.metrics import confusion_matrix
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score
from sklearn.metrics import accuracy_score

## digits 데이터 불러오기
def load_data():
    
    X, y = load_digits(return_X_y = True)
    
    train_X, test_X, train_y, test_y = train_test_split(X, y, test_size =0.2, random_state=100)
    
    return train_X, test_X, train_y, test_y

## SVM 모델 정의
def SVM_clf(train_X, test_X, train_y):
    svm = SVC()
    
    svm.fit(train_X, train_y)
    
    pred = svm.predict(test_X)
    
    return pred

## 출력된 Confusion Matrix 를 통해 인덱스 3의 precision과 recall, 전체 데이터에 대한 accuracy 계산 함수 구현
def cal_eval(test_y, pred):
    index_3_precision = precision_score(test_y==3, pred==3, None)
    index_3_recall = recall_score(test_y==3, pred==3, None)
    accuracy = accuracy_score(test_y==3, pred==3)

    return index_3_precision, index_3_recall, accuracy

def main():
    
    train_X, test_X, train_y, test_y = load_data()
    
    pred = SVM_clf(train_X, test_X, train_y)
    
    # confusion matrix 확인
    print("Confusion matrix results :\n\t- row : real(test_y) 0 ~ 9 label\n\t- column : predicted 0 ~ 9 label\n\n%s\n"  % confusion_matrix(test_y, pred))

    index_3_precision, index_3_recall, accuracy = cal_eval(test_y, pred)
    
    print("index 3의 recall : %f" % index_3_recall)
    print("index 3의 precision : %f" % index_3_precision)
    print("전체 accuracy : %f" % accuracy)
    

<\div>

This post is licensed under CC BY 4.0 by the author.

[LG Aimers] 머신러닝 2. 분류 실습(Python)

[LG Aimers] 딥러닝 기초 3. 과적합 실습(Python)

Comments powered by Disqus.

Comments powered by Disqus.