本示例演示了在XOR数据集上进行高斯过程分类(GPC),在固定、各向同性核函数(RBF)和非固定核函数(DotProduct)之间进行比较。在XOR数据集上,由于类边界是线性的,并且与坐标轴重合,因此,DotProduct核函数获得了更好的结果。
![6b00142e9f95e9ce6134684b32ba5d15.png](https://i-blog.csdnimg.cn/blog_migrate/af0582191900a9e4d6d16d6d1addd2aa.png)
print(__doc__)
# 作者: Jan Hendrik Metzen
#
# 许可证: BSD 3 clause
import numpy as np
import matplotlib.pyplot as plt
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF, DotProduct
xx, yy = np.meshgrid(np.linspace(-3, 3, 50),
np.linspace(-3, 3, 50))
rng = np.random.RandomState(0)
X = rng.randn(200, 2)
Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)
# 拟合模型
plt.figure(figsize=(10, 5))
kernels = [1.0 * RBF(length_scale=1.0), 1.0 * DotProduct(sigma_0=1.0)**2]
for i, kernel in enumerate(kernels):
clf = GaussianProcessClassifier(kernel=kernel, warm_start=True).fit(X, Y)
# 绘制网格上每个数据点的决策函数
Z = clf.predict_proba(np.vstack((xx.ravel(), yy.ravel())).T)[:, 1]
Z = Z.reshape(xx.shape)
plt.subplot(1, 2, i + 1)
image = plt.imshow(Z, interpolation='nearest',
extent=(xx.min(), xx.max(), yy.min(), yy.max()),
aspect='auto', origin='lower', cmap=plt.cm.PuOr_r)
contours = plt.contour(xx, yy, Z, levels=[0.5], linewidths=2,
colors=['k'])
plt.scatter(X[:, 0], X[:, 1], s=30, c=Y, cmap=plt.cm.Paired,
edgecolors=(0, 0, 0))
plt.xticks(())
plt.yticks(())
plt.axis([-3, 3, -3, 3])
plt.colorbar(image)
plt.title("%s\n Log-Marginal-Likelihood:%.3f"
% (clf.kernel_, clf.log_marginal_likelihood(clf.kernel_.theta)),
fontsize=12)
plt.tight_layout()
plt.show()
脚本的总运行时间: ( 0 秒 0.649 分)
估计的内存使用量: 8 MB
下载python源代码: plot_gpc_xor.py
下载Jupyter notebook源代码: plot_gpc_xor.ipynb
由Sphinx-Gallery生成的画廊
![43b2364bb80b3d33d18af5fd5861178c.png](https://i-blog.csdnimg.cn/blog_migrate/a3571f852eefee477a2c00793fbba96c.png)
![5655bf14d06b6c8e49357f8acd4c363e.png](https://i-blog.csdnimg.cn/blog_migrate/e91463afe1cbb347ff64a29a00f82afd.png)
![c213f09b94c61ae6d8bf54b6b4ab1689.png](https://i-blog.csdnimg.cn/blog_migrate/c90c7871f98f6e64ac4243273e0756f4.png)