Exemple #1
0
def lda(X, Y, dim=2, **kargs):
    '''Projection on to the first 2 linear discriminant components'''
    print("Computing LDA projection")
    try:
        ldatr = ldaModel(lda.LDA(n_components=dim))
        X_lda = ldatr.fit_transform(X, Y)
        return ldatr, X_lda, "Linear Discriminant projection of the features"
    except Exception as e:
        traceback.print_exc()
Exemple #2
0
def plot_simple_demo_lda():
    pylab.clf()
    fig = pylab.figure(num=None, figsize=(10, 4))
    pylab.subplot(121)

    title = "Original feature space"
    pylab.title(title)
    pylab.xlabel("$X_1$")
    pylab.ylabel("$X_2$")

    good = x1 > x2
    bad = ~good

    x1g = x1[good]
    x2g = x2[good]
    pylab.scatter(x1g, x2g, edgecolor="blue", facecolor="blue")

    x1b = x1[bad]
    x2b = x2[bad]
    pylab.scatter(x1b, x2b, edgecolor="red", facecolor="white")

    pylab.grid(True)

    pylab.subplot(122)

    X = np.c_[(x1, x2)]

    lda_inst = lda.LDA(n_components=1)
    Xtrans = lda_inst.fit_transform(X, good)

    Xg = Xtrans[good]
    Xb = Xtrans[bad]

    pylab.scatter(Xg[:, 0],
                  np.zeros(len(Xg)),
                  edgecolor="blue",
                  facecolor="blue")
    pylab.scatter(Xb[:, 0],
                  np.zeros(len(Xb)),
                  edgecolor="red",
                  facecolor="white")
    title = "Transformed feature space"
    pylab.title(title)
    pylab.xlabel("$X'$")
    fig.axes[1].get_yaxis().set_visible(False)

    pylab.grid(True)

    pylab.autoscale(tight=True)
    filename = "lda_demo.png"
    pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")