Моя модель:
Код: Выделить всё
def linear_regression(x: np.array, y: np.array, learning_speed: float = 0.1, echoes: int = 1000):
n = len(y)
coef = np.zeros(len(x[0]) + 1)
for _ in range(echoes):
d = np.array([]) # Array to contain a step for each coef
for idx in range(len(coef)):
d_coef = 0
for row in range(len(x)):
if idx == len(coef) - 1:
d_coef += (1) * (np.sum(coef[:-1] * x[row]) + coef[-1] - y[row])
else:
d_coef += (x[row, idx]) * (np.sum(coef[:-1] * x[row]) - y[row] + coef[-1]) # Sum of derivatives of (V1X1 + ... + VnXn + b - y) related to coef idx
d = np.append(d, (2 / n) * d_coef)
coef -= learning_speed * d
print(f'coef = {coef[:-1]}')
print(f'b = {coef[-1]}')
Код: Выделить всё
x1_rnd = np.array([])
x2_rnd = np.array([])
c1 = 5
c2 = 0.75
b = 15
for _ in range(500):
x1_rnd = np.append(x1_rnd, np.random.randint(1, 100))
x2_rnd = np.append(x2_rnd, np.random.randint(1, 100))
combo = zip(x1_rnd, x2_rnd)
x = np.array([list(elem) for elem in combo])
y_rnd = c1 * x1_rnd + c2 * x2_rnd + b
Я не могу найти никаких ошибок в градиентном спуске кода. Пожалуйста, помогите решить проблему!
Подробнее здесь: https://stackoverflow.com/questions/787 ... regression