本文主要介绍使用matplotlib对分类器在二维平面的分类结果进行可视化
以iris数据集为例,取数据集前两列建模
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)
lr = LogisticRegression()
lr.fit(X[:,:2], y)
# 画图
grid1 = np.arange(4, 8, 0.02)
grid2 = np.arange(2, 6, 0.02)
xx, yy = np.meshgrid(grid1, grid2)
Grid = np.vstack([xx.reshape(-1), yy.reshape(-1)]).T
zz = lr.predict(Grid).reshape(xx.shape)
plt.contourf(xx, yy, zz, cmap=plt.cm.Set1, alpha=0.5)