Esempio n. 1
0
    def test_similarity(self):
        actual = Vector.similarity(self.v0, self.v1)
        expected = 0.0
        self.assertAlmostEqual(actual, expected)

        actual = Vector.similarity(self.v1, self.v2)
        expected = 70.0 / (5.477225575 * 13.190905958)
        self.assertAlmostEqual(actual, expected)
def gradient(dataset: Dataset, rho: Vector) -> Vector:
    grad = Vector()
    for i in range(len(dataset.features)):
        part_deriv = 0
        for experiment in dataset:
            part_deriv += (experiment.output -
                           rho * experiment.inputs) * experiment.inputs[i]
        grad.append(-part_deriv)
    return grad
Esempio n. 3
0
    def test_length(self):
        actual = Vector.length(self.v0)
        expected = 0.0
        self.assertAlmostEqual(actual, expected)

        actual = Vector.length(self.v1)
        expected = 5.477225575
        self.assertAlmostEqual(actual, expected)

        actual = Vector.length(self.v2)
        expected = 13.190905958
        self.assertAlmostEqual(actual, expected)
Esempio n. 4
0
def calculate(how_ini, branch, args, path_branch):
    vector = Vector.Vector(len(args))
    if how_ini == 0:
        vector.initialize_with_zeros()
    elif how_ini < 4:
        vector.initialize_with_rd_small()
    elif how_ini < 7:
        vector.initialize_with_rd_medium()
    else:
        vector.initialize_with_rd()
    if (Default.AVM_DEF == 'IteratedPatternSearch'):
        pattern_search = PatternSearch.PatternSearch(Default.ACC_FACTOR_DEF)
        for i in range(vector.leng):
            pattern_search.setup(vector.vars, i, branch, path_branch)
            next = pattern_search.evaluate()
            last = ObjectiveVal.ObjectiveVal()
            cnt = 0
            while cnt < Default.MAX_ITERATIONS_DEF:
                cnt += 1
                pattern_search.initialize()
                if (pattern_search.explore()):
                    pattern_search.pattern_move()
                if (next.better_than(last) == False):
                    break
                last = next
                next = pattern_search.evaluate()
            if last.best_answer():
                return vector.vars
    return None
def fit_linear(dataset: Dataset,
               lambdaa=0.1,
               max_iter=10000,
               threshold=0.11) -> Tuple[Vector, float, int]:
    rho = Vector(*[i for i in range(len(dataset.features))])
    error = sqr_error(dataset, rho)
    prev_error = 0
    iter_count = 0
    while iter_count < max_iter and (prev_error == 0
                                     or abs(prev_error - error) > threshold):
        rho = rho - lambdaa * gradient(dataset, rho)
        prev_error, error = error, sqr_error(dataset, rho)
        iter_count += 1
    return rho, error, iter_count
Esempio n. 6
0
 def test_dot_product(self):
     actual = Vector.dot_product(self.v1, self.v2)
     expected = 5 + 12 + 21 + 32
     self.assertAlmostEqual(actual, expected)