Skip to content
Minsing Log
Go back

English Edition

Supervised Learning (Classification)

Data & ML

Classification

What is Classification?

AI that derives the result of which class a given input value belongs to




Types of Models

  1. Tree-based
  1. Probability model-based
  1. Decision boundary-based
  1. Neural networks




Decision Tree model structure

What is a DecisionTree?

Impurity

What is impurity?

How to measure impurity - Gini Impurity

Gini Index A method of calculating diversity by subtracting the proportions of data belonging to each specific class within the region

Gini impurity Let’s refer to the example

Trade-off of DecisionTree depth

cf) overfitting

DTmodel = DecisionTreeClassifier(max_depth=5)
DTmodel.fit(train_X, train_Y)
# max depth를 하이퍼파라미터튜닝할때는 오버피팅이되는가를 따로 체크해봐야함.

Features of DecisionTree




Logistic Regression

What is Logistic Regression?

It is a model for solving Binary Classification problems. It uses the sigmoid (logistic) function to make the result converge between a minimum of 0 and a maximum of 1.

cf) sigmoid function

cf2) Decision Boundary

Why use Logistic Regression?

Since a general Regression algorithm cannot be used as-is for a classification problem, Linear Regression is modified so that it outputs only values between 0 and 1, representing the probability of belonging to a given class.

Features of Logistic Regression




Support Vector Machine (SVM)

Optimal Decision Boundary

What is a Support Vector?

Margin

Features of SVM




Naive Bayes Classification

cf) What is Bayes’ theorem?

Example - Spam email

Implementation

1. Calculate each one

import numpy as np

"""
1. "확인" 이라는 키워드가 등장했을 때 
    해당 메일이 스팸 메일인지 정상 메일인지
    판별하기 위한 함수를 구현합니다.
"""
def bayes_theorem():
    # 1. P(“스팸 메일”) 의 확률을 구하세요.
    p_spam = float(8/20)
    
    # 2. P(“확인” | “스팸 메일”) 의 확률을 구하세요.
    p_confirm_spam = float(5/8)
    
    # 3. P(“정상 메일”) 의 확률을 구하세요.
    p_ham = float(12/20)
    
    # 4. P(“확인” | "정상 메일" ) 의 확률을 구하세요.
    p_confirm_ham = float(2/12)
    
    # 5. P( "스팸 메일" | "확인" ) 의 확률을 구하세요.
    p_spam_confirm = float(5/7)
    
    # 6. P( "정상 메일" | "확인" ) 의 확률을 구하세요.
    p_ham_confirm = float(2/7)
    
    return p_spam_confirm, p_ham_confirm

def main():
    
    p_spam_confirm, p_ham_confirm = bayes_theorem()
    
    print("P(spam|confirm) = ",p_spam_confirm, "\nP(ham|confirm) = ",p_ham_confirm, "\n")
        
    # 두 값을 비교하여 확인 키워드가 스팸에 가까운지 정상 메일에 가까운지 확인합니다.
    value = [p_spam_confirm, p_ham_confirm]
    
    if p_spam_confirm > p_ham_confirm:
        print( round(value[0] * 100, 2), "% 의 확률로 스팸 메일에 가깝습니다.")
    else :
        print( round(value[1] * 100, 2), "% 의 확률로 일반 메일에 가깝습니다.")


if __name__ == "__main__":
    main()

2. Use the library

from sklearn.datasets import load_wine

from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score

"""
1. 데이터를 불러오고, 
   불러온 데이터를 학습용, 테스트용 데이터로 
   분리하여 반환하는 함수를 구현합니다.
   
   Step01. 사이킷런에 저장되어 있는 데이터를 
           (X, y) 형태로 불러옵니다.
   
   Step02. 불러온 데이터를 
           학습용 데이터와 테스트용 데이터로 분리합니다.
           
           학습용 데이터 : 80%, 테스트용 데이터 : 20%, 
           
           일관된 결과 확인을 위해 random_state를 
           0 으로 설정합니다.
"""
def load_data():
    
    X, y = load_wine(return_X_y = True)
    
    print("데이터 확인해보기 :\n", X[:1])
    
    train_X, test_X, train_y, test_y = train_test_split(X,y, test_size=0.20, random_state = 0)
    
    return train_X, test_X, train_y, test_y
    
"""
2. 가우시안 나이브 베이즈 모델을 불러오고,
   학습을 진행한 후 테스트 데이터에 대한 
   예측값을 반환하는 함수를 구현합니다.
   
   Step01. 가우시안 나이브 베이즈 모델을 정의합니다.
   
   Step02. 학습용 데이터에 대해 모델을 학습시킵니다.
   
   Step03. 테스트 데이터에 대한 모델 예측을 수행합니다.
"""
def Gaussian_NB(train_X, test_X, train_y, test_y):
    
    model = GaussianNB()
    
    model.fit(train_X,train_y)
    
    predicted = model.predict(test_X)
    
    return predicted
    
# 데이터 불러오기, 모델 예측 결과를 확인할 수 있는 함수입니다.
def main():
    
    train_X, test_X, train_y, test_y = load_data()
    
    predicted = Gaussian_NB(train_X, test_X, train_y, test_y)
    
    ## 모델 정확도를 통해 분류 성능을 확인해봅니다.
    print("\nModel Accuracy : ")
    print(accuracy_score(test_y, predicted))

if __name__ == "__main__":
    main()

Source of code: 2022 Military Service Members’ SW/AI Competency Enhancement Intermediate Project Course

Features




KNN (K-Nearest Neighbor)

Principle

Features

cf) knn and kmeans differ in purpose: classification and clustering, respectively. Supplementary explanation link: https://velog.io/@jhlee508/%EB%A8%B8%EC%8B%A0%EB%9F%AC%EB%8B%9D-K-%ED%8F%89%EA%B7%A0K-Means-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98





Evaluation Metrics

Types

Confusion matrix

True Positive: Predicting a value that is actually Positive as Positive (correct) True Negative: Predicting a value that is actually Negative as Negative (correct)

False Positive: Predicting a value that is actually Negative as Positive (incorrect) - Type I error False Negative: Predicting a value that is actually Positive as Negative (incorrect) - Type II error



Accuracy

- Use: How accurately did the model classify the data?



Precision & Recall


#### Precision


Recall/TPR

Sources

  1. Photo of the first decision tree structure: 2022 Military Service Members’ SW/AI Competency Enhancement Program, Classification Unit https://military22.elice.io/courses/26526/lectures/204994/materials/12
  2. Content reference: 2022 Military Service Members’ SW/AI Competency Enhancement Program, Classification Unit
  3. Confusion matrix photo: https://eunsukimme.github.io/ml/2019/10/21/Accuracy-Recall-Precision-F1-score/
  4. Accuracy formula photo:https://wooono.tistory.com/232
  5. Precision formula: https://ctkim.tistory.com/79
  6. Recall formula: http://www.gisdeveloper.co.kr/?p=8146
  7. Source of Kkojimo photo:https://pokemon.fandom.com/ko/wiki/%EA%BC%AC%EC%A7%80%EB%AA%A8_(%ED%8F%AC%EC%BC%93%EB%AA%AC)
  8. SVM model photo https://hleecaster.com/ml-svm-concept/
  9. Logistic Regression model explanation photos: 2022 Military Service Members’ SW/AI Competency Enhancement Program, Classification Unit https://military22.elice.io/courses/26528/lectures/204997/materials/13
  10. Support Vector photo: https://sjpyo.tistory.com/m/40
  11. hard/soft margin: https://ankitnitjsr13.medium.com/math-behind-svm-support-vector-machine-864e58977fdb

공개 여정 구독

글이 올라오면 이메일로 보내드릴게요.

개발, 창업, 배움, 실패와 회고를 짧고 솔직하게 기록합니다. 검색으로 스쳐가는 독자가 아니라 여정을 따라오는 팬을 만들기 위한 뉴스레터입니다.

뉴스레터는 준비 중입니다. 그동안 RSS 로 구독하거나 이메일 로 알림을 요청해 주세요.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
Overfitting & Underfitting
Next Post
Supervised Learning (Regression)