def logreg_image(train: np.ndarray,
                 test: np.ndarray,
                 regularization_coefficients: List[float],
                 is_cifar: bool = False) -> None:
    '''
    without reg : 0
    with reg: regularization_coefficients = 1 / 2sigma^2
    :param train: data and labels for classifier training
    :param test: data and labels for classifier test
    :param is_cifar: True if CIFAR dataset is used, False if MNIST
    '''
    train_label = np.transpose(train[0, :].astype(np.double))
    train_label[train_label < 0] = 0.0
    train_x = train.astype(np.double)
    train_x[0, :] = 1.0

    print("Dataset ballance in training {:.2f}%".format(
        100 * np.sum(train_label) / len(train_label)))

    test_label = np.transpose(test[0, :].astype(np.double))
    test_label[test_label < 0] = 0.0
    test_x = test.astype(np.double)
    test_x[0, :] = 1.0

    print("Dataset ballance in test {:.2f}%".format(100 * np.sum(test_label) /
                                                    len(test_label)))

    for r in regularization_coefficients:
        logreg = LOGREG(r)

        print(
            'Training a LOGREG classifier with regularization coefficient: {}'.
            format(r))

        # training
        logreg.train(train_x, train_label, 50)
        print('Training')
        logreg.printClassification(train_x, train_label)
        print('Test')
        logreg.printClassification(test_x, test_label)

        visualizeClassification(train_x[1:, :], train_label,
                                logreg.classify(train_x), 3 * 3, is_cifar,
                                'training with reg: {}'.format(r))
        visualizeClassification(test_x[1:, :], test_label,
                                logreg.classify(test_x), 3 * 3, is_cifar,
                                'test with reg: {}'.format(r))
def logregMNIST(train, test):
    '''
    without reg : 0
    with reg: regcoeff = 1 / 2sigma^2
    :param train:
    :param test:
    :return:
    '''
    regcoeff = [0, 0.1, 0.5]

    train_label = np.transpose(train[0, :].astype(np.double))
    train_label[train_label < 0] = 0.0
    train_x = train[0:, :].astype(np.double)
    train_x[0, :] = 1.0

    test_label = np.transpose(test[0, :].astype(np.double))
    test_label[test_label < 0] = 0.0
    test_x = test[0:, :].astype(np.double)
    test_x[0, :] = 1.0

    for r in regcoeff:
        logreg = LOGREG(r)

        print(
            'Training a LOGREG classifier with regularization coefficient: {}'.
            format(r))

        # training
        logreg.train(train_x, train_label, 50)
        print('Training')
        logreg.printClassification(train_x, train_label)
        print('Test')
        logreg.printClassification(test_x, test_label)

        visualizeClassification(train_x[1:, :], train_label,
                                logreg.classify(train_x), 3 * 3,
                                'training with reg: {}'.format(r))
        visualizeClassification(test_x[1:, :], test_label,
                                logreg.classify(test_x), 3 * 3,
                                'test with reg: {}'.format(r))
def logregToy():
    #load data
    toy = scipy.io.loadmat('../data/toy.mat')
    toy_train = toy['toy_train']
    toy_test = toy['toy_test']

    toy_train_label = np.transpose(toy_train[0, :].astype(np.double))
    toy_train_label[toy_train_label < 0] = 0.0
    toy_train_x = toy_train[0:3, :].astype(np.double)
    toy_train_x[
        0, :] = 1.0  # adding row of 1s in X matrix to account for w0 term

    toy_test_label = np.transpose(toy_test[0, :].astype(np.double))
    toy_test_label[toy_test_label < 0] = 0.0
    toy_test_x = toy_test[0:3, :].astype(np.double)
    toy_test_x[
        0, :] = 1.0  # adding row of 1s in X matrix to account for w0 term

    #training coefficients
    regcoeff = [0, 0.1, 0.5]
    # without regularization : regcoeff = 0
    # with regularization    : regcoeff = 1 / 2*sigma^2

    for r in regcoeff:

        print('with regularization coefficient ', r)
        logreg = LOGREG(r)
        trained_theta = logreg.train(toy_train_x, toy_train_label, 50)
        print("Training:")
        logreg.printClassification(toy_train_x, toy_train_label)
        print("Test:")
        logreg.printClassification(toy_test_x, toy_test_label)

        # plot for toy dataset
        figname = 'Toy dataset with r: {}'.format(r)
        fig = plt.figure(figname)
        plt.subplot(221)
        plot2D(plt, toy_train_x, toy_train_label, trained_theta, 'Training')
        plt.subplot(222)
        plot2D(plt, toy_test_x, toy_test_label, trained_theta, 'Testing')

        plot3D(plt, fig.add_subplot(223, projection='3d'), toy_train_x,
               toy_train_label, trained_theta, 'Training')
        plot3D(plt, fig.add_subplot(224, projection='3d'), toy_test_x,
               toy_test_label, trained_theta, 'Test')

    plt.show()
Example #4
0
def plot3D(ax: plt, sub3d: plt, X: np.ndarray, y: np.ndarray, w: np.ndarray,
           name: str) -> None:
    '''
    Visualize decision boundary and data classes in 3D
    :param ax:  matplotlib
    :param sub3d: fig.add_subplot(XXX, projection='3d')
    :param X: data
    :param y: data labels
    :param w: model parameters
    :param name: plot name identifier
    :return:
    '''
    x1 = np.array(X[1, :])  # note: X_train[0,:] is the added row of 1s (bias)
    x2 = np.array(X[2, :])
    posterior1 = LOGREG().activationFunction(w, X)
    posterior1 = np.squeeze(np.asarray(posterior1))
    markers = ['o', '+']
    groundTruthLabels = np.unique(y)
    for li in range(len(groundTruthLabels)):
        x1_sub = x1[y[:] == groundTruthLabels[li]]
        x2_sub = x2[y[:] == groundTruthLabels[li]]
        m_sub = markers[li]
        posterior1_sub = posterior1[y[:] == groundTruthLabels[li]]
        sub3d.scatter(x1_sub,
                      x2_sub,
                      posterior1_sub,
                      c=posterior1_sub,
                      vmin=0,
                      vmax=1,
                      marker=m_sub,
                      label='ground truth label = ' + str(li))
    ax.legend()
    x = np.arange(np.min(x1), np.max(x1), 0.1)
    pms = [[0.1, 'k:'], [0.25, 'k--'], [0.5, 'r'], [0.75, 'k-.'], [0.9, 'k-']]
    for (p, m) in pms:
        yp = (-np.log((1 / p) - 1) - w[1] * x - w[0]) / w[2]
        yp = np.squeeze(np.asarray(yp))
        z = np.ones(yp.shape) * p
        sub3d.plot(x, yp, z, m, label='p = ' + str(p))
        ax.legend()
    ax.xlabel('feature 1')
    ax.ylabel('feature 2')
    ax.title(name + '\n Posterior for class labeled 1')
Example #5
0
def logregToy() -> None:
    # load data
    toy = scipy.io.loadmat(dataPath + 'toy.mat')
    toy_train = toy['toy_train']
    toy_test = toy['toy_test']

    train_label = np.transpose(toy_train[0, :].astype(np.double))
    train_label[train_label < 0] = 0.0
    train_x = toy_train[0:3, :].astype(np.double)
    train_x[0, :] = 1.0  # adding row of 1s in X matrix to account for w0 term

    test_label = np.transpose(toy_test[0, :].astype(np.double))
    test_label[test_label < 0] = 0.0
    test_x = toy_test[0:3, :].astype(np.double)
    test_x[0, :] = 1.0  # adding row of 1s in X matrix to account for w0 term

    # training coefficients
    regularization_coefficients = [0, 0.1, 1.0]
    # without regularization : regularization_coefficients = 0
    # with regularization    : regularization_coefficients = 1 / 2*sigma^2

    for r in regularization_coefficients:
        print('with regularization coefficient ', r)
        logreg = LOGREG(r)
        trained_w = logreg.train(train_x, train_label, 50)
        print("Training:")
        logreg.printClassification(train_x, train_label)
        print("Test:")
        logreg.printClassification(test_x, test_label)
        print("PARS: {}".format(trained_w))

        # plot for toy dataset
        figname = 'Toy dataset with r: {}'.format(r)
        fig = plt.figure(figname, figsize=(12, 12))
        plt.tight_layout()
        plt.subplot(221)
        plot2D(plt, train_x, train_label, trained_w, 'Training')
        plt.subplot(222)
        plot2D(plt, test_x, test_label, trained_w, 'Testing')
        plot3D(plt, fig.add_subplot(223, projection='3d'), train_x,
               train_label, trained_w, 'Training')
        plot3D(plt, fig.add_subplot(224, projection='3d'), test_x, test_label,
               trained_w, 'Test')
        plt.tight_layout()
    plt.show()