[ML_code] visualize_boundary(model, X, y)

2025. 7. 17. 22:28python/ML

import numpy as np

# Classifier의 Decision Boundary를 시각화 하는 함수
def visualize_boundary(model, X, y):
    fig,ax = plt.subplots()
    
    # 학습 데이타 scatter plot으로 나타내기
    ax.scatter(X[:, 0], X[:, 1], c=y, s=25, cmap='rainbow', edgecolor='k',
               clim=(y.min(), y.max()), zorder=3)
    ax.axis('tight')
    ax.axis('off')
    xlim_start , xlim_end = ax.get_xlim()
    ylim_start , ylim_end = ax.get_ylim()
    
    # 호출 파라미터로 들어온 training 데이타로 model 학습 . 
    model.fit(X, y)
    # meshgrid 형태인 모든 좌표값으로 예측 수행. 
    xx, yy = np.meshgrid(np.linspace(xlim_start,xlim_end, num=200),np.linspace(ylim_start,ylim_end, num=200))
    Z = model.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
    
    # contourf() 를 이용하여 class boundary 를 visualization 수행. 
    n_classes = len(np.unique(y))
    contours = ax.contourf(xx, yy, Z, alpha=0.3,
                           levels=np.arange(n_classes + 1) - 0.5,
                           cmap='rainbow', clim=(y.min(), y.max()),
                           zorder=1)

def visualize_boundary(model, X, y): 

 --> Defines a function to visualize the decision boundary of a classification model trained on 2D data. 

 

    fig, ax = plt.subplots() 

 --> Creates a new matplotlib figure and axis object for plotting. (fig is paper, ax is pencil or the place where plots are drawing.) 

 

    ax.scatter(X[:, 0], X[:, 1], c=y, s=25, cmap='rainbow', edgecolor='k', zorder=3) 

 --> Plots the training data as a scatter plot, coloring each point based on its label. 

 

    ax.axis('tight') ;     ax.axis('off')

 --> Sets the plot limits tightly around the data points and hides the axis ticks and borders for a cleaner look.

 

    xlim_start, xlim_end = ax.get_xlim()
    ylim_start, ylim_end = ax.get_ylim()

 --> Retrieves the x and y axis limits, which will be used to generate the mesh grid for the background. 

 

    model.fit(X, y) 

 --> Trains the given classifier model on the training data. 

 

    xx, yy = np.meshgrid(
        np.linspace(xlim_start, xlim_end, num=200),
        np.linspace(ylim_start, ylim_end, num=200)
    )

 --> Creates a dense grid of (x, y) coordinates across the entire plot area using meshgrid. ( In this case, xx or yy each shape is (200 x 200).) 

 

    Z = model.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape) 

 --> Predicts the class for each point on the grid, then reshapes the flat predictions back into a 2D grid to match the shape of xx and yy. 

 

    n_classes = len(np.unique(y)) 

 --> Determines the number of unique classes in the dataset. 

 

    contours = ax.contourf(xx, yy, Z, alpha=0.3,
                           levels=np.arange(n_classes + 1) - 0.5,
                           cmap='rainbow',
                           zorder=1) 

 --> Uses contourf() to fill the background with colors representing predicted class regions.
  levels=np.arange(n_classes + 1) - 0.5 ensures decision boundaries are drawn precisely between classes (e.g., 0.5 creates a boundary between class 0 and 1. This trick is necessary because if the levels are simply set to [0, 1], then only the region where 0 ≤ Z < 1 will be filled — meaning class 1 won't be visualized at all. By setting levels to [-0.5, 0.5, 1.5], we ensure both class 0 and class 1 regions are painted properly, with the boundary at 0.5.)

 

 

Ref : https://github.com/wikibook/pymlrev2/blob/main/4%EC%9E%A5/4.2%20%EA%B2%B0%EC%A0%95%20%ED%8A%B8%EB%A6%AC.ipynb