Created
February 9, 2024 23:28
-
-
Save RenatoExpert/7749f6231c5132a7c5a152c715e568f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Next time use input() to define parameters | |
fit = 5000 | |
secret_a = 15 | |
secret_b = -22 | |
def calcule(x, a, b): | |
y = a * x + b | |
return y | |
def secret_y(x): | |
y = calcule(x, secret_a, secret_b) | |
return y | |
class Frame: | |
def __init__(self, x, a, b): | |
self.x = x | |
self.a = a | |
self.b = b | |
self.y = secret_y(x) | |
raw_error = calcule(x, a, b) - self.y | |
self.error = abs(raw_error) | |
self.signal = raw_error/abs(raw_error) | |
a = 0 | |
b = 0 | |
turn = True | |
for attempt in range(0, fit): | |
signal = 0 | |
errors = [] | |
for x in range(10): | |
data = Frame(x, a, b) | |
errors.append(data.error) | |
signal = data.signal | |
avg_error = sum(errors)/len(errors) | |
print("------") | |
print(attempt) | |
if turn: | |
print("a") | |
print(a, avg_error) | |
a -= signal * avg_error/10 | |
else: | |
print("b") | |
print(b, avg_error) | |
b -= signal * avg_error/10 | |
turn = not turn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment