def main():
    # Number of possible degrees to be tested
    K = 30
    data_path = 'data_linreg.json'

    # Load the data
    f = open(data_path, 'r')
    data = json.load(f)
    for k, v in data.items():
        data[k] = np.array(v).reshape((len(v), 1))

    # Init vectors storing MSE (Mean square error) values of each sets at each degrees
    mse_train = np.zeros(K)
    mse_val = np.zeros(K)
    mse_test = np.zeros(K)
    theta_list = np.zeros(K, dtype=object)
    degrees = np.arange(K) + 1

    # Compute the MSE values
    for i in range(K):
        theta_list[i], mse_train[i], mse_val[i], mse_test[
            i] = poly.train_and_test(data, degrees[i])

    i_best = np.argmin(mse_val)
    best_degree = degrees[i_best]
    best_theta = theta_list[i_best]

    # Plot the training error as a function of the degrees
    plot_errors(i_best, degrees, mse_train, mse_val, mse_test)
    plot_poly(data, best_degree, best_theta)
    plt.show()
Esempio n. 2
0
def main():
    # Number of possible degrees to be tested
    K = 30
    data_path = 'data_linreg.json'

    # Load the data
    f = open(data_path, 'r')
    data = json.load(f)
    for k, v in data.items():
        data[k] = np.array(v).reshape((len(v), 1))

    # Init vectors storing MSE (Mean squared error) values of each set at each degrees
    mse_train = np.zeros(K)
    mse_val = np.zeros(K)
    mse_test = np.zeros(K)
    theta_list = np.zeros(K, dtype=object)
    degrees = np.arange(K) + 1

    # Compute the MSE values
    for i in range(K):
        theta_list[i], mse_train[i], mse_val[i], mse_test[i] = poly.train_and_test(data, degrees[i])

    ######################
    #
    # TODO
    #
    # Find the best degree that minimizes the validation error.
    # Store it in the variable i_best for plotting the results
    #
    # TIPs:
    # - use the argmin function of numpy
    # - the code above is already giving the vectors of errors
    i_best_val = np.argmin(mse_val) # TODO: Change this
    best_degree_val = degrees[i_best_val]
    best_theta_val = theta_list[i_best_val]

    i_best_tr = np.argmin(mse_train) # TODO: Change this
    best_degree_tr = degrees[i_best_tr]
    best_theta_tr = theta_list[i_best_tr]

    #
    # END TODO
    ######################

    # Plot the training error as a function of the degrees
    plot_errors(i_best_val, degrees, mse_train, mse_val, mse_test)
    plot_poly(data, best_degree_val, best_theta_val)
    plt.show()

    plot_errors(i_best_tr, degrees, mse_train, mse_val, mse_test)
    plot_poly(data, best_degree_tr, best_theta_tr)
    plt.show()
Esempio n. 3
0
def main():
    # Set the degree of the polynomial expansion

    degree = 1
    degrees = []

    for j in range(30):
        degrees.append(j)

    for i in range(1, 31):

        degree = i

        data_path = 'data_linreg.json'

        # Load the data
        f = open(data_path, 'r')
        data = json.load(f)
        for k, v in data.items():
            data[k] = np.array(v).reshape((len(v), 1))

        # Print the training and testing errors
        theta, err_train, err_val, err_test = poly.train_and_test(data, degree)
        print('Training error {} \t Validation error {} \t Testing error {} '.
              format(err_train, err_val, err_test))

        if (degree == 1):
            lowest = err_train

        if (lowest > err_train):
            lowest = err_train
            lowest_degree = degree
            lowest_theta = theta


#def plot_poly(data, degree, theta_opt, n_line_precision=100):
#def plot_errors(i_best, degrees, mse_train, mse_val, mse_test):
    plot_errors(13, degrees, err_train, err_val, err_test)
    plt.show()