【问题标题】:Miscalculating the cost function for a linear regression model错误计算线性回归模型的成本函数
【发布时间】:2021-10-17 02:44:55
【问题描述】:

我正在尝试渲染成本函数的 3D 图。给定一个数据集和两个不同的参数(theta0 和 theta1),我想渲染一个我们在经典文献中都看到的碗状图形。我的假设函数只是一个简单的h(x) = theta_0 + theta_1 * x。但是,我的成本函数呈现如下:

得到这个情节可以吗?如果是的话,我们怎么能画出这样一个“碗”呢?

import matplotlib.pyplot as plt
import numpy as np

training_set = np.array([
    [20, 400],
    [30, 460],
    [10, 300],
    [50, 780],
    [15, 350],
    [60, 800],
    [19, 360],
    [31, 410],
    [5, 50],
    [46, 650],
    [37, 400],
    [39, 900]])

cost_factor = (1.0 / (len(training_set) * 2))

hypothesis = lambda theta0, theta1, x: theta0 + theta1 * x

cost = lambda theta0, theta1: cost_factor * sum(map(
    lambda entry: (hypothesis(theta0, theta1, entry[0]) - entry[1]) ** 2, training_set))

theta1 = np.arange(0, 10, 1)
theta2 = np.arange(0, 10, 1)

X, Y = np.meshgrid(theta1, theta1)

Z = cost(X, Y)

ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')
ax.set_xlabel(r'$\theta_0$')
ax.set_ylabel(r'$\theta_1$')
ax.set_zlabel(r'$J(\theta_0, \theta_1)$')
ax.set_title('Cost function')

plt.show()

【问题讨论】:

    标签: python numpy matplotlib math data-science


    【解决方案1】:

    旁注:

    • 我已在您的代码中将 theta1 重命名为 theta0 并将 theta2 重命名为 theta1 以避免代码和绘图标签之间的混淆
    • 您的代码中有错字:X, Y = np.meshgrid(theta1, theta1) 应该是 X, Y = np.meshgrid(theta0, theta1)

    您的Z 表面可能有一个绝对/相对最小/最大值点,该点在您选择的域之外:0 < theta0 < 100 < theta1 < 10。你可以尝试扩大这个区间,看看是否真的有一个静止点:

    theta0 = np.arange(-100, 100, 5)
    theta1 = np.arange(-100, 100, 5)
    

    所以-50 < theta1 < 50 有一个最小区域。看来您的 2D 表面在 theta0 方向上没有最小值;但是您也可以尝试扩展此域:

    theta0 = np.arange(-1000, 1000, 100)
    theta1 = np.arange(-50, 50, 1)
    

    因此您可以看到您的Z 表面没有最小点,而是与theta0theta1 都不对齐的最小区域。
    由于我不知道theta0theta1究竟代表什么,我可能给它们分配了没有意义的值:例如,如果它们分别是纬度和经度,那么它们的域应该是-90 < theta0 < 900 < theta1 < 180 .这取决于theta0theta1的物理含义。


    但是,您始终可以使用np.gradient 计算表面的梯度并绘制它们:

    g1, g2 = np.gradient(Z)
    
    fig = plt.figure()
    ax1 = fig.add_subplot(1, 3, 1, projection = '3d')
    ax2 = fig.add_subplot(1, 3, 2, projection = '3d')
    ax3 = fig.add_subplot(1, 3, 3, projection = '3d')
    ax1.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')
    ax2.plot_surface(X, Y, g1, cmap='viridis', edgecolor='none')
    ax3.plot_surface(X, Y, g2, cmap='viridis', edgecolor='none')
    
    ax1.set_xlabel(r'$\theta_0$')
    ax1.set_ylabel(r'$\theta_1$')
    ax1.set_zlabel(r'$J(\theta_0, \theta_1)$')
    ax1.set_title('Cost function')
    
    ax2.set_xlabel(r'$\theta_0$')
    ax2.set_ylabel(r'$\theta_1$')
    
    ax3.set_xlabel(r'$\theta_0$')
    ax3.set_ylabel(r'$\theta_1$')
    
    plt.show()
    

    可以看到渐变为空的区域是一条线,而不是一个点。


    如果您的Z 表面会有不同的表达,例如:

    Z = np.exp(-X**2 - Y**2)
    

    你会:

    在这种情况下,您可以看到两个梯度在点 (0, 0) 处为空,该点表面具有最大值。

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      • 2019-10-13
      • 2018-03-16
      • 2017-12-15
      • 2020-08-09
      • 1970-01-01
      相关资源
      最近更新 更多