티스토리 뷰
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]:
In [3]:
plt.figure(figsize=[8,6])
plt.scatter(X[:,0], X[:,1], c=y)
plt.colorbar()
Out[3]:
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)
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]:
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]:
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)
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]:
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)
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]:
'beginner > 파이썬 머신러닝 기초' 카테고리의 다른 글
유방암 데이터 분석 (3) | 2019.03.05 |
---|---|
지도학습 - 로지스틱회귀 (0) | 2019.03.05 |
지도학습 - LinearSVM_1 (0) | 2019.03.05 |
1월 지하철 승하차 인원 분석 (0) | 2019.02.28 |
지도학습 - 선형회귀 (1) | 2019.02.27 |