def test_distance_manhattan(self): x = Vector(1, 2) y = Vector(2, 1) expected_distance_x_y = 2 self.assertEqual(expected_distance_x_y, x.distance(y, algorithm="manhattan"))
def test_distance_euclidean(self): x = Vector(1, 2) y = Vector(2, 1) expected_distance_x_y = sqrt(2) self.assertEqual(expected_distance_x_y, x.distance(y, algorithm="euclidean"))
def test_tanh(self): x = Vector(1, 2, 3) tanh = x.tanh() expected_tanh = Vector(0.7615941559557646, 0.9640275800758169, 0.9950547536867307) self.assertEqual(expected_tanh, tanh)
def test_vector_equation(self): a = Vector(1, 2, 3) b = Vector(3, 2, 1) expected = "eq = [1.0 2.0 3.0] + t[2.0 0.0 -2.0]" self.assertEqual(expected, a.vector_eq(b))
def test_flatten(self): x = [Vector(2, 3), 5, (3, 5), [3, 5]] y = Vector.flatten(x) expected_y = Vector(2, 3, 5, 3, 5, 3, 5) self.assertEqual(expected_y, y)
def test_corr(self): a = Vector(2, 3, 5) b = Vector(6, 5, 4) c = round(a.corr(b), 2) expected_c = -0.98 self.assertEqual(c, expected_c)
def test_vectors_dressing(self): # Test vector 'dressing' a = Vector(1, 1, 1, 1) b = Vector(1, 2) expected_return_from_a_dress_b = ([1, 1, 1, 1], [1, 2, 0, 0]) self.assertEqual(expected_return_from_a_dress_b, a._dress_(b))
def test_join(self): a = Vector(1, 2, 3) expected_join = "1.0 2.0 3.0" actual_join = a.join() self.assertEqual(expected_join, actual_join)
def test_power(self): a = Vector(1, 2, 3) expected_power = Vector(1, 8, 27) power = a.pow(3) self.assertEqual(expected_power, power)
def test_mse(self): a = Vector(1, 2, 3) b = Vector(2, 2, 3) expected_mse = 1 / 3 mse = a.mse(b) self.assertEqual(expected_mse, mse)
def test_describe(self): x = Vector(1, 2) expected_x_description = ("A 2-dimensional vector. " "[ Memory Size 190 bytes ]") actual_x_description = x.describe() self.assertEqual(expected_x_description, actual_x_description)
def test_mae(self): a = Vector(1, 2, 3) b = Vector(2, 2, 3) expected_mae = 1 / 3 mae = a.mae(b) self.assertEqual(expected_mae, mae)
def test_least_squares(self): a = Vector(1, 2, 3) b = Vector(2, 2, 3) expected_lsq = 1 lsq = a.leastsq(b) self.assertEqual(expected_lsq, lsq)
def test_least_absolute_deviations(self): a = Vector(1, 2, 3) b = Vector(2, 2, 3) expected_lad = 1 lad = a.leastdev(b) self.assertEqual(expected_lad, lad)
def test_jaccard(self): a = Vector(1, 2, 3, 4) b = Vector(1, 2, 4, 4) expected_jaccard = round(3 / 4, 2) jaccard = a.jaccard(b) self.assertEqual(expected_jaccard, jaccard)
def test_element_wise_division(self): x = Vector(1, 2) y = Vector(2, 1) expected_x_div_y = Vector(0.5, 2) x_div_y = x.ediv(y) self.assertEqual(expected_x_div_y, x_div_y)
def test_element_wise_multiplication(self): x = Vector(1, 2) y = Vector(2, 1) expected_xy = Vector(2, 2) xy = x.emul(y) self.assertEqual(expected_xy, xy)
def test_mbe(self): a = Vector(1, 2, 3) b = Vector(2, 2, 3) expected_mbe = -1 / 3 mbe = a.mbe(b) self.assertEqual(expected_mbe, mbe)
def test_cost_function(self): a = Vector(1, 2, 3) b = Vector(0.5, 1, 1.5) cost = a.cost(b) expected_cost = 3.5 / 6 self.assertEqual(expected_cost, cost)
def test_softmax(self): a = Vector(1, 2, 3) b = a.softmax() expected_soft = Vector(0.09003057317038046, 0.24472847105479767, 0.6652409557748219) self.assertEqual(b, expected_soft)
def test_sigmoid(self): a = Vector(1, 2, 3) b = a.sigmoid() expected_sig = Vector(0.7310585786300049, 0.8807970779778823, 0.9525741268224334) self.assertEqual(b, expected_sig)
def test_rsquare(self): a = Vector(1, 2, 3) b = Vector(2, 2, 3) expected_r2 = 1 - ((1 / 3) / (2 / 3)) r2 = a.rsquare(b) self.assertEqual(expected_r2, r2)
def test_rmse(self): a = Vector(1, 2, 3) b = Vector(2, 2, 3) expected_rmse = sqrt(1 / 3) rmse = a.rmse(b) self.assertEqual(expected_rmse, rmse)
def test_dimensions(self): x = Vector(1, 2) z = Vector(1, 2, 3) expected_x_dimensions = 2 expected_z_dimensions = 3 self.assertEqual(expected_x_dimensions, x.dimensions) self.assertEqual(expected_z_dimensions, z.dimensions)
def test_sum_of_vectors(self): a = Vector(1, 1, 1) b = Vector(2, 2, 2) c = Vector(3, 3, 3) expected_sum = Vector(6, 6, 6) sum = Vector.sum([a, b, c]) self.assertEqual(expected_sum, sum)
def test_string_representation(self): a = Vector(1, 2, 3) self.assertEqual("Vector(1.0 2.0 3.0)", str(a)) self.assertEqual( "The right representation must be: Vector(1.0 2.0 3.0)", "The right representation must be: {}".format(a)) self.assertEqual("1.0 2.0 3.0", a.__repr__(matrixmode=True))
def test_dot_product(self): x = Vector(1, 2) y = Vector(2, 1) z = Vector(1, 2, 3) expected_dot_product_x_y = 4 expected_dot_product_x_z = 5 self.assertEqual(expected_dot_product_x_y, x.dotmul(y)) self.assertEqual(expected_dot_product_x_z, x.dotmul(z))
def test_subtraction(self): expected_x_minus_y = Vector(-1, 1) expected_y_minus_x = Vector(1, -1) # Test subtraction using (-) operator self.assertEqual(expected_x_minus_y, (x - y)) self.assertEqual(expected_y_minus_x, (y - x)) # Test subtraction using .subtract() method self.assertEqual(expected_x_minus_y, x.subtract(y)) self.assertEqual(expected_y_minus_x, y.subtract(x))
def test_scalar_multiplication(self): a = Vector(1, 2, 3) expected_result_from_scalar_mul_by_two = Vector(2, 4, 6) # Test .smul() self.assertEqual(expected_result_from_scalar_mul_by_two, a.smul(2)) # Test scalar multiplication with (*) operator # Test multiplication from right and left sides self.assertEqual(expected_result_from_scalar_mul_by_two, 2 * a) self.assertEqual(expected_result_from_scalar_mul_by_two, a * 2)
def test_random_vector(self): seed = 3 dimensions = 2 random.seed(seed) a = Vector.random(dimensions, seed) expected_a = Vector( [random.random() * 2 - 1 for i in range(dimensions)]) self.assertEqual(2, a.dimensions) self.assertTrue(isinstance(a, Vector))