线性回归调整问题:学习率与方程精度(Python)

问题描述 投票:0回答:1

我正在尝试以 0.01 的学习率调整线性回归。但是,我遇到了一个问题,线路似乎无法正确调整。该线似乎没有遵循学习率规定的预期路径,而是随机移动到不同的位置,而没有正确对齐。我确信这不是因为学习率,而是因为方程。

这是我尝试过的

from sklearn.datasets import make_regression, make_classification, make_blobs
import matplotlib.pyplot as plt
import numpy as np
import random as rd

# 1. make_regression
x, y = make_regression(
  n_samples=100,
  n_features=1,
  noise=30
)

# 2. make_classification
train_x = x[:-20]
test_x = x[-20:]

train_y = y[:-20]
test_y = y[-20:]

# 3. make_blobs
def graph_linear_regression():
  # Plot the data
  plt.scatter(train_x, train_y, color='blue', label='Data')

  # Plot the linear regression line
  plt.plot(train_x, train_x, color='red', label='Linear Regression')

  # Add labels and title
  plt.xlabel('x')
  plt.ylabel('y')
  plt.title('Linear Regression')

  # Display the plot
  plt.legend()
  plt.show()

# 4. Calculate the error
def calculate_update_error():
  global m
  global b
  learning_rate = 0.01
  # Calculate Error
  # M = 1/n * sum(f(x) - y)
  # B = 1/n * sum(f(x) - y) * x

  M = 1 / n * np.sum(f(train_x) - train_y)
  B = 1 / n * np.sum(np.sum(f(train_x) - train_y) * train_x)

  m = m - learning_rate * M
  b = b - learning_rate * B

# Calculate linear regresion
# Step 1 - initialize

m = rd.randint(1, 100)
b = rd.randint(1, 100)
n = len(train_x)
learning_rate = 0.01

print(f"n: {n}")
print(f"m: {m}")
print(f"b: {b}")
print(f"learning_rate: {learning_rate}")

def f(x):
  return m * x + b

# Problem?
for i in range(n):
  calculate_update_error()
  print()

graph_linear_regression()

这就是我得到的

enter image description here

现在,这就是我想要获得的,这是使用最小平方 enter image description here

python machine-learning linear-regression
1个回答
0
投票

您指出的几行:

M = 1 / n * np.sum(f(train_x) - train_y)
B = 1 / n * np.sum(np.sum(f(train_x) - train_y) * train_x)

不正确,因为它们涉及不兼容形状数组之间的操作。

y_train
是形状为
(80,)
的 1D 数组,
f(x_train)
是形状为
(1, 80)
的 2D 数组。因此
f(train_x) - train_y
将是一个
(80, 80)
数组,这不属于本用例。

此外,在计算

x_train
的梯度时,应乘以
f
项,而不是
M
您可以通过转置其中之一来计算梯度:

B

或者像这样使用For循环:

M = 1 / n * np.sum((f(train_x) - train_y.reshape(-1, 1)) * train_x) B = 1 / n * np.sum(f(train_x) - train_y.reshape(-1, 1))

© www.soinside.com 2019 - 2024. All rights reserved.