def example2():
    my_random_generator = random.Random()
    my_random_generator.seed(0)
    # dense moons data set
    X_train_l, L_train_l, X_train_u, X_test, L_test = examples.get_moons_data(
        my_random_generator)
    X_test = np.array(X_test)
    L_train_l = (np.array(L_train_l) + 1) // 2
    L_test = (np.array(L_test) + 1) // 2
    print("Unique labels: ", np.unique(L_test))

    X_train = np.concatenate((X_train_l, X_train_u), axis=0)
    y_train = np.hstack((L_train_l, -np.ones(len(X_train_u))))
    X1 = X_train[:, 0].reshape((-1, 1))
    X2 = X_train[:, 1].reshape((-1, 1))

    print('Logistic')
    base_lr = LogisticRegression()
    base_lr.fit(X_train_l, L_train_l)
    #base_lr.fit(X_test, L_test)
    y_pred = base_lr.predict(X_test)
    print(classification_report(L_test, y_pred))

    print('Logistic CoTraining')
    t_start = time.time()
    lg_co_clf = CoTrainingClassifier(LogisticRegression())
    lg_co_clf.fit(X1, X2, y_train)
    t_end = time.time()
    y_pred_train = lg_co_clf.predict(X_train[:, 0].reshape((-1, 1)),
                                     X_train[:, 1].reshape((-1, 1)))
    y_pred = lg_co_clf.predict(X_test[:, 0].reshape((-1, 1)),
                               X_test[:, 1].reshape((-1, 1)))
    print(classification_report(L_test, y_pred))
    return
Exemplo n.º 2
0
def s3vm_moon():
    my_random_generator = random.Random()
    my_random_generator.seed(0)
    # dense moons data set
    X_train_l, L_train_l, X_train_u, X_test, L_test = examples.get_moons_data(
        my_random_generator)
    print("Unique labels: ", np.unique(L_test))
    print("X_train_l: ", X_train_l)
    print("L_train_l: ", L_train_l)
    print("X_train_u: ", X_train_u)
    print("X_test: ", X_test)
    print("L_test: ", L_test)
    t_start = time.time()
    # the parameter estimate_r for the balance ratio has to be provided explicitly, since the estimation
    # is bad due to only 5 labeled patterns in the training set.
    model = QN_S3VM(
        X_train_l,
        L_train_l,
        X_train_u,
        my_random_generator,
        lam=0.0009765625,
        lamU=1,
        kernel_type="RBF",
        sigma=0.5,
        estimate_r=0.0,
    )
    preds_train = model.train()
    t_end = time.time()
    elapsed_time = t_end - t_start
    preds = model.getPredictions(X_test)
    error = examples.classification_error(preds, L_test)
    print("Time needed to compute the model: ", elapsed_time, " seconds")
    print("Classification error of QN-S3VM: ", error)

    X_draw = np.concatenate((X_train_l, X_train_u), axis=0)
    y_draw = np.hstack((L_train_l, np.zeros(len(X_train_u))))
    disp(X_draw, y_draw)
    disp(X_draw, preds_train)
    disp(np.array(X_test), np.array(preds))
    return
Exemplo n.º 3
0
    model = QN_S3VM(X_train_l,
                    L_train_l,
                    X_train_u,
                    my_random_generator,
                    lam=1,
                    lamU=1)
    model.train()
    t_end = time.time()
    elapsed_time = t_end - t_start
    preds = model.getPredictions(X_test)
    error = examples.classification_error(preds, L_test)
    print("Time needed to compute the model: ", elapsed_time, " seconds")
    print("Classification error of QN-S3VM: ", error)

    # dense moons data set
    X_train_l, L_train_l, X_train_u, X_test, L_test = examples.get_moons_data(
        my_random_generator)
    t_start = time.time()
    # the parameter estimate_r for the balance ratio has to be provided explicitly, since the estimation
    # is bad due to only 5 labeled patterns in the training set.
    model = QN_S3VM(X_train_l, L_train_l, X_train_u, my_random_generator, lam=0.0009765625, lamU=1, \
                    kernel_type="RBF", sigma=0.5,  estimate_r=0.0,)
    model.train()
    t_end = time.time()
    elapsed_time = t_end - t_start
    preds = model.getPredictions(X_test)
    error = examples.classification_error(preds, L_test)
    print("Time needed to compute the model: ", elapsed_time, " seconds")
    print("Classification error of QN-S3VM: ", error)

    print("")
Exemplo n.º 4
0
    print "Classification error of QN-S3VM: ", error

    # dense gaussian data set
    X_train_l, L_train_l, X_train_u, X_test, L_test = examples.get_gaussian_data(my_random_generator)
    t_start = time.time()
    model = QN_S3VM(X_train_l, L_train_l, X_train_u, my_random_generator, lam=1, lamU=1)
    model.train()
    t_end = time.time()
    elapsed_time = t_end - t_start
    preds = model.getPredictions(X_test)
    error = examples.classification_error(preds,L_test)
    print "Time needed to compute the model: ", elapsed_time, " seconds"
    print "Classification error of QN-S3VM: ", error

    # dense moons data set
    X_train_l, L_train_l, X_train_u, X_test, L_test = examples.get_moons_data(my_random_generator)
    t_start = time.time()
    # the parameter estimate_r for the balance ratio has to be provided explicitly, since the estimation
    # is bad due to only 5 labeled patterns in the training set.
    model = QN_S3VM(X_train_l, L_train_l, X_train_u, my_random_generator, lam=0.0009765625, lamU=1, \
                    kernel_type="RBF", sigma=0.5,  estimate_r=0.0,)
    model.train()
    t_end = time.time()
    elapsed_time = t_end - t_start
    preds = model.getPredictions(X_test)
    error = examples.classification_error(preds,L_test)
    print "Time needed to compute the model: ", elapsed_time, " seconds"
    print "Classification error of QN-S3VM: ", error    

    print ""