본문 바로가기
수업(국비지원)/Python

[Python] 머신러닝 - 지도학습(분류) 2. SVM

by byeolsub 2023. 4. 26.

2. SVM

서포트 백터 : 마진과 가까이에 있는 데이터들을 의미함.


 📌

########### 6. SVM(Support Vector Machine)
# SVM(Support Vector Machine) 분류 알고리즘으로 모델 구현하기
# SVM : 공간을 (선/면)으로 분류하는 방식
from sklearn import svm    
# kernel='rbf' : 공간분리 방식을 결정하는 함수 지정
#                rbf(기본값), linear, poly(곡선)
svm_model = svm.SVC(kernel='rbf') # SVM 객체 설정
svm_model.fit(x_train, y_train) # 학습하기
y_hat = svm_model.predict(x_test) # 예측하기
svm_report = metrics.classification_report(y_test, y_hat)
svm_report
svm_matrix = metrics.confusion_matrix(y_test, y_hat)
svm_matrix
# 함수들을 이용하여 정확도, 정밀도, 재현율, f1_Score 값 출력하기
print("정확도(accuracy): %.3f" %\\
      accuracy_score(y_test, y_hat))
print("정밀도(Precision) : %.3f" %\\
      precision_score(y_test, y_hat)) 
print("재현율(Recall) : %.3f" %\\
      recall_score(y_test, y_hat))
print("F1-score : %.3f" %\\
      f1_score(y_test, y_hat))