File:Kernel trick idea.svg
维基共享资源,媒体文件资料库
跳转到导航
跳转到搜索
此SVG文件的PNG预览的大小:800 × 343像素。 其他分辨率:320 × 137像素 | 640 × 274像素 | 1,024 × 439像素 | 1,280 × 549像素 | 2,560 × 1,097像素 | 1,344 × 576像素。
原始文件 (SVG文件,尺寸为1,344 × 576像素,文件大小:13 KB)
文件信息
结构化数据
说明
摘要[编辑]
描述Kernel trick idea.svg |
English: An illustration of kernel trick in SVM. Here the kernel is given by:
|
日期 | |
来源 | 自己的作品 |
作者 | Shiyu Ji |
Python Source Code[编辑]
import numpy as np
import matplotlib
matplotlib.use('svg')
import matplotlib.pyplot as plt
from sklearn import svm
from matplotlib import cm
# Prepare the training set.
# Suppose there is a circle with center at (0, 0) and radius 1.2.
# All the points within the circle are labeled 1.
# All the points outside the circle are labeled 0.
nSamples = 100
spanLen = 2
X = np.zeros((nSamples, 2))
y = np.zeros((nSamples, ))
for i in range(nSamples):
a, b = [np.random.uniform(-spanLen, spanLen) for _ in ['x', 'y']]
X[i][0], X[i][1] = a, b
y[i] = 1 if a*a + b*b < 1.2*1.2 else 0
# Custom kernel,
def my_kernel(A, B):
gram = np.zeros((A.shape[0], B.shape[0]))
for i in range(A.shape[0]):
for j in range(B.shape[0]):
assert A.shape[1] == B.shape[1]
L2A, L2B = 0.0, 0.0
for k in range(A.shape[1]):
gram[i, j] += A[i, k] * B[j, k]
L2A += A[i, k] * A[i, k]
L2B += B[j, k] * B[j, k]
gram[i, j] += L2A * L2B
return gram
# SVM train.
clf = svm.SVC(kernel = my_kernel)
clf.fit(X, y)
coef = clf.dual_coef_[0]
sup = clf.support_
b = clf.intercept_
x_min, x_max = -spanLen, spanLen
y_min, y_max = -spanLen, spanLen
xx, yy = np.meshgrid(np.arange(x_min, x_max, .02), np.arange(y_min, y_max, .02))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# Plot the 2D layout.
fig = plt.figure(figsize = (6, 14))
plt1 = plt.subplot(121)
plt1.set_xlim([-spanLen, spanLen])
plt1.set_ylim([-spanLen, spanLen])
plt1.set_xticks([-1, 0, 1])
plt1.set_yticks([-1, 0, 1])
plt1.pcolormesh(xx, yy, Z, cmap=cm.Paired)
y_unique = np.unique(y)
colors = cm.rainbow(np.linspace(0.0, 1.0, y_unique.size))
for this_y, color in zip(y_unique, colors):
this_Xx = [X[i][0] for i in range(len(X)) if y[i] == this_y]
this_Xy = [X[i][1] for i in range(len(X)) if y[i] == this_y]
plt1.scatter(this_Xx, this_Xy, c=color, alpha=0.5)
# Process the training data into 3D by applying the kernel mapping:
# phi(x, y) = (x, y, x*x + y*y).
X3d = np.ndarray((X.shape[0], 3))
for i in range(X.shape[0]):
a, b = X[i][0], X[i][1]
X3d[i, 0], X3d[i, 1], X3d[i, 2] = [a, b, a*a + b*b]
# Plot the 3D layout after applying the kernel mapping.
from mpl_toolkits.mplot3d import Axes3D
plt2 = plt.subplot(122, projection="3d")
plt2.set_xlim([-spanLen, spanLen])
plt2.set_ylim([-spanLen, spanLen])
plt2.set_xticks([-1, 0, 1])
plt2.set_yticks([-1, 0, 1])
plt2.set_zticks([0, 2, 4])
for this_y, color in zip(y_unique, colors):
this_Xx = [X3d[i, 0] for i in range(len(X3d)) if y[i] == this_y]
this_Xy = [X3d[i, 1] for i in range(len(X3d)) if y[i] == this_y]
this_Xz = [X3d[i, 2] for i in range(len(X3d)) if y[i] == this_y]
plt2.scatter(this_Xx, this_Xy, this_Xz, c=color, alpha=0.5)
# Plot the 3D boundary.
def onBoundary(x, y, z, X3d, coef, sup, b):
err = 0.0
n = len(coef)
for i in range(n):
err += coef[i] * (x*X3d[sup[i], 0] + y*X3d[sup[i], 1] + z*X3d[sup[i], 2])
err += b
if abs(err) < .1:
return True
return False
Xr = np.arange(x_min, x_max, .02)
Yr = np.arange(y_min, y_max, .02)
Z = np.zeros(Z.shape)
for i in range(Xr.shape[0]):
x = Xr[i]
for j in range(Yr.shape[0]):
y = Yr[j]
for z in np.arange(0, 2, .02):
if onBoundary(x, y, z, X3d, coef, sup, b):
Z[i, j] = z
break
plt2.plot_surface(xx, yy, Z, cmap='summer', alpha=0.2)
plt.savefig("kernel_trick_idea.svg", format = "svg")
许可协议[编辑]
我,本作品著作权人,特此采用以下许可协议发表本作品:
本文件采用知识共享署名-相同方式共享 4.0 国际许可协议授权。
- 您可以自由地:
- 共享 – 复制、发行并传播本作品
- 修改 – 改编作品
- 惟须遵守下列条件:
- 署名 – 您必须对作品进行署名,提供授权条款的链接,并说明是否对原始内容进行了更改。您可以用任何合理的方式来署名,但不得以任何方式表明许可人认可您或您的使用。
- 相同方式共享 – 如果您再混合、转换或者基于本作品进行创作,您必须以与原先许可协议相同或相兼容的许可协议分发您贡献的作品。
文件历史
点击某个日期/时间查看对应时刻的文件。
日期/时间 | 缩略图 | 大小 | 用户 | 备注 | |
---|---|---|---|---|---|
当前 | 2020年7月17日 (五) 14:41 | 1,344 × 576(13 KB) | SemperVinco(留言 | 贡献) | Optimized svg code | |
2017年6月28日 (三) 06:08 | 1,260 × 540(8.06 MB) | Shiyu Ji(留言 | 贡献) | Reverted to version as of 05:28, 28 June 2017 (UTC) | ||
2017年6月28日 (三) 06:05 | 540 × 1,260(7.33 MB) | Shiyu Ji(留言 | 贡献) | vertical for better display | ||
2017年6月28日 (三) 05:28 | 1,260 × 540(8.06 MB) | Shiyu Ji(留言 | 贡献) | User created page with UploadWizard |
您不可以覆盖此文件。
文件用途
以下页面使用本文件:
全域文件用途
以下其他wiki使用此文件:
- ca.wikipedia.org上的用途
- de.wikipedia.org上的用途
- en.wikipedia.org上的用途
- es.wikipedia.org上的用途
- fa.wikipedia.org上的用途
- fr.wikipedia.org上的用途
- it.wikipedia.org上的用途
- ja.wikipedia.org上的用途
- ru.wikipedia.org上的用途
- sr.wikipedia.org上的用途
- uk.wikipedia.org上的用途
- zh.wikipedia.org上的用途
元数据
此文件含有额外信息,这些信息可能是创建或数字化该文件时使用的数码相机或扫描仪所添加的。如果文件已从其原始状态修改,某些详细信息可能无法完全反映修改后的文件。
宽度 | 1344 |
---|---|
高度 | 576 |