梯度下降算法的Python实现 – 等高线图

作为自学练习,我尝试从头开始在线性回归问题上实现梯度下降算法,并在等高线图上绘制结果迭代。

我的梯度下降实现给出了正确的结果(使用Sklearn测试过),然而,梯度下降图似乎并不与等高线垂直。这是预期的,还是我的代码/理解有误?

算法

enter image description here

成本函数和梯度下降

import numpy as npimport pandas as pdfrom matplotlib import pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Ddef costfunction(X,y,theta):    m = np.size(y)    #成本函数的向量化形式    h = X @ theta    J = float((1./(2*m)) * (h - y).T @ (h - y));        return J;def gradient_descent(X,y,theta,alpha = 0.0005,num_iters=1000):    #初始化有用的值     m = np.size(y)    J_history = np.zeros(num_iters)    theta_0_hist, theta_1_hist = [], [] #用于后续绘图    for i in range(num_iters):        #梯度函数的向量化形式        h = X @ theta        theta = theta - alpha * (1/m)* (X.T @ (h-y))        #每轮迭代的成本和中间值        J_history[i] = costfunction(X,y,theta)        theta_0_hist.append(theta[0,0])        theta_1_hist.append(theta[1,0])    return theta,J_history, theta_0_hist, theta_1_hist

绘图

#创建数据集(如前所述)x = np.linspace(0,1,40)noise = 1*np.random.uniform(  size = 40)y = np.sin(x * 1.5 * np.pi ) y_noise = (y + noise).reshape(-1,1)X = np.vstack((np.ones(len(x)),x)).T#设置theta值的网格T0, T1 = np.meshgrid(np.linspace(-1,3,100),np.linspace(-6,2,100))#计算每个theta组合的成本函数zs = np.array(  [costfunction(X, y_noise.reshape(-1,1),np.array([t0,t1]).reshape(-1,1))                      for t0, t1 in zip(np.ravel(T0), np.ravel(T1)) ] )#重塑成本值    Z = zs.reshape(T0.shape)#计算梯度下降theta_result,J_history, theta_0, theta_1 = gradient_descent(X,y_noise,np.array([0,-6]).reshape(-1,1),alpha = 0.3,num_iters=1000)#用于箭头图的角度anglesx = np.array(theta_0)[1:] - np.array(theta_0)[:-1]anglesy = np.array(theta_1)[1:] - np.array(theta_1)[:-1]%matplotlib inlinefig = plt.figure(figsize = (16,8))#表面图ax = fig.add_subplot(1, 2, 1, projection='3d')ax.plot_surface(T0, T1, Z, rstride = 5, cstride = 5, cmap = 'jet', alpha=0.5)ax.plot(theta_0,theta_1,J_history, marker = '*', color = 'r', alpha = .4, label = 'Gradient descent')ax.set_xlabel('theta 0')ax.set_ylabel('theta 1')ax.set_zlabel('成本函数')ax.set_title('梯度下降: 根在{}'.format(theta_result.ravel()))ax.view_init(45, 45)#等高线图ax = fig.add_subplot(1, 2, 2)ax.contour(T0, T1, Z, 70, cmap = 'jet')ax.quiver(theta_0[:-1], theta_1[:-1], anglesx, anglesy, scale_units = 'xy', angles = 'xy', scale = 1, color = 'r', alpha = .9)plt.show()

表面和等高线图

enter image description here

评论

我的理解是梯度下降应垂直于等高线进行。这不是这样的吗?谢谢


回答:

等高线图的问题在于theta0和theta1的尺度不同。只需在等高线图指令中添加”plt.axis(‘equal’)”,你就会发现梯度下降实际上是垂直于等高线的。

两个轴尺度相同的等高线图

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注