티스토리 뷰

2장_05_LinearSVM_2
In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

iris = load_iris()
In [2]:
from sklearn.model_selection import train_test_split

col1 = 0
col2 = 1

X = iris.data[:,[col1,col2]] # 시각화를 위해 속성 2개만 선정 (petal length & petal width)
# y = iris.target
y = iris.target.copy()
y[y==0] = 2
y[y==1] = 0
y[y==2] = 1
#y[y==2]=1, np.where(y==2,1,y), np.where(y==0,0,1)
#setosa=0 나머지는 1로 해서 setosa와 나머지로 분류

X_train, X_test, y_train, y_test = train_test_split(X, y)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
Out[2]:
((112, 2), (38, 2), (112,), (38,))
In [3]:
plt.figure(figsize=[8,6])
plt.scatter(X[:,0], X[:,1], c=y)
plt.colorbar()
Out[3]:
<matplotlib.colorbar.Colorbar at 0x21251b5c5c0>
In [4]:
from sklearn.svm import LinearSVC

model = LinearSVC(C=1)
model.fit(X_train, y_train)

score = model.score(X_test, y_test)
print(score)
0.6578947368421053
In [5]:
# y = np.where(iris.target==2, 0, 1)
In [6]:
import mglearn

plt.figure(figsize=[8,6])
mglearn.plots.plot_2d_classification(model, X_train, eps=0.5, cm='spring')
mglearn.discrete_scatter(X_train[:,0], X_train[:,1], y_train)
Out[6]:
[<matplotlib.lines.Line2D at 0x21253484f28>,
 <matplotlib.lines.Line2D at 0x21253495080>]
In [7]:
import mglearn

plt.figure(figsize=[8,6])
mglearn.plots.plot_2d_classification(model, X_train, eps=0.5, cm='spring')
mglearn.discrete_scatter(X_test[:,0], X_test[:,1], y_test)
Out[7]:
[<matplotlib.lines.Line2D at 0x2125344b7f0>,
 <matplotlib.lines.Line2D at 0x212534cbf60>]
In [14]:
from sklearn.svm import LinearSVC

model = LinearSVC(C=0.01)
model.fit(X_train, y_train)

score = model.score(X_test, y_test)
print(score)
0.6052631578947368
In [13]:
import mglearn
import matplotlib.pyplot as plt

plt.figure(figsize=[5,4])
mglearn.plots.plot_2d_classification(model, X_train, eps=0.5, cm='spring') # cm을 이용해 색 조절
mglearn.discrete_scatter(X_train[:,0], X_train[:,1], y_train)
Out[13]:
[<matplotlib.lines.Line2D at 0x21253962eb8>,
 <matplotlib.lines.Line2D at 0x21253962fd0>]
In [21]:
from sklearn.svm import LinearSVC

model = LinearSVC(C=250)
model.fit(X_train, y_train)

score = model.score(X_test, y_test)
print(score)
0.6842105263157895
In [22]:
import mglearn
import matplotlib.pyplot as plt

plt.figure(figsize=[5,4])
mglearn.plots.plot_2d_classification(model, X_train, eps=0.5, cm='spring') # cm을 이용해 색 조절
mglearn.discrete_scatter(X_train[:,0], X_train[:,1], y_train)
Out[22]:
[<matplotlib.lines.Line2D at 0x21253a4aef0>,
 <matplotlib.lines.Line2D at 0x21253a53048>]

'beginner > 파이썬 머신러닝 기초' 카테고리의 다른 글

유방암 데이터 분석  (3) 2019.03.05
지도학습 - 로지스틱회귀  (0) 2019.03.05
지도학습 - LinearSVM_1  (0) 2019.03.05
1월 지하철 승하차 인원 분석  (0) 2019.02.28
지도학습 - 선형회귀  (1) 2019.02.27
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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 31
글 보관함