Matplotlib visualization contour map plt.contourf() and drawing decision boundaries in machine learning

  • Function function: used to draw contour lines and decision boundaries
  • Call method: plt.contourf(X,Y,Z,cmap)
  • Parameter Description:
    • X: the abscissa of the grid point
    • Y: Y coordinate of the grid point
    • Z: the value of the grid point (the height value of the contour map)
    • cmap: color map, specifying different fill colors corresponding to different values ​​of Z (different heights)

1. Draw a contour map:

import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(2)
x = np.arange(-5, 5, 0.1)
y = np.arange(-5, 5, 0.1)
xx, yy = np.meshgrid(x, y)  # 生成网格点的横坐标xx与纵坐标yy
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)  # 三维中的高度值
ax1.contourf(xx, yy, z)
ax2.contour(xx, yy, z)  # 与contourf区别在于不同高度面不填充颜色
plt.show()

 2. Understanding of contour maps and grid points:

The contour map is essentially the mapping of the three-dimensional map on the two-dimensional plane, and the grid points are the points on the plane formed by the x and y axes. Since the z-axis is based on the two-dimensional plane, it refers to the two-dimensional plane. And the whole plane can be seen as densely packed grid points piled together to form.

The grid point coordinates can be generated by the np.meshgrid(x,y) function, and the function returns two arrays of the abscissa and ordinate of the grid point

 

 As can be seen from the above figure, when the number of grid points is large, a surface is formed, x and y are equivalent to one-dimensional lines, and then the grid points are generated through np.meshgrid, which can be understood as two lines enclosing a surface, all 3D graphs are drawn based on grid points

3. Drawing decision boundaries in machine learning:

In machine learning, it is often necessary to analyze and observe the decision boundary. Here is a small case of machine learning and draw the decision boundary:

import numpy as np
import pandas as pd 
import sklearn
from sklearn import datasets,model_selection
from sklearn.svm import SVC  # 支持向量机分类模型
from sklearn.preprocessing import MinMaxScaler # 数据归一化函数
def data_plot():
    # 使用葡萄酒数据集
    wine = datasets.load_wine()
    wine_data = wine.data
    data = pd.DataFrame(wine_data,columns = wine.feature_names)
    target = wine.target
    # 选取前两个特征作为数据
    data = data.iloc[:,:2]

    scaler = MinMaxScaler()           # 归一化特征函数
    data = scaler.fit_transform(data) # 对特征数据进行归一化
    
    # 根据两个特征数据绘制散点图
    plt.scatter(data[:,0],data[:,1],c = target,s= 50, cmap = plt.cm.plasma,edgecolors = 'k')
    plt.xlim(-0.5,1.5)
    plt.ylim(-0.5,1.5)
    plt.show()
     
    # 划分数据集
    x_train, x_test, y_train,y_test = model_selection.train_test_split(data,target,test_size = 0.2,random_state = 100)
    # 创建支持向量机模型
    svc = SVC()
    
    # 训练模型
    svc.fit(x_train,y_train)
    print('模型在测试集上的准确率:',svc.score(x_test,y_test))
    print('-'*120)
    
    # 构建生成网格点的函数
    def make_meshgrid(x,y):
        x_min, x_max = x.min()-0.5,x.max()+0.5
        y_min, y_max = y.min()-0.5,y.max()+0.5
        xx,yy = np.meshgrid(np.linspace(x_min,x_max,300),np.linspace(y_min,y_max,500))
        return xx,yy
    
    # 对网格点进行预测并得到标签类别
    xx,yy = make_meshgrid(data[:,0],data[:,0])
    xy = np.c_[xx.ravel(),yy.ravel()]
    z = model2.predict(xy).reshape(xx.shape)
    
    # 绘制决策边界
    fig = plt.figure()
    ax = fig.add_subplot(111)
    print('绘制决策边界:')
    plt.scatter(data[:,0],data[:,1],c = target,s= 50, cmap = plt.cm.plasma,edgecolors = 'k')
    ax.contourf(xx,yy,z,cmap = plt.cm.plasma,alpha =0.5)
    plt.show()

# 调用函数
data_plot()

Guess you like

Origin blog.csdn.net/weixin_46707493/article/details/119843991