Exemplo n.º 1
0
    def _algorithm(self):
        if len(self._data) == 2:
            point_a = self._data[0]
            point_b = self._data[1]

            if len(point_a) == len(point_b):
                try:
                    dividend = sum(map(operator.mul, point_b, point_a))
                    divider = Norm.calculate(point_b) * Norm.calculate(point_a)
                    self._result = 1 - (dividend / divider)
                except ArithmeticError:
                    raise ArithmeticError("float division by zero")
            else:
                raise ArithmeticError("You cant calculate Cosine distance of array has different sizes.")
        else:
            raise ArithmeticError("You must enter two array to find Cosine distance.")
Exemplo n.º 2
0
    def test_algorithm(self):
        test_logger.debug("NormTest - test_algorithm Starts")
        data = [1, -2]
        norm = Norm.calculate(data)
        result = math.sqrt(5)
        self.assertEquals(result, norm)

        data = [1, 2, 3, 4]
        norm = Norm.calculate(data)
        self.assertEquals(5.477225575051661, norm)

        data = (5, 6, 7, 8)
        norm = Norm.calculate(data)
        self.assertEquals(13.19090595827292, norm)

        data = [3, 1, 4, 1]
        norm = Norm.calculate(data)
        self.assertEquals(5.196152422706632, norm)

        data = [3, 1, 5, 5, 5, 3]
        norm = Norm.calculate(data)
        self.assertEquals(9.695359714832659, norm)

        data = [3]
        norm = Norm.calculate(data)
        self.assertEquals(3.0, norm)

        data = [[3], [4, 5, 6]]
        with self.assertRaises(TypeError) as context:
            norm = Norm.calculate(data)
        self.assertEqual('float() argument must be a string or a number',
                         context.exception.message)

        data = [["a"], [4]]
        with self.assertRaises(TypeError) as context:
            norm = Norm.calculate(data)
        self.assertEqual("float() argument must be a string or a number",
                         context.exception.message)
        test_logger.debug("NormTest - test_algorithm Ends")
Exemplo n.º 3
0
    def test_algorithm(self):
        test_logger.debug("NormTest - test_algorithm Starts")
        data = [1, -2]
        norm = Norm.calculate(data)
        result = math.sqrt(5)
        self.assertEquals(result, norm)

        data = [1, 2, 3, 4]
        norm = Norm.calculate(data)
        self.assertEquals(5.477225575051661, norm)

        data = (5, 6, 7, 8)
        norm = Norm.calculate(data)
        self.assertEquals(13.19090595827292, norm)

        data = [3, 1, 4, 1]
        norm = Norm.calculate(data)
        self.assertEquals(5.196152422706632, norm)

        data = [3, 1, 5, 5, 5, 3]
        norm = Norm.calculate(data)
        self.assertEquals(9.695359714832659, norm)

        data = [3]
        norm = Norm.calculate(data)
        self.assertEquals(3.0, norm)

        data = [[3], [4, 5, 6]]
        with self.assertRaises(TypeError) as context:
            norm = Norm.calculate(data)
        self.assertEqual("float() argument must be a string or a number", context.exception.message)

        data = [["a"], [4]]
        with self.assertRaises(TypeError) as context:
            norm = Norm.calculate(data)
        self.assertEqual("float() argument must be a string or a number", context.exception.message)
        test_logger.debug("NormTest - test_algorithm Ends")