Example #1
0
def cross_validation(t, X, J, seed, lamb, istraining):
    # concate t and X and named it X' and go to next step
    X_prime = np.column_stack((t, X))
    X_prime = X_prime.tolist()
    trainning = X_prime
    # random split X' to J fold -> A list of list of list
    total_length = len(trainning)
    fold = int(total_length / J)
    random.Random(seed).shuffle(trainning)
    generator = (trainning[i:i + fold] for i in range(0, len(trainning), fold))
    master_fold_list = list(generator)
    MSE_list = list()
    # loop on index of each fold:
    for v_index in range(len(master_fold_list)):
        # set current train and test
        test = np.array(master_fold_list[v_index])
        temp = master_fold_list[:v_index] + master_fold_list[v_index + 1:]
        train = np.array(helper_depack(temp))
        t_test = test[:, 0]
        t_train = train[:, 0]
        X_test = test[:, 1:]
        X_train = train[:, 1:]
        # put train to ols_coefficent_prediction_lamda (X,t,lamb) and get w
        w = ols_main_vector.ols_coefficent_prediction_lamda(
            X_train, t_train, lamb)
        # generate t_hat by calling generate_predition_vector(x,w) where w from previous step and x is test set
        t_hat = ols_main_vector.generate_predition_vector(X_test, w)
        # use MSE function to caluclate (t,t_hat) for error rate
        socre = mean_squared_error(t_test, t_hat)
        MSE_list.append(socre)
    # keep track of each loop's MSE and return the mean and standard deviation across folds of the MSE.
    std = statistics.stdev(MSE_list)
    mean = statistics.mean(MSE_list)
    print(std, mean)
    return mean, std
Example #2
0
def compare(master_trainning_x, master_trainning_t, master_testing_x,
            master_testing_t, D, lamb):
    result = list()
    result.append(0)
    for power in range(1, D + 1):
        master_traiining_X = ols_main_vector.creates_predictor_matrix(
            master_trainning_x, power)
        w = ols_main_vector.ols_coefficent_prediction_lamda(
            master_traiining_X, master_trainning_t, lamb)
        t_hat = ols_main_vector.generate_predition_vector(master_testing_x, w)
        error = cross.mean_squared_error(master_testing_t, t_hat)
        result.append(error)
    return result