Example #1
0
def generate_random_data(number_of_files: int) -> list:
    data_files = []

    for i in range(0, number_of_files):
        theta = generate_random_theta()
        data = []
        points = random.randint(2, 100)
        power = random.randint(1, 5)
        for _ in range(2, points):
            error = ft_abs(
                int(linear(ft_pow(random.randint(1, 10), power), theta)))
            x = generate_random_int(power)
            y = linear(x, theta) + random.randint(-error, error)
            data.append([x, int(y)])
        filename = 'test_' + str(i)
        write_file(data, filename)
        write_answer_file(theta, filename)
        # il faut normalizer theta
        data_files.append({'file': filename, 'result': theta})

    return data_files
Example #2
0
def compute_price(distance: int):
    thetas = get_thetas()
    price = ft_math.linear(distance, thetas)
    print(
        'This car should cost {} euros.\
            \n\tHis kilometrage (distance) is {}\
            \n\tfunction used: price(distance) = {} + {} * distance'
        .format(
            int(price),
            distance,
            thetas[0],
            thetas[1]
        )
    )
Example #3
0
def denormalize(x: float, theta_normalized: list, data_range: list,
                average: list) -> float:
    x_normalized = normalize(x, data_range[0], average[0])
    result = linear(x_normalized, theta_normalized)

    return result * data_range[1] + average[1]
Example #4
0
def partial_derivative_theta0(theta: list, data: list) -> float:
    return linear(data[0], theta) - data[1]
Example #5
0
def cost_function(data: list, theta: list, count: int):
    return 1 / (2 * count) * sum(
        [ft_pow(linear(item[0], theta) - item[1], 2) for item in data])
Example #6
0
def test():
    linear_test = [
        [[1, 0], 2, 1],
        [[1, 0], 1, 1],
        [[1, 0], 0, 1],
        [[1, 0], -1, 1],
        [[-2, 0], -1, -2],
        [[0, 1], 2, 2],
        [[0, 1], 1, 1],
        [[0, 1], 0, 0],
        [[0, 1], -1, -1],
        [[0, -2], 2, -4],
        [[1, 1], 1, 2],
        [[-1, -1], 1, -2],
        [[-1, 1], 1, 0],
        [[-2, 3], 1, 1],
    ]

    for item in linear_test:
        result = linear(item[1], item[0])
        if (result != item[2]):
            print(item, result)
            break

    partial_derivative_theta_test = [
        # theta = 0
        [[0, 0], [1, 1], -1, -1],
        [[0, 0], [2, 0], 0, 0],
        [[0, 0], [0, 2], -2, 0],
        # theta1 = 0
        [[1, 0], [1, 1], 0, 0],
        [[1, 0], [0, 2], -1, 0],
        [[1, 0], [2, 0], 1, 2],
        # theta0 = 0
        [[0, 1], [2, 2], 0, 0],
        [[0, 1], [2, 0], 2, 4],
        [[0, 1], [0, 2], -2, 0],
        # theta != 0
        [[2, 2], [1, 1], 3, 3],
        [[2, 2], [0, 2], 0, 0],
        [[2, 2], [2, 0], 6, 12],
        # with neg numbers
        [[-2, 2], [2, 0], 2, 4],
        [[-2, 2], [2, 1], 1, 2],
        [[-2, 2], [2, -3], 5, 10],
        [[-2, 2], [-2, 1], -7, 14],
    ]

    for item in partial_derivative_theta_test:
        result0 = partial_derivative_theta0(item[0], item[1])
        result1 = partial_derivative_theta1(item[0], item[1])
        if (result0 != item[2] or result1 != item[3]):
            print(item, result0, result1)
            break

    gradient_descent_test = [
        [[0, 0], [[0, 0], [1, 1]], [1, 1]],
        [[1, 1], [[0, 0], [1, 1]], [-1, 0]],
        [[0, -1], [[0, 0], [1, 1]], [2, 1]],
        [[-1, -1], [[0, 0], [1, 1]], [3, 2]],
        [[0, 1], [[0, 0], [1, 1]], [0, 1]],
    ]
    ratio = 2
    for item in gradient_descent_test:
        result = gradient_descent(item[0], item[1], ratio / len(item[1]))
        if (result != item[2]):
            print(item, result)
            break

    gradient_descent_test = [
        [[0, 0], [[0, 0], [1, 1]], [0.5, 0.5]],
        [[1, 1], [[0, 0], [1, 1]], [0, 0.5]],
        [[0, -1], [[0, 0], [1, 1]], [1, 0]],
        [[-1, -1], [[0, 0], [1, 1]], [1, 0.5]],
        [[0, 1], [[0, 0], [1, 1]], [0, 1]],
    ]
    ratio = 1
    for item in gradient_descent_test:
        result = gradient_descent(item[0], item[1], ratio / len(item[1]))
        if (result != item[2]):
            print(item, result)
            break

    pow_test = [
        [1, 2, 1],
        [0, 2, 0],
        [0, 0, 1],
        [42, 0, 1],
        [42, -1, 1 / 42],
        [10, 2, 100],
        [10, 3, 1000],
        [2, 2, 4],
        [2, 10, 1024],
        [0.5, 2, 0.25],
    ]
    for item in pow_test:
        result = ft_pow(item[0], item[1])
        if (result != item[2]):
            print(item, result)
            break

    abs_test = [
        [1, 1],
        [0, 0],
        [-1, -1],
        [-0.5, -0.5],
    ]
    for item in abs_test:
        result = ft_abs(item[0])
        if (result != item[1]):
            print(item, result)
            break