Exemple #1
0
def plot_hyperplane(clf, ax: Axes = None, interval: float = .05, alpha=.3,
                    colors=('r', 'b')) -> Line3DCollection:
    """
Plots the hyperplane of the model in an axes
    :param clf: the classifier to use to find the hyperplane
    :param ax: the axes to plot the hyperplane into
    :param interval: the precision of the the hyperplane rendering.
    :param alpha:
    :param colors:
    :return: the mesh of the created hyperplane that was added to the axes
    """

    is_3d, ax = needed_axes(clf, ax)

    interval = int(1 / interval)

    # get the separating hyperplane
    x_min, x_max = ax.get_xlim()
    y_min, y_max = ax.get_ylim()

    # create grid to evaluate model
    xx = np.linspace(x_min, x_max, interval)
    yy = np.linspace(y_min, y_max, interval)

    if is_3d:
        z_min, z_max = ax.get_zlim()

        zz = np.linspace(z_min, z_max, interval)

        yy, xx, zz = np.meshgrid(yy, xx, zz)

        if hasattr(clf, "decision_function"):
            z = clf.decision_function(np.c_[xx.ravel(), yy.ravel(), zz.ravel()])
        elif hasattr(clf, "predict_proba"):
            z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel(), zz.ravel()])[:, 1]
        else:
            raise ValueError(
                "The model passed in does not contain either the decision_function or the predict_proba functions.")

        z = z.reshape(xx.shape)

        vertices, faces, _, _ = measure.marching_cubes_lewiner(z, 0)
        # Scale and transform to actual size of the interesting volume
        vertices = vertices * [x_max - x_min, y_max - y_min, z_max - z_min] / interval
        vertices += [x_min, y_min, z_min]
        # and create a mesh to display
        mesh = Line3DCollection(vertices[faces],
                                facecolor=colors, alpha=alpha)

        ax.add_collection3d(mesh)

        return mesh
    else:
        xx, yy = np.meshgrid(xx,
                             yy)
        Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
        Z = Z.reshape(xx.shape)

        return ax.contourf(xx, yy, Z, 10, colors=colors, alpha=alpha)
Exemple #2
0
def plot_hyperplane(clf, ax: Axes = None, interval: float = .05, alpha=.3,
                    colors: list = ('r', 'b')) -> Line3DCollection:
    """
Plots the hyperplane of the model in an axes
    :param clf: the classifier to use to find the hyperplane
    :param ax: the axes to plot the hyperplane into
    :param interval: the precision of the the hyperplane rendering.
    :return: the mesh of the created hyperplane that was added to the axes
    """

    is_3d = False

    if ax is None:
        try:
            clf.predict([[0, 0, 0]])
            is_3d = True
            ax = plt.gca(projection="3d")
        except ValueError:
            is_3d = False
            ax = plt.gca()

    elif isinstance(ax, Axes3D):
        is_3d = True

    interval = int(1 / interval)

    # get the separating hyperplane
    x_min, x_max = ax.get_xlim()
    y_min, y_max = ax.get_ylim()

    # create grid to evaluate model
    xx = np.linspace(x_min, x_max, interval)
    yy = np.linspace(y_min, y_max, interval)

    if is_3d:
        z_min, z_max = ax.get_zlim()

        zz = np.linspace(z_min, z_max, interval)

        yy, xx, zz = np.meshgrid(yy, xx, zz)

        if hasattr(clf, "decision_function"):
            z = clf.decision_function(np.c_[xx.ravel(), yy.ravel(), zz.ravel()])
        elif hasattr(clf, "predict_proba"):
            z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel(), zz.ravel()])[:, 1]
        z = z.reshape(xx.shape)

        vertices, faces, _, _ = measure.marching_cubes(z, 0)
        # Scale and transform to actual size of the interesting volume
        vertices = vertices * [x_max - x_min, y_max - y_min, z_max - z_min] / interval
        vertices += [x_min, y_min, z_min]
        # and create a mesh to display
        mesh = Line3DCollection(vertices[faces],
                                facecolor=colors, alpha=alpha)

        ax.add_collection3d(mesh)

        return mesh
    else:
        xx, yy = np.meshgrid(xx,
                             yy)
        Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
        Z = Z.reshape(xx.shape)

        return ax.contourf(xx, yy, Z, 10, colors=colors, alpha=alpha)