[ 머신러닝 ] 서포트 벡터 머신

2023. 9. 6. 04:04머신러닝

728x90

🌀 손글씨 데이터셋 살펴보기

from sklearn.datasets import load_digits
digits = load_digits()
digits

🌀 결과

digits.keys()
-----------------------------------------------------------------------------------------
# 결과
dict_keys(['data', 'target', 'frame', 'feature_names', 'target_names', 'images', 'DESCR'])

data = digits['data']
data.shape
----------------------
# 결과
(1797, 64)

data[0]
------------------------------------------------------------------------
# 결과
array([ 0.,  0.,  5., 13.,  9.,  1.,  0.,  0.,  0.,  0., 13., 15., 10.,
       15.,  5.,  0.,  0.,  3., 15.,  2.,  0., 11.,  8.,  0.,  0.,  4.,
       12.,  0.,  0.,  8.,  8.,  0.,  0.,  5.,  8.,  0.,  0.,  9.,  8.,
        0.,  0.,  4., 11.,  0.,  1., 12.,  7.,  0.,  0.,  2., 14.,  5.,
       10., 12.,  0.,  0.,  0.,  0.,  6., 13., 10.,  0.,  0.,  0.])
       
target = digits['target']
target.shape
---------------------------
# 결과
(1797,)
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 5, figsize=(14, 8))
for i, ax in enumerate(axes.flatten()):
    ax.imshow(data[i].reshape((8, 8)), cmap='gray')
    ax.set_title(target[i])

🌀 결과

fig,axes = plt.subplots (2,5,figsize=(14,8))
print(fig)
print(axes)
---------------------------------------------
# 결과
Figure(1400x800)
[[<Axes: > <Axes: > <Axes: > <Axes: > <Axes: >]
 [<Axes: > <Axes: > <Axes: > <Axes: > <Axes: >]]

🌀 결과

🌀 정규화

data[0]
---------------------------------------
# 결과
array([ 0.,  0.,  5., 13.,  9.,  1.,  0.,  0.,  0.,  0., 13., 15., 10.,
       15.,  5.,  0.,  0.,  3., 15.,  2.,  0., 11.,  8.,  0.,  0.,  4.,
       12.,  0.,  0.,  8.,  8.,  0.,  0.,  5.,  8.,  0.,  0.,  9.,  8.,
        0.,  0.,  4., 11.,  0.,  1., 12.,  7.,  0.,  0.,  2., 14.,  5.,
       10., 12.,  0.,  0.,  0.,  0.,  6., 13., 10.,  0.,  0.,  0.])
       
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaled= scaler.fit_transform(data)
scaled[0]
-------------------------------------------------
# 결과
array([0.        , 0.        , 0.3125    , 0.8125    , 0.5625    ,
       0.0625    , 0.        , 0.        , 0.        , 0.        ,
       0.8125    , 0.9375    , 0.625     , 0.9375    , 0.3125    ,
       0.        , 0.        , 0.1875    , 0.9375    , 0.125     ,
       0.        , 0.6875    , 0.5       , 0.        , 0.        ,
       0.26666667, 0.75      , 0.        , 0.        , 0.5       ,
       0.53333333, 0.        , 0.        , 0.35714286, 0.5       ,
       0.        , 0.        , 0.5625    , 0.57142857, 0.        ,
       0.        , 0.25      , 0.6875    , 0.        , 0.0625    ,
       0.75      , 0.4375    , 0.        , 0.        , 0.125     ,
       0.875     , 0.3125    , 0.625     , 0.75      , 0.        ,
       0.        , 0.        , 0.        , 0.375     , 0.8125    ,
       0.625     , 0.        , 0.        , 0.        ])
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(scaled, target,test_size =0.2, random_state=10)
print(x_train.shape,y_train.shape)
print(x_test.shape,x_test.shape)
------------------------------------------------------------------------------------------------
# 결과
(1437, 64) (1437,)
(360, 64) (360, 64)

🌀 Support Vector Machine(SVM)

두 클래스로부터 최대한 멀리 떨어져 있는 결정 경계를 찾는 분류기로 특정 조건을 만족하는 동시에 클래스를 분류하는 것을 목표로 함

from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
model = SVC()
model.fit(x_train,y_train)

🌀 결과

y_pred = model.predict(x_test)
accuracy_score(y_test,y_pred)
----------------------------------
# 결과
0.9861111111111112

print ( y_test[0],y_pred[0])
plt.imshow(x_test[0].reshape(8,8))
plt.show()

🌀 결과

import matplotlib.pyplot as plt
fig,axes = plt.subplots(2,5, figsize = (14, 8))

for i, ax in enumerate(axes.flatten()):
  ax.imshow(x_test[i].reshape(8,8), cmap='gray')
  ax.set_title(f'Label: {y_test[i]},PRed:{y_pred[i]}')

🌀 결과

728x90
반응형

'머신러닝' 카테고리의 다른 글

[ 머신러닝 ] lightGBM  (0) 2023.09.11
[ 머신러닝 ] 랜덤 포레스트  (0) 2023.09.11
[ 머신러닝 ] 로지스틱 회귀  (0) 2023.09.06
[ 머신러닝 ] 의사결정나무  (0) 2023.09.06
[ 머신러닝 ] 선형회귀  (0) 2023.09.06