def test_mean_squared_error(): from linear_regression import mean_squared_error outputs = np.float32([[1, 2, 3, 4]]).T targets = np.float32([[1.1, 2.2, 3.3, 4.4]]).T actual = mean_squared_error(outputs, targets) desired = 0.07500000000000007 np.testing.assert_allclose(actual, desired, rtol=1e-3, atol=1e-3)
with open("../validation_set.json", "r") as rf_validation_set: data = json.load(rf_validation_set) X_validation_set_Adv, Y_validation_set_Adv, str_output = pf.generate_wordfeature_and_output(wordcount, data, False, 0, False, 0) #測試Closed form的性能 start_time = time.time() W_closed, str_to_write = lr.least_squares_estimate_linear_regression_alg(X_validation_set_Adv, Y_validation_set_Adv) closed_running_time = time.time() - start_time est_Y = np.dot(X_training_set_Adv, W_closed) trn_mse_closed, diff_AB, Sigma_Square_of_diff_AB = lr.mean_squared_error(est_Y, Y_training_set_Adv) est_Y = np.dot(X_validation_set_Adv, W_closed) val_mse_closed, diff_AB, Sigma_Square_of_diff_AB = lr.mean_squared_error(est_Y, Y_validation_set_Adv) #測試Gradient Descent的性能 list_T_Robbins_Monroe_pow = [] list_trn_mse = [] list_val_mse = [] list_elapsed_time = [] list_W_MSE = [] epsilon_power = -6 eta_power = 0 for T_Robbins_Monroe_pow in range(1, 8, 1):
str_for_statistics += "least_squares_estimate_linear_regression_alg:" + "\n\n" running_stat_item['desc'] = "least_squares_estimate_linear_regression_alg" str_for_statistics += "W_cf = \n" + str(W_cf) + "\n\n" running_stat_item['W'] = str(W_cf) str_for_statistics += "alg runtime = " + str(elapsed_time) + "\n" running_stat_item['alg_runtime'] = elapsed_time start_time = time.time() est_Y = np.dot(X_training_set_Adv, W_cf) tmp_mse, diff_AB, Sigma_Square_of_diff_AB = lr.mean_squared_error( est_Y, Y_training_set_Adv) #print("est_Y = \n", est_Y) #print("tmp_mse =", tmp_mse) elapsed_time = time.time() - start_time str_to_write = "tmp_mse for training set = " + str(tmp_mse) + "\n\n\n\n" str_to_write += "mean_squared_error elapsed_time = " + str( elapsed_time) + "\n" Sigma_Square_of_diff_AB = np.sort(Sigma_Square_of_diff_AB, axis=None, kind='mergesort') str_to_write += "Sigma_Square_of_diff_AB = \n" + str( Sigma_Square_of_diff_AB) + "\n" + str(
fout_t2r.write(str_to_write) print(str_to_write) str_to_write = "lr.least_squares_estimate_linear_regression_alg W = \n" + str( W) + "\n" elapsed_time = time.time() - start_time str_to_write += "elapsed_time = " + str(elapsed_time) + "\n" fout_t2r.write(str_to_write) print(str_to_write) #print("elapsed_time = ", elapsed_time) est_Y = np.dot(X_validation_set, W) tmp_mse = lr.mean_squared_error(est_Y, Y_validation_set) #print("est_Y = \n", est_Y) #print("tmp_mse =", tmp_mse) str_to_write = "est_Y = \n" + str(est_Y) + "\n" str_to_write += "tmp_mse = " + str(tmp_mse) + "\n" fout_t2r.write(str_to_write) print(str_to_write) # -2 ~ -8 epsilon_power = -2 # 1 ~ -3 beta_power = 1