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):
                mean = Mean()
                mean_b = mean.calculate(point_b)
                mean_a = mean.calculate(point_a)
                try:
                    point_b_normalized = [(float(c) - mean_b) for c in point_b]
                    point_a_normalized = [(float(c) - mean_a) for c in point_a]
                    dividend = sum(map(operator.mul, point_a_normalized, point_b_normalized))
                    point_b_squared_normalized = sum([(float(c) - mean_b) ** 2 for c in point_b])
                    point_a_squared_normalized = sum([(float(c) - mean_a) ** 2 for c in point_a])
                    divider = math.sqrt(point_b_squared_normalized) * math.sqrt(point_a_squared_normalized)
                    self._result = 1 - (dividend / divider)
                except ArithmeticError:
                    raise ArithmeticError("float division by zero")

            else:
                raise ArithmeticError("You cant calculate Correlation distance of array has different sizes.")

        else:
            raise ArithmeticError("You must enter two array to find Correlation distance.")
예제 #2
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):
             mean = Mean()
             mean_b = mean.calculate(point_b)
             mean_a = mean.calculate(point_a)
             try:
                 dividend = sum(
                     ((float(c) + mean_b - mean_a))**2
                     for c in map(operator.sub, point_a, point_b))
                 divider = 2 * (sum(
                     (float(c) - mean_a)**2 for c in point_a) + sum(
                         (float(c) - mean_b)**2 for c in point_b))
                 self._result = (dividend / divider)
             except ArithmeticError:
                 raise ArithmeticError("float division by zero")
         else:
             raise ArithmeticError(
                 "You cant calculate Normalized Squared Euclidean distance of array has different sizes."
             )
     else:
         raise ArithmeticError(
             "You must enter two array to find Normalized Squared Euclidean distance."
         )
예제 #3
0
    def test_algorithm_with_tuple(self):
        test_logger.debug("MeanTest - test_algorithm_with_tuple Starts")
        mean = Mean()
        data_list = [("a", 1), ("b", 2), ("c", 3), ( "d", 4), ("e", 5)]
        self.assertEquals(3, mean.calculate(data_list, is_tuple=True, index=1))

        data_list = [("a", "a", 1), ("b", "b", 2), ("c", "c", 3), ("d", "d", 4), ("e", "e", 5)]
        self.assertEquals(3.0, mean.calculate(data_list, is_tuple=True, index=2))
        test_logger.debug("MeanTest - test_algorithm_with_tuple Ends")
예제 #4
0
    def test_algorithm_with_tuple(self):
        test_logger.debug("MeanTest - test_algorithm_with_tuple Starts")
        mean = Mean()
        data_list = [("a", 1), ("b", 2), ("c", 3), ("d", 4), ("e", 5)]
        self.assertEquals(3, mean.calculate(data_list, is_tuple=True, index=1))

        data_list = [("a", "a", 1), ("b", "b", 2), ("c", "c", 3),
                     ("d", "d", 4), ("e", "e", 5)]
        self.assertEquals(3.0, mean.calculate(data_list,
                                              is_tuple=True,
                                              index=2))
        test_logger.debug("MeanTest - test_algorithm_with_tuple Ends")
예제 #5
0
    def __algorithm(self):
        try:
            mean = Mean()
            mean_value = mean.calculate(self._data)
            values = map(lambda x: (float(x) - mean_value), self._data)
            sum_formula = SumFormula()
            sum_of_powers = sum_formula.calculate(values, power=2)

            result = sum_of_powers / (self._n - 1)
            return round(result, 4)
        except:
            raise
예제 #6
0
    def __algorithm(self):
        try:
            mean = Mean()
            mean_value = mean.calculate(self._data)
            values = map(lambda x: (float(x) - mean_value), self._data)
            sum_formula = SumFormula()
            sum_of_powers = sum_formula.calculate(values, power=2)

            result = sum_of_powers / (self._n - 1)
            return round(result, 4)
        except:
            raise
예제 #7
0
 def test_algorithm_with_list(self):
     test_logger.debug("MeanTest - test_algorithm_with_list Starts")
     mean = Mean()
     data_list = [1, 2, 3, 4, 5]
     self.assertEquals(3, mean.calculate(data_list))
     data_list = [1, 2, 3, 4]
     self.assertEquals(2.5, mean.calculate(data_list))
     data_list = []
     with self.assertRaises(ZeroDivisionError) as context:
         mean.calculate(data_list)
     self.assertEqual("integer division or modulo by zero",
                      context.exception.message)
     test_logger.debug("MeanTest - test_algorithm_with_list Ends")
 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):
             mean = Mean()
             mean_b = mean.calculate(point_b)
             mean_a = mean.calculate(point_a)
             try:
                 dividend = sum(
                     ((float(c) + mean_b - mean_a )) ** 2 for c in map(operator.sub, point_a, point_b))
                 divider = 2 * (
                     sum((float(c) - mean_a ) ** 2 for c in point_a) + sum(
                         (float(c) - mean_b ) ** 2 for c in point_b))
                 self._result = (dividend / divider)
             except ArithmeticError:
                 raise ArithmeticError("float division by zero")
         else:
             raise ArithmeticError(
                 "You cant calculate Normalized Squared Euclidean distance of array has different sizes.")
     else:
         raise ArithmeticError("You must enter two array to find Normalized Squared Euclidean distance.")
예제 #9
0
 def test_algorithm_with_list(self):
     test_logger.debug("MeanTest - test_algorithm_with_list Starts")
     mean = Mean()
     data_list = [1, 2, 3, 4, 5]
     self.assertEquals(3, mean.calculate(data_list))
     data_list = [1, 2, 3, 4]
     self.assertEquals(2.5, mean.calculate(data_list))
     data_list = []
     with self.assertRaises(ZeroDivisionError) as context:
         mean.calculate(data_list)
     self.assertEqual("integer division or modulo by zero",
                      context.exception.message)
     test_logger.debug("MeanTest - test_algorithm_with_list Ends")