通过图像对核函数进行直观理解
2017-03-16 14:25
609 查看
载入库
%matplotlib inline import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np import numpy.linalg as la
几个比较著名的核函数
内积
def linear(): return lambda x, y: np.inner(x, y)
多项式
def polykernel(dimension, offset): return lambda x, y: (offset + np.inner(x, y)) ** dimension
径向基
def radial_basis(gamma=10): return lambda x, y: np.exp(-gamma*la.norm(np.subtract(x, y)))
高斯
def gaussian(sigma): return lambda x, y: \ np.exp(-np.sqrt(la.norm(x-y) ** 2 / (2 * sigma ** 2)))
正切
def hyperbolic_tangent(kappa, c): return lambda x, y: np.tanh(kappa * np.dot(x, y) + c)
通过图像来直观地展示核函数的作用
选定图像
读入图像
img=plt.imread("./example.jpg")
图像通道合并
img=img.max(axis=2)
图像显示
plt.imshow(img) plt.show()
核函数效果展示
准备画布
X = np.arange(-1.5, 1.5, 0.01) Y = np.arange(-1.5, 1.5, 0.01) X, Y = np.meshgrid(X, Y) fig = plt.figure() ax = Axes3D(fig)
初始图像
ax.plot_surface(X,Y, img)
内积核函数
数据填充
img4kernel=np.zeros((300,300)) for i,x_i in enumerate(img[:,:]): for j,x_j in enumerate(img[:,:]): img4kernel[i,j]=linear()(x_i,x_j)
转换后的图像展示
ax.plot_surface(X,Y, img4kernel)
plt.imshow(img4kernel)
多项式核函数
数据填充
img4kernel=np.zeros((300,300)) for i,x_i in enumerate(img[:,:]): for j,x_j in enumerate(img[:,:]): img4kernel[i,j]=polykernel(2,0)(x_i,x_j)
转换后的图像展示
ax.plot_surface(X,Y, img4kernel)
plt.imshow(img4kernel)
径向基核函数
数据填充
img4kernel=np.zeros((300,300)) for i,x_i in enumerate(img[:,:]): for j,x_j in enumerate(img[:,:]): img4kernel[i,j]=radial_basis()(x_i,x_j)
转换后的图像展示
ax.plot_surface(X,Y, img4kernel)
plt.imshow(img4kernel)
高斯核函数
数据填充
img4kernel=np.zeros((300,300)) for i,x_i in enumerate(img[:,:]): for j,x_j in enumerate(img[:,:]): img4kernel[i,j]=gaussian(0.5)(x_i,x_j)
转换后的图像展示
ax.plot_surface(X,Y, img4kernel)
plt.imshow(img4kernel)
正切核函数
数据填充
img4kernel=np.zeros((300,300)) for i,x_i in enumerate(img[:,:]): for j,x_j in enumerate(img[:,:]): img4kernel[i,j]=hyperbolic_tangent(2,2)(x_i,x_j)
转换后的图像展示
ax.plot_surface(X,Y, img4kernel)
plt.imshow(img4kernel)
总结
更多关于核函数的内容可以参考该github仓库。相关文章推荐
- 通过汇编角度理解虚函数
- [转] 通过汇编代码理解成员函数指针并不是指针
- 一张图像表示成NxN的矩阵,图像中每个像素是4个字节,写一个函数把图像旋转90度。 你能原地进行操作吗?(即不开辟额外的存储空间)
- 通过操作指针,与指针做函数参数'实现字串在主串中出现的次数,然后将出现的部分按照要求进行替换
- STM32通过调用库函数进行编程
- 通过函数分配内存,理解实参,形参之间的传递.
- 通过K-均值聚类算法对图像进行分割
- CDays–3 通过函数进行功能化 Python基础教程
- 通过函数调用进行选择法排序
- android---------SurfaceTexture----------通过SurfaceTexture来对预览的图像进行处理
- VC下通过直方图变换对图像进行有效增强
- Matlab函数间通过workspace进行数据共享的方法
- 通过与现有分布系统进行比较,简简单单理解“云计算”
- 使用StretchBlt函数进行图像缩放
- 针对fork()函数的深入理解!用事例family家谱来进行说明!
- VC下通过直方图变换对图像进行有效增强
- 通过 Python + Python Imaging Library (PIL) 生成简单函数图像
- 如何理解、使用Android LogCat以及通过Monkey进行压力测试
- 通过简单编译,编译成C,以及编译成C而且并行化的方式对某些Mathematica的函数进行提速
- [指针四] 通过函数进行动态内存申请---二级指针、函数返回值