Ejemplo n.º 1
0
def train_perceptron(kernel_name, kernel, learning_rate):
    """Train a perceptron with the given kernel.

    This function trains a perceptron with a given kernel and then
    uses that perceptron to make predictions.
    The output predictions are saved to src/output/p05_{kernel_name}_predictions.txt.
    The output plots are saved to src/output_{kernel_name}_output.pdf.

    Args:
        kernel_name: The name of the kernel.
        kernel: The kernel function.
        learning_rate: The learning rate for training.
    """
    train_x, train_y = util.load_csv('../data/ds5_train.csv')

    state = initial_state()

    for x_i, y_i in zip(train_x, train_y):
        update_state(state, kernel, learning_rate, x_i, y_i)

    test_x, test_y = util.load_csv('../data/ds5_train.csv')

    plt.figure(figsize=(12, 8))
    util.plot_contour(lambda a: predict(state, kernel, a))
    util.plot_points(test_x, test_y)
    plt.savefig('./output/p05_{}_output.pdf'.format(kernel_name))

    predict_y = [
        predict(state, kernel, test_x[i, :]) for i in range(test_y.shape[0])
    ]

    np.savetxt('./output/p05_{}_predictions'.format(kernel_name), predict_y)
Ejemplo n.º 2
0
def main():
    # plot
    DEBUG = False
    if DEBUG:
        from util import plot_points
        from matplotlib import pyplot as plt
        plt.figure()
        Xa, Ya = util.load_csv('../data/ds1_a.csv', add_intercept=False)
        Ya = (Ya == 1).astype(np.float)
        plot_points(Xa, Ya)

        plt.figure()
        Xb, Yb = util.load_csv('../data/ds1_b.csv', add_intercept=False)
        Yb = (Yb == 1).astype(np.float)
        plot_points(Xb, Yb)
        plt.show()
        import sys
        sys.exit()

    # print('==== Training model on data set A ====')
    # Xa, Ya = util.load_csv('../data/ds1_a.csv', add_intercept=True)
    # logistic_regression(Xa, Ya)

    print('\n==== Training model on data set B ====')
    Xb, Yb = util.load_csv('../data/ds1_b.csv', add_intercept=True)
    logistic_regression(Xb, Yb)
Ejemplo n.º 3
0
def train_perceptron(train_path, test_path, kernel_name, kernel,
                     learning_rate):
    """Train a perceptron with the given kernel.

    This function trains a perceptron with a given kernel and then uses that perceptron to make predictions.
    The output predictions are saved to src/output/p05_{kernel_name}_predictions.txt
    The output plots are saved to src/output_{kernel_name}_output.pdf

    Args:
        kernel_name: The name of the kernel
        kernel: The kernel function
        learning_rate: The learning rate for training
    """

    train_x, train_y = util.load_csv(train_path)
    test_x, test_y = util.load_csv(test_path)

    state = initial_state()

    for x_i, y_i in zip(train_x, train_y):
        update_state(state, kernel, learning_rate, x_i, y_i)

    plt.figure()
    util.plot_contour(lambda a: predict(state, kernel, a))
    util.plot_points(test_x, test_y)
    plt.title(
        f"Kernel: {kernel_name} || Test data, color corresponds to real label")
    plt.legend()
    plt.xlabel("x1")
    plt.ylabel("x2")

    y_pred = np.array(
        [predict(state, kernel, test_x[i, :]) for i in range(test_y.shape[0])])
    plt.figure()
    util.plot_points(test_x, y_pred)
    plt.title(
        f"Kernel: {kernel_name} || Test data, color corresponds to predicted label"
    )
    plt.legend()
    plt.xlabel("x1")
    plt.ylabel("x2")
    plt.show()
Ejemplo n.º 4
0
def train(pct, x_train, y_train, x_eval, y_eval):
    """Train a perceptron with the given kernel.

    This function trains a perceptron with a given kernel and then
    uses that perceptron to make predictions.
    The output predictions are saved to src/output/p05_{kernel_name}_predictions.txt.
    The output plots are saved to src/output_{kernel_name}_output.pdf.

    Args:
        kernel_name: The name of the kernel.
        kernel: The kernel function.
        learning_rate: The learning rate for training.
    """
    for x, y in zip(x_train, y_train):
        pct.update(x, y)

    plt.figure(figsize=(12, 8))
    util.plot_contour(lambda x: pct.predict(x))
    util.plot_points(x_eval, y_eval)
    plt.savefig('./output/p05_{}_output.pdf'.format(pct.kernel_name))

    y_pred = [pct.predict(x_eval[i, :]) for i in range(y_eval.shape[0])]
    np.savetxt('./output/p05_{}_predictions'.format(pct.kernel_name), y_pred)
Ejemplo n.º 5
0
def plot_clusters(key, location_type):
    util.plot_points(util.rearrange_data(get_cluster_coords(key, location_type)))
Ejemplo n.º 6
0
def plot_seeds(key):
    util.plot_points(util.rearrange_data(get_seed_coords(key)))