마지막으로 2장에서 만든 plot_decision_regions 함수를 사용하여 새로운 퍼셉트론 모델의 결정 경계를 그려서 세 개의 붓꽃 샘플을 잘 구분하는지 시각화하겠습니다. 여기서는 조금 수정해서 테스트 데이터셋의 샘플을 작은 원으로 다르게 표시하겠습니다.

    from matplotlib.colors import ListedColormap
    import matplotlib.pyplot as plt
    
    def plot_decision_regions(X, y, classifier, test_idx=None,
                              resolution=0.02):
    
        # 마커와 컬러맵을 설정합니다
        markers = ('s', 'x', 'o', '^', 'v')
        colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
        cmap = ListedColormap(colors[:len(np.unique(y))])
    
        # 결정 경계를 그립니다
        x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
        x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
        xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                               np.arange(x2_min, x2_max, resolution))
        Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
        Z = Z.reshape(xx1.shape)
        plt.contourf(xx1, xx2, Z, alpha=0.3, cmap=cmap)
        plt.xlim(xx1.min(), xx1.max())
        plt.ylim(xx2.min(), xx2.max())
    
        for idx, cl in enumerate(np.unique(y)):
            plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
                        alpha=0.8, c=colors[idx],
                        marker=markers[idx], label=cl,
                        edgecolor='black')

     

        # 테스트 샘플을 부각하여 그립니다7

        if test_idx:
            X_test, y_test = X[test_idx, :], y[test_idx]
    
            plt.scatter(X_test[:, 0], X_test[:, 1],
                        facecolors='none', edgecolor='black', alpha=1.0,
                        linewidth=1, marker='o',
                        s=100, label='test set')

     

     


      7 역주 맷플롯립 scatter 함수의 s 매개변수는 마커 크기를 지정합니다. 기본값은 36입니다. facecolors='none'으로 지정하면 마커 안을 비웁니다.

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.