Ejemplo n.º 1
0
def runLDA(X_train, X_test, y_train, y_test, comp_range):
    rbf_scores = []
    linear_scores = []
    for n_comp in comp_range:
        print("\nn_comp=%d\n"%(n_comp))
        transformer = LinearDiscriminantAnalysis(solver='svd', n_components=n_comp)
        transformer.fit(X_train, y_train)
        X_train_proj = transformer.transform(X_train)
        X_test_proj = transformer.transform(X_test)
        if n_comp == 2:
            np.save('X_train_proj_2d_LDA', X_train_proj)
            np.save('X_test_proj_2d_LDA', X_test_proj)
        score_rbf = SVMmodel.runSVM(X_train_proj, X_test_proj, y_train, y_test, SVMmodel.getBestParam('rbf'), 'rbf')
        rbf_scores.append(score_rbf.mean())
        score_linear = SVMmodel.runSVM(X_train_proj, X_test_proj, y_train, y_test, SVMmodel.getBestParam('linear'), 'linear')
        linear_scores.append(score_linear.mean())
    return rbf_scores, linear_scores
def runAE(comp_range):
    rbf_scores = []
    linear_scores = []
    for n_comp in comp_range:
        print("\nn_comp=%d\n" % (n_comp))
        data_pointer = 0
        X_train_proj, X_test_proj = AutoEncoder(n_comp)
        if n_comp == 2:
            np.save('X_train_proj_2d_AE', X_train_proj)
            np.save('X_test_proj_2d_AE', X_test_proj)
        score_rbf = SVMmodel.runSVM(X_train_proj, X_test_proj, y_train, y_test,
                                    SVMmodel.getBestParam('rbf'), 'rbf')
        rbf_scores.append(score_rbf.mean())
        score_linear = SVMmodel.runSVM(X_train_proj, X_test_proj, y_train,
                                       y_test, SVMmodel.getBestParam('linear'),
                                       'linear')
        linear_scores.append(score_linear.mean())
    return rbf_scores, linear_scores
def runTreeBasedSelection(X_train, X_test, y_train, y_test, comp_range):
    rbf_scores = []
    linear_scores = []
    dimension = []

    for n_comp in comp_range:
        print("\nn_comp=%f\n"%(n_comp))

        clf = ExtraTreesClassifier(n_estimators=n_comp)
        model = SelectFromModel(estimator=clf)
        model.fit(X_train, y_train)
        X_train_sel = model.transform(X_train)
        X_test_sel = model.transform(X_test)

        dimension.append(X_train_sel.shape[1])

        score_rbf = SVMmodel.runSVM(X_train_sel, X_test_sel, y_train, y_test, SVMmodel.getBestParam('rbf'), 'rbf')
        rbf_scores.append(score_rbf.mean())
        score_linear = SVMmodel.runSVM(X_train_sel, X_test_sel, y_train, y_test, SVMmodel.getBestParam('linear'), 'linear')
        linear_scores.append(score_linear.mean())
    return rbf_scores, linear_scores, dimension
Ejemplo n.º 4
0
def runIsomap(X_train, X_test, y_train, y_test, comp_range, n_neigh):
    rbf_scores = []
    linear_scores = []
    for n_comp in comp_range:
        print("\nn_comp=%d\n" % (n_comp))
        transformer = Isomap(n_neighbors=n_neigh,
                             n_components=n_comp,
                             n_jobs=8)
        transformer.fit(X_train)
        X_train_proj = transformer.transform(X_train)
        X_test_proj = transformer.transform(X_test)
        if n_comp == 2:
            np.save('X_train_proj_2d_Isomap_' + str(n_neigh), X_train_proj)
            np.save('X_test_proj_2d_Isomap_' + str(n_neigh), X_test_proj)
        score_rbf = SVMmodel.runSVM(X_train_proj, X_test_proj, y_train, y_test,
                                    SVMmodel.getBestParam('rbf'), 'rbf')
        rbf_scores.append(score_rbf.mean())
        score_linear = SVMmodel.runSVM(X_train_proj, X_test_proj, y_train,
                                       y_test, SVMmodel.getBestParam('linear'),
                                       'linear')
        linear_scores.append(score_linear.mean())
    for i, scores in enumerate([rbf_scores, linear_scores]):
        if i == 0:
            kernel = 'rbf'
        elif i == 1:
            kernel = 'linear'
        else:
            kernel = ''
        bestIdx = np.argmax(scores)
        bestNComp = comp_range[bestIdx]
        bestAcc = scores[bestIdx]
        with open('res_Isomap_' + kernel + '_' + str(n_neigh) + '.txt',
                  'w') as f:
            for j in range(len(comp_range)):
                f.write(kernel + ": n_comp = %f, acc = %f\n" %
                        (comp_range[j], scores[j]))
            f.write(kernel + ": Best n_comp = %f\n" % (bestNComp))
            f.write(kernel + ": acc = %f\n" % (bestAcc))
    return rbf_scores, linear_scores
def runSelectKBest(X_train, X_test, y_train, y_test, comp_range):
    rbf_scores = []
    linear_scores = []
    for n_comp in comp_range:
        print("\nn_comp=%d\n" % (n_comp))

        selector = SelectKBest(score_func=f_classif, k=n_comp)
        selector.fit(X_train, y_train)
        X_train_sel = selector.transform(X_train)
        X_test_sel = selector.transform(X_test)

        if n_comp == 2:
            np.save('X_train_proj_2d_SelectKBest', X_train_sel)
            np.save('X_test_proj_2d_SelectKBest', X_test_sel)
        score_rbf = SVMmodel.runSVM(X_train_sel, X_test_sel, y_train, y_test,
                                    SVMmodel.getBestParam('rbf'), 'rbf')
        rbf_scores.append(score_rbf.mean())
        score_linear = SVMmodel.runSVM(X_train_sel, X_test_sel, y_train,
                                       y_test, SVMmodel.getBestParam('linear'),
                                       'linear')
        linear_scores.append(score_linear.mean())
    return rbf_scores, linear_scores
Ejemplo n.º 6
0
def runTSNE(X_train, X_test, y_train, y_test, comp_range, ppl, m):
    rbf_scores = []
    linear_scores = []
    for n_comp in comp_range:
        print("\nn_comp=%d\n" % (n_comp))
        transformer = TSNE(n_components=n_comp,
                           perplexity=50.0,
                           method=m,
                           n_jobs=8)
        transformer.fit(X_train)
        X_train_proj = transformer.fit_transform(X_train)
        X_test_proj = transformer.fit_transform(X_test)
        if n_comp == 2 and m == 'barnes_hut':
            np.save('X_train_proj_2d_TSNE_' + str(ppl), X_train_proj)
            np.save('X_test_proj_2d_TSNE_' + str(ppl), X_test_proj)
        score_rbf = SVMmodel.runSVM(X_train_proj, X_test_proj, y_train, y_test,
                                    SVMmodel.getBestParam('rbf'), 'rbf')
        rbf_scores.append(score_rbf.mean())
        score_linear = SVMmodel.runSVM(X_train_proj, X_test_proj, y_train,
                                       y_test, SVMmodel.getBestParam('linear'),
                                       'linear')
        linear_scores.append(score_linear.mean())
    return rbf_scores, linear_scores
def runVarianceThreshold(X_train, X_test, y_train, y_test, comp_range):
    rbf_scores = []
    linear_scores = []
    dimension = []

    for n_comp in comp_range:
        print("\nn_comp=%f\n" % (n_comp))

        selector = VarianceThreshold(threshold=n_comp)
        selector.fit(X_train)
        X_train_sel = selector.transform(X_train)
        X_test_sel = selector.transform(X_test)

        dimension.append(X_train_sel.shape[1])

        score_rbf = SVMmodel.runSVM(X_train_sel, X_test_sel, y_train, y_test,
                                    SVMmodel.getBestParam('rbf'), 'rbf')
        rbf_scores.append(score_rbf.mean())
        score_linear = SVMmodel.runSVM(X_train_sel, X_test_sel, y_train,
                                       y_test, SVMmodel.getBestParam('linear'),
                                       'linear')
        linear_scores.append(score_linear.mean())
    return rbf_scores, linear_scores, dimension
Ejemplo n.º 8
0
def runPCA(X_train, X_test, y_train, y_test, comp_range, Kernel):
    C = SVMmodel.getBestParam(Kernel)
    scores = []
    for n_comp in comp_range:
        print("\nn_comp=%d\n" % (n_comp))
        transformer = KernelPCA(n_components=n_comp,
                                kernel=Kernel,
                                copy_X=True,
                                n_jobs=8)
        transformer.fit(X_train)
        X_train_proj = transformer.transform(X_train)
        X_test_proj = transformer.transform(X_test)
        if n_comp == 2:
            np.save('X_train_proj_2d_' + Kernel, X_train_proj)
            np.save('X_test_proj_2d_' + Kernel, X_test_proj)
        score = SVMmodel.runSVM(X_train_proj, X_test_proj, y_train, y_test, C,
                                Kernel)
        scores.append(score.mean())
        print(scores)
    return scores