728x90
반응형
SVM 이론 참고 사이트
머신러닝 - 2. 서포트 벡터 머신 (SVM) 개념
서포트 벡터 머신(SVM, Support Vector Machine)이란 주어진 데이터가 어느 카테고리에 속할지 판단하는 이진 선형 분류 모델입니다. (Reference1) 본 포스트는 Udacity의 SVM 챕터를 정리한 것입니다. 아래 그
bkshin.tistory.com
# 파이썬 구현
BMI 식을 이용해 무작위 자료를 작성한다.
# BMI 식을 이용해 dataset을 작성 후 SVM 분류 모델 생성
# BMI 식 = 몸무게(kg) / (키(m) * 키(m))
# BMI 식을 이용해 무작위 자료 작성
import random
def calc_func(h, w):
bmi = w / (h/100)**2
if bmi < 18.5: return 'thin'
if bmi < 24.0: return 'normal'
return 'fat'
fp = open('bmi.csv', 'w')
fp.write('height, weight, label\n') #제목
cnt = {'thin':0, 'normal':0, 'fat':0}
random.seed(12)
for i in range(50000):
h = random.randint(150, 200)
w = random.randint(35, 100)
label = calc_func(h, w)
cnt[label] += 1
fp.write('{0},{1},{2}\n'.format(h, w, label))
fp.close()
분류 모델을 불러와 정규화를 하고 모델을 실행해보자
# SVM 분류 모델을 적용
from sklearn import svm, metrics
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
tbl = pd.read_csv('bmi.csv')
label = tbl[' label']
# weight, height는 정규화
w = tbl[' weight'] / 100
h = tbl['height'] / 200
wh = pd.concat([w,h], axis=1) #자료 합치기
label = label.map({'thin':0, 'normal':1, 'fat':2}) #dummy
# test / train
data_train, data_test, label_train, label_test = train_test_split( wh, label, random_state=1 )
# model = svm.SVC(C = 1).fit(data_train, label_train)
model = svm.LinearSVC(C = 1).fit(data_train, label_train)
pred = model.predict(data_test)
print('실제값:', label_test[:10].values)
print('예측값:', pred[:10])
print(metrics.accuracy_score(label_test, pred)) #정확도
print(metrics.classification_report(label_test, pred)) #분류리포트
svc와 linearsvc 의 차이점을 참고할 사이트
https://data-scientist-brian-kim.tistory.com/72
[핸즈온 머신러닝 2/E] 5장. 서포트 벡터 머신
이번 포스팅에서는 서포트 벡터 머신(SVM)을 다뤄보겠다. 머신러닝 모델들 중, 가장 많이 사용하고 성능이 뛰어난 모델이므로 개념을 정확하게 이해해야 할 것 같다. ▶ 선형 SVM 분류 SVM 분류기(
data-scientist-brian-kim.tistory.com
시각화 !
tbl2 = pd.read_csv('bmi.csv', index_col = 2)
def scatter_func(lbl, color):
b = tbl2.loc[lbl]
plt.scatter(b[' weight'], b['height'], c = color, label=lbl)
scatter_func('fat','red')
scatter_func('normal','yellow')
scatter_func('thin','blue')
plt.legend()
plt.show()
728x90
'데이터분석 > 데이터분석' 카테고리의 다른 글
나이브 베이즈 (0) | 2022.05.16 |
---|---|
주성분 분석(PCA) (0) | 2022.05.14 |
XGboost (0) | 2022.05.12 |
Regressor (0) | 2022.05.11 |
앙상블 (Esemble Learning) (0) | 2022.05.11 |