import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_circles
from sklearn import svm
from sklearn.svm import LinearSVC
np.random.seed(0)
xx, yy = np.meshgrid(np.linspace(-1.5, 1.5, 500), np.linspace(-1.5, 1.5, 500))
X, y = make_circles(n_samples=400, factor=.3, noise=.05)
# 線形サポートベクトルマシン
linear_svc = LinearSVC(random_state=0)
# 多項式カーネルサポートベクトルマシン
poly_svc = svm.SVC(kernel='poly')
poly_svc.fit(X, y)
# plot the line, the points, and the nearest vectors to the plane
Z = poly_svc.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors='darkred')
plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors='palevioletred')
X, y = make_circles(n_samples=500, factor=.3, noise=.08)
y_poly = poly_svc.predict(X)
plt.scatter(X[y_poly==0,0],X[y_poly==0,1])
plt.scatter(X[y_poly==1,0],X[y_poly==1,1])
plt.legend([a.collections[0]],
["learnd frontier"],
loc="upper left",)
<matplotlib.legend.Legend at 0x1a202591d0>
X, y = make_circles(n_samples=500, factor=.3, noise=.08)
y_rbf = rbf_svc.predict(X)
plt.scatter(X[y_rbf==0,0],X[y_rbf==0,1])
plt.scatter(X[y_rbf==1,0],X[y_rbf==1,1])
<matplotlib.collections.PathCollection at 0x1a1d3605f8>
# plot the line, the points, and the nearest vectors to the plane
Z = linear_svc.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors='darkred')
plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors='palevioletred')
plt.legend([a.collections[0]],
["learnd frontier"],
loc="upper left",)
y_linear = linear_svc.predict(X)
plt.scatter(X[y_linear==0,0],X[y_linear==0,1])
plt.scatter(X[y_linear==1,0],X[y_linear==1,1])
<matplotlib.collections.PathCollection at 0x1a201b6908>
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA, KernelPCA
from sklearn.datasets import make_circles
np.random.seed(0)
X, y = make_circles(n_samples=400, factor=.3, noise=.05)
kpca = KernelPCA(kernel="rbf", fit_inverse_transform=True, gamma=10)
X_kpca = kpca.fit_transform(X)
plt.subplot(2, 2, 1, aspect='equal')
plt.scatter(X[:,0],X[:,1])
plt.subplot(2, 2, 2, aspect='equal')
plt.scatter(X_kpca[y==1,0],X_kpca[y==1,1])
plt.scatter(X_kpca[y==0,0],X_kpca[y==0,1])
<matplotlib.collections.PathCollection at 0x1a1d523320>
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.decomposition import PCA, KernelPCA
from sklearn.datasets import make_circles
from sklearn.datasets.samples_generator import make_swiss_roll
np.random.seed(0)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, _ = make_swiss_roll(n_samples=1500, noise=0.05)
# 元のスイスロールを描画
#ax.scatter(X[:,0],X[:,1],X[:,2])
kpca = KernelPCA(kernel="rbf", fit_inverse_transform=True, gamma=2)
X_kpca = kpca.fit_transform(X)
ax.scatter(X_kpca[:,0],X_kpca[:,1])
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x1a22ec3470>
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x1a230fa518>