def test_get_feature_vector_02(self): data_feed = RadialBasisFunctions(self.training_data, basis_function='linear') output = data_feed.get_feature_vector() expected_dict = {0: 0, 1: 0} self.assertDictEqual(expected_dict, output.extract_values()) print()
def test_r2_distance(self): u = np.array([[3.6, 7.2]]) data_feed = RadialBasisFunctions(self.training_data[0:10, :], basis_function='linear') output = data_feed.r2_distance(u) expected_output = np.array([ 8.049844719, 5.9203040462, 4.2190046219, 3.6124783736, 4.5607017004, 7.2835430939, 4.8270073545, 2.4596747752, 1.1401754251, 3.0083217913 ]) np.testing.assert_almost_equal(expected_output, output, decimal=6)
def test_rbf_predict_output_03(self): data_feed = RadialBasisFunctions(self.training_data[0:3, :], basis_function='gaussian') x_test = np.array([[0, 7.5]]) opt_wts = np.array([[2], [1], [3]]) lambda_reg = 0 reg = 1 distance_vec = np.array([[7.5, 5, 2.5]]) expected_output = np.matmul(np.exp(-(distance_vec**reg)**2), opt_wts) output = data_feed.rbf_predict_output(opt_wts, x_test, data_feed.centres, reg, lambda_reg) self.assertEqual(expected_output, output) print()
def test_bfgs_parameter_optimization_01(self): input_array = np.array([[0, 1], [1, 4], [2, 9], [3, 16], [4, 25], [5, 36], [6, 49], [7, 64], [8, 81], [9, 100]]) x = input_array[:, 0] y = input_array[:, 1] x_vector = np.zeros((x.shape[0], 3)) x_vector[:, 0] = x[:, ]**2 x_vector[:, 1] = x[:, ] x_vector[:, 2] = 1 expected_value = np.array([[1.], [2.], [1.]]).reshape(3, ) data_feed = RadialBasisFunctions(self.test_data, basis_function='linear', solution_method='bfgs') output_1 = data_feed.bfgs_parameter_optimization(x_vector, y) self.assertEqual(data_feed.solution_method, 'bfgs') np.testing.assert_array_equal(expected_value, np.round(output_1, 4))
def test_inverse_multiquadric_basis_transformation(self): d_vec = np.array([[0, 0], [5e-6, 7e-6], [0.005, 0.007], [0.05, 0.07], [0.5, 0.7], [5, 7], [50, 70]]) shape_list = [0.001, 1, 1000] expected_output_1 = 1 / np.sqrt(((d_vec * shape_list[0])**2) + 1) expected_output_2 = 1 / np.sqrt(((d_vec * shape_list[1])**2) + 1) expected_output_3 = 1 / np.sqrt(((d_vec * shape_list[2])**2) + 1) output_1 = RadialBasisFunctions.inverse_multiquadric_basis_transformation( d_vec, shape_list[0]) output_2 = RadialBasisFunctions.inverse_multiquadric_basis_transformation( d_vec, shape_list[1]) output_3 = RadialBasisFunctions.inverse_multiquadric_basis_transformation( d_vec, shape_list[2]) np.testing.assert_array_equal(expected_output_1, output_1) np.testing.assert_array_equal(expected_output_2, output_2) np.testing.assert_array_equal(expected_output_3, output_3)
def test_gaussian_basis_transformation(self): d_vec = np.array([[0, 0], [5e-6, 7e-6], [0.005, 0.007], [0.05, 0.07], [0.5, 0.7], [5, 7], [50, 70]]) shape_list = [0.001, 1, 1000] expected_output_1 = np.exp(-1 * ((d_vec * shape_list[0])**2)) expected_output_2 = np.exp(-1 * ((d_vec * shape_list[1])**2)) expected_output_3 = np.exp(-1 * ((d_vec * shape_list[2])**2)) output_1 = RadialBasisFunctions.gaussian_basis_transformation( d_vec, shape_list[0]) output_2 = RadialBasisFunctions.gaussian_basis_transformation( d_vec, shape_list[1]) output_3 = RadialBasisFunctions.gaussian_basis_transformation( d_vec, shape_list[2]) np.testing.assert_array_equal(expected_output_1, output_1) np.testing.assert_array_equal(expected_output_2, output_2) np.testing.assert_array_equal(expected_output_3, output_3)
def test_initialization_rbfs_03(self): sample_points = np.array([[0, 1], [1, 4], [2, 9], [3, 16], [4, 25], [5, 36], [6, 49], [7, 64], [8, 81], [9, 100]]) with self.assertRaises(ValueError): RadialBasisFunctions(self.training_data.tolist(), basis_function='linear')
def test_bfgs_parameter_optimization_02(self): x = self.training_data[:, :-1] y = self.training_data[:, -1] x_vector = np.zeros((x.shape[0], 6)) x_vector[:, 0] = x[:, 0]**2 x_vector[:, 1] = x[:, 1]**2 x_vector[:, 2] = x[:, 0] x_vector[:, 3] = x[:, 1] x_vector[:, 4] = x[:, 1] * x[:, 0] x_vector[:, 5] = 1 expected_value = np.array([[1.], [1.], [2.], [2.], [0.], [2.]]).reshape(6, ) data_feed = RadialBasisFunctions(self.full_data, solution_method='bfgs') output_1 = data_feed.bfgs_parameter_optimization(x_vector, y) self.assertEqual(data_feed.solution_method, 'bfgs') np.testing.assert_array_equal(expected_value, np.round(output_1, 4))
def test_r2_calculation_02(self): y_actual = np.array([[1], [4], [9], [16], [25], [36], [49], [64], [81], [100]]) y_pred = y_actual * 1.50 expected_output = 0.3974358974 # Evaluated in Excel output = RadialBasisFunctions.r2_calculation(y_actual, y_pred) self.assertAlmostEqual(expected_output, output, places=7) print()
def test_initialization_rbfs_02(self): data_feed = RadialBasisFunctions(self.training_data, 'linear') expected_output_1 = self.training_data[:, :-1] expected_output_2 = self.training_data[:, -1].reshape( expected_output_1.shape[0], 1) expected_output_3 = expected_output_1 expected_output_4 = list(range(self.training_data.shape[1] - 1)) np.testing.assert_array_equal(expected_output_1, data_feed.x_data) np.testing.assert_array_equal(expected_output_2, data_feed.y_data) np.testing.assert_array_equal(expected_output_3, data_feed.centres) self.assertListEqual(expected_output_4, data_feed.x_data_columns)
def test_initialization_rbfs_01(self): data_feed = RadialBasisFunctions(self.full_data, 'linear') expected_output_1 = self.full_data.values[:, :-1] expected_output_2 = self.full_data.values[:, -1].reshape( expected_output_1.shape[0], 1) expected_output_3 = expected_output_1 expected_output_4 = list(self.full_data.columns)[:-1] np.testing.assert_array_equal(expected_output_1, data_feed.x_data) np.testing.assert_array_equal(expected_output_2, data_feed.y_data) np.testing.assert_array_equal(expected_output_3, data_feed.centres) self.assertListEqual(expected_output_4, data_feed.x_data_columns)
def test_loo_error_estimation_with_rippa_method_02(self): reg_param = 0.1 shape_factor = 1 expected_x = np.ones( (self.training_data.shape[0], self.training_data.shape[0])) + (reg_param * np.eye( self.training_data.shape[0], self.training_data.shape[0])) expected_inverse_x = np.diag(np.linalg.pinv(expected_x)) expected_radial_weights = 500 * np.ones( (self.training_data.shape[0], 1)) expected_errors = np.linalg.norm( expected_radial_weights / (expected_inverse_x.reshape(expected_inverse_x.shape[0], 1))) data_feed = RadialBasisFunctions(self.training_data, solution_method='pyomo') _, output_1, output_2 = data_feed.loo_error_estimation_with_rippa_method( shape_factor, reg_param) self.assertEqual(output_1, np.linalg.cond(expected_x)) np.testing.assert_array_equal(output_2, expected_errors) print()
def test_pyomo_optimization_02(self): x = self.training_data[:, :-1] y = self.training_data[:, -1] x_vector = np.zeros((x.shape[0], 6)) x_vector[:, 0] = x[:, 0]**2 x_vector[:, 1] = x[:, 1]**2 x_vector[:, 2] = x[:, 0] x_vector[:, 3] = x[:, 1] x_vector[:, 4] = x[:, 1] * x[:, 0] x_vector[:, 5] = 1 expected_value = np.array([[1.], [1.], [2.], [2.], [0.], [2.]]) output_1 = RadialBasisFunctions.pyomo_optimization(x_vector, y) np.testing.assert_array_equal(expected_value, np.round(output_1, 4))
def test_pyomo_optimization_01(): # Create x vector for ax2 + bx + c: x data supplied in x_vector input_array = np.array([[0, 1], [1, 4], [2, 9], [3, 16], [4, 25], [5, 36], [6, 49], [7, 64], [8, 81], [9, 100]]) x = input_array[:, 0] y = input_array[:, 1] x_vector = np.zeros((x.shape[0], 3)) x_vector[:, 0] = x[:, ]**2 x_vector[:, 1] = x[:, ] x_vector[:, 2] = 1 expected_value = np.array([[1.], [2.], [1.]]) output_1 = RadialBasisFunctions.pyomo_optimization(x_vector, y) np.testing.assert_array_equal(expected_value, np.round(output_1, 4))
def test_explicit_linear_algebra_solution_01(self): input_array = np.array([[0, 1], [1, 4], [2, 9], [3, 16], [4, 25], [5, 36], [6, 49], [7, 64], [8, 81], [9, 100]]) x = input_array[:, 0] y = input_array[:, 1] x_vector = np.zeros((x.shape[0], 3)) x_vector[:, 0] = x[:, ]**2 x_vector[:, 1] = x[:, ] x_vector[:, 2] = 1 expected_value = np.array([[1.], [2.], [1.]]).reshape(3, ) output_1 = RadialBasisFunctions.explicit_linear_algebra_solution( x_vector, y) np.testing.assert_array_equal(expected_value, np.round(output_1, 4))
def test_explicit_linear_algebra_solution_02(self): x = self.training_data[:, :-1] y = self.training_data[:, -1] x_vector = np.zeros((x.shape[0], 6)) x_vector[:, 0] = x[:, 0]**2 x_vector[:, 1] = x[:, 1]**2 x_vector[:, 2] = x[:, 0] x_vector[:, 3] = x[:, 1] x_vector[:, 4] = x[:, 1] * x[:, 0] x_vector[:, 5] = 1 expected_value = np.array([[1.], [1.], [2.], [2.], [0.], [2.]]).reshape(6, ) output_1 = RadialBasisFunctions.explicit_linear_algebra_solution( x_vector, y) np.testing.assert_array_equal(expected_value, np.round(output_1, 4))
def test_cost_function_02(self): x = self.training_data[:, :-1] y = self.training_data[:, -1] x_data_nr = x.shape[0] x_data_nc = 6 x_vector = np.zeros((x_data_nr, x_data_nc)) x_vector[:, 0] = 1 x_vector[:, 1] = x[:, 0] x_vector[:, 2] = x[:, 1] x_vector[:, 3] = x[:, 0]**2 x_vector[:, 4] = x[:, 1]**2 x_vector[:, 5] = x[:, 0] * x[:, 1] theta = np.array([[4.5], [3], [3], [1], [1], [0]]) expected_value = 90.625 # Calculated externally as sum(dy^2) / 2m output_1 = RadialBasisFunctions.cost_function(theta, x_vector, y) self.assertEqual(output_1, expected_value)
def test_cost_function_03(self): x = self.training_data[:, :-1] y = self.training_data[:, -1] x_data_nr = x.shape[0] x_data_nc = 6 x_vector = np.zeros((x_data_nr, x_data_nc)) x_vector[:, 0] = 1 x_vector[:, 1] = x[:, 0] x_vector[:, 2] = x[:, 1] x_vector[:, 3] = x[:, 0]**2 x_vector[:, 4] = x[:, 1]**2 x_vector[:, 5] = x[:, 0] * x[:, 1] theta = np.array([[2], [2], [2], [1], [1], [0]]) expected_value = 0 output_1 = RadialBasisFunctions.cost_function(theta, x_vector, y) self.assertEqual(output_1, expected_value)
def test_gradient_function_01(self): x = self.training_data[:, :-1] y = self.training_data[:, -1] x_data_nr = x.shape[0] x_data_nc = 6 x_vector = np.zeros((x_data_nr, x_data_nc)) x_vector[:, 0] = 1 x_vector[:, 1] = x[:, 0] x_vector[:, 2] = x[:, 1] x_vector[:, 3] = x[:, 0]**2 x_vector[:, 4] = x[:, 1]**2 x_vector[:, 5] = x[:, 0] * x[:, 1] theta = np.zeros((x_data_nc, )) expected_value = np.array([[-97], [-635], [-635], [-5246.875], [-5246.875], [-3925]]) expected_value = expected_value.reshape(expected_value.shape[0], ) output_1 = RadialBasisFunctions.gradient_function(theta, x_vector, y) np.testing.assert_equal(output_1, expected_value)
def test_error_calculation_01(self): x = self.training_data[:, :-1] y = self.training_data[:, -1].reshape(self.training_data.shape[0], 1) x_data_nr = x.shape[0] x_data_nc = 6 x_vector = np.zeros((x_data_nr, x_data_nc)) x_vector[:, 0] = 1 x_vector[:, 1] = x[:, 0] x_vector[:, 2] = x[:, 1] x_vector[:, 3] = x[:, 0]**2 x_vector[:, 4] = x[:, 1]**2 x_vector[:, 5] = x[:, 0] * x[:, 1] theta = np.zeros((x_data_nc, 1)) expected_value_1 = 2 * 6613.875 # Calculated externally as sum(y^2) / m expected_value_2 = expected_value_1**0.5 output_1, output_2, _ = RadialBasisFunctions.error_calculation( theta, x_vector, y) self.assertEqual(output_1, expected_value_1) self.assertEqual(output_2, expected_value_2)
def test_error_calculation_02(self): x = self.training_data[:, :-1] y = self.training_data[:, -1].reshape(self.training_data.shape[0], 1) x_data_nr = x.shape[0] x_data_nc = 6 x_vector = np.zeros((x_data_nr, x_data_nc)) x_vector[:, 0] = 1 x_vector[:, 1] = x[:, 0] x_vector[:, 2] = x[:, 1] x_vector[:, 3] = x[:, 0]**2 x_vector[:, 4] = x[:, 1]**2 x_vector[:, 5] = x[:, 0] * x[:, 1] theta = np.array([[4.5], [3], [3], [1], [1], [0] ]) # coefficients in (x1 + 1.5)^2 + (x2 + 1.5) ^ 2 expected_value_1 = 2 * 90.625 # Calculated externally as sum(dy^2) / 2m expected_value_2 = expected_value_1**0.5 output_1, output_2, _ = RadialBasisFunctions.error_calculation( theta, x_vector, y) self.assertEqual(output_1, expected_value_1) self.assertEqual(output_2, expected_value_2)
def test_error_calculation_03(self): x = self.training_data[:, :-1] y = self.training_data[:, -1].reshape(self.training_data.shape[0], 1) x_data_nr = x.shape[0] x_data_nc = 6 x_vector = np.zeros((x_data_nr, x_data_nc)) x_vector[:, 0] = 1 x_vector[:, 1] = x[:, 0] x_vector[:, 2] = x[:, 1] x_vector[:, 3] = x[:, 0]**2 x_vector[:, 4] = x[:, 1]**2 x_vector[:, 5] = x[:, 0] * x[:, 1] theta = np.array( [[2], [2], [2], [1], [1], [0]]) # Actual coefficients in (x1 + 1)^2 + (x2 + 1) ^ 2 expected_value_1 = 2 * 0 # Value should return zero for exact solution expected_value_2 = expected_value_1**0.5 output_1, output_2, _ = RadialBasisFunctions.error_calculation( theta, x_vector, y) self.assertEqual(output_1, expected_value_1) self.assertEqual(output_2, expected_value_2)
def test_gradient_function_03(self): x = self.training_data[:, :-1] y = self.training_data[:, -1] x_data_nr = x.shape[0] x_data_nc = 6 x_vector = np.zeros((x_data_nr, x_data_nc)) x_vector[:, 0] = 1 x_vector[:, 1] = x[:, 0] x_vector[:, 2] = x[:, 1] x_vector[:, 3] = x[:, 0]**2 x_vector[:, 4] = x[:, 1]**2 x_vector[:, 5] = x[:, 0] * x[:, 1] theta = np.array( [[2], [2], [2], [1], [1], [0]]) # Actual coefficients in (x1 + 1)^2 + (x2 + 1) ^ 2 theta = theta.reshape(theta.shape[0], ) expected_value = np.array([[0], [0], [0], [0], [0], [0] ]) # Calculated externally: see Excel sheet expected_value = expected_value.reshape(expected_value.shape[0], ) output_1 = RadialBasisFunctions.gradient_function(theta, x_vector, y) np.testing.assert_equal(output_1, expected_value)
def test_basis_generation(self): # Pair-wise distances for the first three samples of training_data distance_array = np.array([[0, 2.5, 5], [2.5, 0, 2.5], [5, 2.5, 0]]) # Linear data_feed_01 = RadialBasisFunctions(self.training_data[0:3, :], basis_function='linear') expected_output_1 = distance_array output_1 = data_feed_01.basis_generation(2) np.testing.assert_array_equal(expected_output_1, output_1) # Cubic data_feed_02 = RadialBasisFunctions(self.training_data[0:3, :], basis_function='cubic') expected_output_2 = distance_array**3 output_2 = data_feed_02.basis_generation(2) np.testing.assert_array_equal(expected_output_2, output_2) # Spline data_feed_03 = RadialBasisFunctions(self.training_data[0:3, :], basis_function='spline') expected_output_3 = np.nan_to_num(distance_array**2 * np.log(distance_array)) output_3 = data_feed_03.basis_generation(2) np.testing.assert_array_equal(expected_output_3, output_3) # Gaussian data_feed_04 = RadialBasisFunctions(self.training_data[0:3, :], basis_function='gaussian') shape_value = 2 expected_output_4 = np.exp(-1 * ((distance_array * shape_value)**2)) output_4 = data_feed_04.basis_generation(shape_value) np.testing.assert_array_equal(expected_output_4, output_4) # Multiquadric data_feed_05 = RadialBasisFunctions(self.training_data[0:3, :], basis_function='mq') shape_value = 2 expected_output_5 = np.sqrt(((distance_array * shape_value)**2) + 1) output_5 = data_feed_05.basis_generation(shape_value) np.testing.assert_array_equal(expected_output_5, output_5) # Inverse multiquadric data_feed_06 = RadialBasisFunctions(self.training_data[0:3, :], basis_function='imq') shape_value = 2 expected_output_6 = 1 / np.sqrt(( (distance_array * shape_value)**2) + 1) output_6 = data_feed_06.basis_generation(shape_value) np.testing.assert_array_equal(expected_output_6, output_6) print()
def test_initialization_rbfs_20(self): reg = False output = RadialBasisFunctions(self.test_data, regularization=reg) self.assertEqual(output.regularization, reg)
def test_initialization_rbfs_21(self): default_reg = True output = RadialBasisFunctions(self.test_data) self.assertEqual(output.regularization, default_reg)
def test_initialization_rbfs_23(self): reg = 5 with self.assertRaises(Exception): RadialBasisFunctions(self.test_data, regularization=reg)
def test_thin_plate_spline_transformation(self): d_vec = np.array([[0, 0], [5e-6, 7e-6], [0.005, 0.007], [0.05, 0.07], [0.5, 0.7], [5, 7], [50, 70]]) expected_output = np.nan_to_num(d_vec**2 * np.log(d_vec)) output = RadialBasisFunctions.thin_plate_spline_transformation(d_vec) np.testing.assert_array_equal(expected_output, output)
def test_cubic_transformation(self): d_vec = np.array([[0, 0], [5e-6, 7e-6], [0.005, 0.007], [0.05, 0.07], [0.5, 0.7], [5, 7], [50, 70]]) expected_output = d_vec**3 output = RadialBasisFunctions.cubic_transformation(d_vec) np.testing.assert_array_equal(expected_output, output)
def test_linear_transformation(self): d_vec = np.array([[0, 0], [5e-6, 7e-6], [0.005, 0.007], [0.05, 0.07], [0.5, 0.7], [5, 7], [50, 70]]) output_1 = RadialBasisFunctions.linear_transformation(d_vec) np.testing.assert_array_equal(d_vec, output_1)