Finding the ‘Heavenly Path’ of Data Points: A Linear Regression Walkthrough

This article walks through linear regression by visualizing scattered data points, defining the line y = ax + b, deriving the mean‑squared‑error cost function, computing its gradients with the chain rule, and implementing gradient descent in Python to find the optimal parameters.

YiSu Grain
YiSu Grain
YiSu Grain
Finding the ‘Heavenly Path’ of Data Points: A Linear Regression Walkthrough

Imagine a cloud of data points on a coordinate plane and draw a line that captures their overall trend—this line, expressed as y = a·x + b, represents the "Heavenly Path" (regression line) that the points tend to follow.

The slope a indicates how steep the line is, while the intercept b gives the value of y when x = 0. To evaluate how well a line fits the data, the article introduces a cost (or loss) function: the average of the squared differences between the actual

y</i> values and the predicted values <code>ŷ_i = a·x_i + b

. This is the mean‑squared‑error (MSE):

Minimizing the MSE requires computing its partial derivatives with respect to a and b. Using the chain rule, the gradient for the slope becomes -2·Σ (x_i·(y_i − (a·x_i + b))) and for the intercept -2·Σ (y_i − (a·x_i + b)). The article shows each differentiation step, including how terms that do not contain the variable disappear.

With the gradients, gradient descent updates the parameters iteratively:

Initialize a and b (randomly or at zero).

Compute gradients using the formulas above.

Update a ← a − α·∂J/∂a and b ← b − α·∂J/∂b, where α is the learning rate.

Repeat steps 2‑3 until the loss change is below a threshold or a maximum number of iterations is reached.

The article then provides a complete Python script that loads the data, computes the optimal a and b, plots the original points and the fitted line, and prints the equation and data ranges.

# 导入必要的库
import numpy as np
import matplotlib.pyplot as plt

# 使用之前计算的 a_orig 和 b_orig(线性回归的斜率和截距)

# 重新绘制图表
plt.figure(figsize=(12, 8))

# 绘制原始数据点
plt.scatter(x_orig, y_orig, color='blue', alpha=0.7, label='Data points')

# 计算拟合线的点
x_line = np.linspace(x_orig.min(), x_orig.max(), 100)
y_line = a_orig * x_line + b_orig

# 绘制拟合线
plt.plot(x_line, y_line, color='green', linewidth=3, linestyle='--', label='Fitted line')

# 设置图表标题和轴标签
plt.title("最终修正后的散点和拟合线", fontsize=16)
plt.xlabel('房屋面积 (平方米)', fontsize=12)
plt.ylabel('房屋价格 (万元)', fontsize=12)
plt.legend(fontsize=10)

# 设置合适的坐标轴范围
x_margin = (x_orig.max() - x_orig.min()) * 0.1
y_margin = (y_orig.max() - y_orig.min()) * 0.1
plt.xlim(x_orig.min() - x_margin, x_orig.max() + x_margin)
plt.ylim(min(y_orig.min(), y_line.min()) - y_margin, max(y_orig.max(), y_line.max()) + y_margin)

# 添加网格线
plt.grid(True, linestyle='--', alpha=0.7)

# 在图上添加拟合线方程
equation = f'y = {a_orig:.2f}x + {b_orig:.2f}'
plt.text(0.05, 0.95, equation, transform=plt.gca().transAxes, fontsize=12, verticalalignment='top')

# 显示图像
plt.show()

# 打印拟合线信息和原始数据范围
print(f"拟合线方程: {equation}")
print(f"最终修正后的拟合线起点: ({x_line[0]:.2f}, {y_line[0]:.2f})")
print(f"最终修正后的拟合线终点: ({x_line[-1]:.2f}, {y_line[-1]:.2f})")
print(f"原始数据 x 范围: ({x_orig.min():.2f}, {x_orig.max():.2f})")
print(f"原始数据 y 范围: ({y_orig.min():.2f}, {y_orig.max():.2f})")

Running this code visualizes the data, draws the regression line, and demonstrates how gradient descent converges to the optimal parameters, completing the "Heavenly Path" analogy.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

machine learningPythongradient descentLinear Regressioncost functionmean squared error
YiSu Grain
Written by

YiSu Grain

A fleeting mayfly in the world, a single grain in the boundless sea.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.