プロメモグラム

誰が見てもわかるような文章を目指す

irisデータセットでOneClassSVM

One-Class SVMは1つのラベルでモデルを生成する。
RBFカーネルを用いることで、実行結果のように取り囲む識別面ができる。

実行結果

gamma = 0.1

f:id:zia_glass:20171008005341p:plain

gamma = 1.0

f:id:zia_glass:20171008005352p:plain

実行

import numpy as np
import scipy as sp
from sklearn import datasets
from sklearn import svm
import matplotlib.pyplot as plt 

def iris():
    iris = datasets.load_iris()
    X = np.hsplit(iris.data[0:100], [2])[0]
    y = iris.target[0:100]
    return(X, y)

X, y = iris()
colordic = ['red', 'green', 'g']
colors_map = map(lambda x: colordic[x] , y)
colors = [x for x in colors_map]


"""
gamma = 0.1
"""
#one-class svm
clf2 = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1)
clf2.fit(X[:50, :])
#scatter
plt.scatter(X[:,0], X[:,1], color=colors)
#mesh
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.05),\
     np.arange(y_min, y_max, 0.05))
# contour
Z = clf2.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.5)
plt.show()


"""
gamma = 1.0
"""
# one-class svm
clf1 = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=1.0)
clf1.fit(X[:50, :])
#scatter
plt.scatter(X[:,0], X[:,1], color=colors)
#mesh
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.05),\
     np.arange(y_min, y_max, 0.05))
# contour
Z = clf1.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.5)
plt.show()