Exemple #1
0
def run(clf):
    acc_records, y_records = [], []
    bar = ProgressBar(max_value=10, name="Main")
    for _ in range(10):
        if clf == "Naive Bayes":
            _clf = SKMultinomialNB(alpha=0.1)
        elif clf == "Non-linear SVM":
            _clf = SKSVM()
        else:
            _clf = SKLinearSVM()
        rs = main(_clf)
        acc_records.append(rs[0])
        y_records += rs[1]
        bar.update()
    acc_records = np.array(acc_records) * 100

    plt.figure()
    plt.boxplot(acc_records, vert=False, showmeans=True)
    plt.show()

    from Util.DataToolkit import DataToolkit
    idx = np.argmax(acc_records)  # type: int
    print(
        metrics.classification_report(y_records[idx][0],
                                      y_records[idx][1],
                                      target_names=np.load(
                                          os.path.join("_Data",
                                                       "LABEL_DIC.npy"))))
    toolkit = DataToolkit(acc_records[np.argmax(np.average(acc_records,
                                                           axis=1))])
    print("Acc Mean     : {:8.6}".format(toolkit.mean))
    print("Acc Variance : {:8.6}".format(toolkit.variance))
    print("Done")
def main():

    # x, y = DataUtil.gen_xor(100, one_hot=False)
    x, y = DataUtil.gen_spiral(20, 4, 2, 2, one_hot=False)
    # x, y = DataUtil.gen_two_clusters(n_dim=2, one_hot=False)
    y[y == 0] = -1

    animation_params = {
        "show": False,
        "mp4": False,
        "period": 50,
        "dense": 400,
        "draw_background": True
    }

    svm = SVM(animation_params=animation_params)
    svm.fit(x, y, kernel="poly", p=12, epoch=600)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm["alpha"] > 0)

    svm = GDSVM(animation_params=animation_params)
    svm.fit(x, y, kernel="poly", p=12, epoch=10000)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm["alpha"] > 0)

    if TorchSVM is not None:
        svm = TorchSVM(animation_params=animation_params)
        svm.fit(x, y, kernel="poly", p=12)
        svm.evaluate(x, y)
        svm.visualize2d(x,
                        y,
                        padding=0.1,
                        dense=400,
                        emphasize=svm["alpha"] > 0)

    svm = TFSVM()
    svm.fit(x, y)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400)

    svm = SKSVM()
    # svm = SKSVM(kernel="poly", degree=12)
    svm.fit(x, y)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm.support_)

    (x_train,
     y_train), (x_test,
                y_test), *_ = DataUtil.get_dataset("mushroom",
                                                   "../_Data/mushroom.txt",
                                                   n_train=100,
                                                   quantize=True,
                                                   tar_idx=0)
    y_train[y_train == 0] = -1
    y_test[y_test == 0] = -1

    svm = SKSVM()
    svm.fit(x_train, y_train)
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    svm = TFSVM()
    svm.fit(x_train, y_train)
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    if TorchSVM is not None:
        svm = TorchSVM()
        svm.fit(x_train, y_train)
        svm.evaluate(x_train, y_train)
        svm.evaluate(x_test, y_test)

        svm = TorchSVM()
        logs = [
            log[0] for log in svm.fit(x_train,
                                      y_train,
                                      metrics=["acc"],
                                      x_test=x_test,
                                      y_test=y_test)
        ]
        svm.evaluate(x_train, y_train)
        svm.evaluate(x_test, y_test)

        plt.figure()
        plt.title(svm.title)
        plt.plot(range(len(logs)), logs)
        plt.show()

    svm = SVM()
    logs = [
        log[0] for log in svm.fit(
            x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test)
    ]
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    plt.figure()
    plt.title(svm.title)
    plt.plot(range(len(logs)), logs)
    plt.show()

    svm = GDSVM()
    logs = [
        log[0] for log in svm.fit(
            x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test)
    ]
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    plt.figure()
    plt.title(svm.title)
    plt.plot(range(len(logs)), logs)
    plt.show()

    svm.show_timing_log()
Exemple #3
0
def main():

    # x, y = DataUtil.gen_xor(100, one_hot=False)
    x, y = DataUtil.gen_spiral(20, 4, 2, 2, one_hot=False)
    # x, y = DataUtil.gen_two_clusters(n_dim=2, one_hot=False)
    y[y == 0] = -1
    #
    # svm = SKSVM()
    # # svm = SKSVM(kernel="poly", degree=12)
    # svm.fit(x, y)
    # svm.evaluate(x, y)
    # svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm.support_)
    #
    # svm = TFSVM()
    # svm.fit(x, y, lr=0.0001)
    # svm.evaluate(x, y)
    # svm.visualize2d(x, y, padding=0.1, dense=400)
    #
    svm = SVM()
    svm.fit(x, y, kernel="poly", p=12)
    # _logs = [_log[0] for _log in svm.fit(x, y, metrics=["acc"])]
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm["alpha"] > 0)

    (x_train,
     y_train), (x_test,
                y_test), *_ = DataUtil.get_dataset("mushroom",
                                                   "../_Data/mushroom.txt",
                                                   train_num=100,
                                                   quantize=True,
                                                   tar_idx=0)
    y_train[y_train == 0] = -1
    y_test[y_test == 0] = -1

    svm = SKSVM()
    svm.fit(x_train, y_train)
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    svm = TFSVM()
    _logs = [
        _log[0] for _log in svm.fit(
            x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test)
    ]
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    plt.figure()
    plt.title(svm.title)
    plt.plot(range(len(_logs)), _logs)
    plt.show()

    svm = SVM()
    _logs = [
        _log[0] for _log in svm.fit(
            x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test)
    ]
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    plt.figure()
    plt.title(svm.title)
    plt.plot(range(len(_logs)), _logs)
    plt.show()

    svm.show_timing_log()