250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- remove()
- choice()
- items()
- MySQL
- 파이썬
- 오버라이딩
- HTML
- fileinput
- zipfile
- __annotations__
- fnmatch
- mro()
- MySqlDB
- CSS
- decode()
- JS
- shutil
- View
- count()
- locals()
- node.js
- __getitem__
- inplace()
- discard()
- shuffle()
- __sub__
- __len__
- glob
- randrange()
- Database
Archives
- Today
- Total
흰둥이는 코드를 짤 때 짖어 (왈!왈!왈!왈!왈!왈!왈!왈!왈!왈!왈!)
(Python) 서포트 벡터 머신 본문
728x90
반응형
1. 손글씨 데이터셋 살펴보기
In [ ]:
from sklearn.datasets import load_digits
In [ ]:
digits = load_digits()
In [ ]:
digits.keys()
Out[ ]:
dict_keys(['data', 'target', 'frame', 'feature_names', 'target_names', 'images', 'DESCR'])
In [ ]:
data = digits['data']
data.shape
Out[ ]:
(1797, 64)
In [ ]:
data[0]
Out[ ]:
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.])
In [ ]:
target = digits['target']
target.shape
Out[ ]:
(1797,)
In [ ]:
import matplotlib.pyplot as plt
In [ ]:
fig, axes = plt.subplots(2, 5, figsize=(14, 8))
# flatten: 다차원을 1차원으로 바꿔주는 메소드
for i, ax in enumerate(axes.flatten()):
ax.imshow(data[i].reshape((8, 8)), cmap='gray')
ax.set_title(target[i])
In [ ]:
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: >]]
2. 정규화
In [ ]:
data[0]
Out[ ]:
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.])
In [ ]:
from sklearn.preprocessing import MinMaxScaler
In [ ]:
scaler = MinMaxScaler()
In [ ]:
scaled = scaler.fit_transform(data)
scaled[0]
Out[ ]:
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. ])
In [ ]:
from sklearn.model_selection import train_test_split
In [ ]:
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, y_test.shape)
(1437, 64) (1437,)
(360, 64) (360,)
3. Support Vector Machine(SVM)
- 두 클래스로부터 최대한 멀리 떨어져 있는 결정 경계를 찾는 분류기로 특정 조건을 만족하는 동시에 클래스를 분류하는 것을 목표로 함
In [ ]:
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
In [ ]:
model = SVC()
In [ ]:
model.fit(X_train, y_train)
SVC()
In [ ]:
y_pred = model.predict(X_test)
In [ ]:
accuracy_score(y_test, y_pred)
Out[ ]:
0.9861111111111112
In [ ]:
print(y_test[0], y_pred[0])
plt.imshow(X_test[0].reshape(8, 8))
plt.show()
5 5
In [ ]:
import matplotlib.pyplot as plt
In [ ]:
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
반응형
'파이썬 머신러닝, 딥러닝' 카테고리의 다른 글
(Python) lightGBM (0) | 2023.06.15 |
---|---|
(Python) 랜덤 포레스트 (0) | 2023.06.15 |
(Python) 로지스틱 회귀 (0) | 2023.06.14 |
(Python) 의사 결정 나무 (0) | 2023.06.14 |
(Python) 선형 회귀 (0) | 2023.06.12 |
Comments