示例#1
0
    def decision_boundary(self, x, y, name: str = '', ax: plt.axis = None):
        if ax is None:
            fig, ax = plt.subplots(figsize=(10, 10))

        h = 0.02

        x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1
        y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1

        xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                             np.arange(y_min, y_max, h))

        print(np.c_[xx.ravel(), yy.ravel()].shape)
        z = self.predict(np.c_[xx.ravel(), yy.ravel()])
        z = z.reshape(xx.shape)

        ax.contourf(xx, yy, z, cmap=plt.cm.coolwarm, alpha=0.8)
        ax.scatter(x[:, 0], x[:, 1], c=y, cmap=plt.cm.coolwarm)
        ax.set_xlim(xx.min(), xx.max())
        ax.set_ylim(yy.min(), yy.max())
        ax.set_xticks(())
        ax.set_yticks(())

        if name:
            plt.savefig(os.path.join(os.getcwd(), name + '.png'))
            plt.clf()
        else:
            plt.show()
            plt.clf()
示例#2
0
文件: nn_core.py 项目: gamble27/HW_ML
    def decision_boundary(self, x, y, ax: plt.axis = None):
        """
        Plot decision boundary and labeled data
        :param x: data
        :param y: true labels
        :param ax: matplotlib axes (optional)
        :return: None
        """
        if ax is None:
            fig, ax = plt.subplots(figsize=(10, 10))

        h = 0.02
        x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1
        y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1
        xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                             np.arange(y_min, y_max, h))

        z = self.predict(np.c_[xx.ravel(), yy.ravel()])
        z = z.reshape(xx.shape)
        ax.contourf(xx, yy, z, cmap=plt.cm.coolwarm, alpha=0.8)
        ax.scatter(x[:, 0], x[:, 1], c=y, cmap=plt.cm.coolwarm)
        ax.set_xlim(xx.min(), xx.max())
        ax.set_ylim(yy.min(), yy.max())
        ax.set_xticks(())
        ax.set_yticks(())

        plt.show()
示例#3
0
def plot(ax: plt.axis, x, y, u, v, trajectory, pause=0.2, title=""):
    """
    Plot function for subtask 1
    """
    ax.set_aspect('equal')
    arrowed_spines(ax)
    ax.set_xlim([min(x) * 1, max(x) * 1])
    ax.set_ylim([min(y) * 1, max(y) * 1])
    ax.set_title(title)
    ax.streamplot(x, y, u, v)
    ax.plot(trajectory[:, 0], trajectory[:, 1])
def set_inset_spectrum(axis: plt.axis, data: np.ndarray, current_index: int,
                       peak_collection: PeakCollection) -> plt.axis:
    axis.plot(data)  # Show intensity spectrum
    axis = plot_peaks(axis=axis, collection=peak_collection)  # Show peaks
    y_bot, y_top = axis.get_ylim()
    text_height = y_bot + 0.6 * (y_top - y_bot)  # Data coordinates
    _margin = 50
    x_lim = [
        max(0, current_index - _margin),
        min(len(data) - 1, current_index + _margin)
    ]
    # Plot clusters
    # for cluster in peak_collection.get_clusters:
    #     bound_left, bound_right = cluster.get_value_slice
    #     if bound_right > x_lim[0] or bound_left < x_lim[1]:
    #         axis.axvspan(bound_left, bound_right, alpha=0.5, color='green')
    #     if x_lim[0] < cluster.get_avg_x < x_lim[1]:
    #         axis.text(x=cluster.get_avg_x, y=text_height, s=r'$\tilde{m}$'+f'={cluster.get_transverse_mode_id}')
    axis.axvline(x=current_index, color='r')
    axis.set_xlim(x_lim)
    return axis