文章目录
源码
源码来自Python Machine Learning 3rd,Chapter 3,自己加入了一些理解和修改
优势
- 适用于大多数分类算法模型,对不同参数的统一模型都能够绘制出不同的决策边界
- 高亮测试集样本
- 默认支持5种分类情况,可拓展
局限
- 只能够适用于二维特征向量的数据可视化
- 只适用于监督学习分类问题,包括线性回归,逻辑回归,SVM,KNN,决策树,随机森林等等
代码
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
%matplotlib inline
def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):
# setup marker generator and color map
markers = ('s', 'x', 'o', '^', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(y))]) # colors that need in classfication
## plot the decision surface
# get the range of axes
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1