Esempio n. 1
0
def main():
    # Number of possible degrees to be tested
    K = 20
    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)
    n_centers = 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] = rbf.train_and_test(data, n_centers[i])

    i_best = np.argmin(mse_val)
    print(mse_test[i_best])
    #
    # TODO END
    ######################

    # Plot the training error as a function of the degrees
    plot_errors(i_best, n_centers, mse_train, mse_val, mse_test)
    plot_rbf(data, n_centers[i_best], theta_list[i_best])
    plt.show()
Esempio n. 2
0
def main():
    # Number of possible degrees to be tested
    K = 40
    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))

    ######################
    #
    # TODO
    #
    # Compute the arrays containing the Means Square Errors in all cases
    #
    # Find the degree that minimizes the validation error
    # Store it in the variable i_best for plotting the results
    #
    # TIP:
    # - You are invited to adapt the code you did for the polynomial  case
    # - use the argmin function of numpy
    #

    # 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)
    n_centers = 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] = rbf.train_and_test(data, n_centers[i])
    i_best = np.argmin(mse_val)
    #i_best = np.argmin(mse_test)
    #i_best = np.argmin(mse_train)

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

    # Plot the training error as a function of the degrees
    plot_errors(i_best, n_centers, mse_train, mse_val, mse_test)
    plot_rbf(data, n_centers[i_best], theta_list[i_best])
    plt.show()
Esempio n. 3
0
def main():
    # Set the n_centers of the polynomial expansion
    n_centers = 5

    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 = rbf.train_and_test(data, n_centers)
    print(
        'Training error {} \t Validation error {} \t Testing error {} '.format(
            err_train, err_val, err_test))

    plot_rbf(data, n_centers, theta)
    plt.show()