### Python梯度下降线性回归计算不准确

我开始学习Andrew NG在Coursera上的机器学习课程。我尝试实现梯度下降的线性回归,但我不确定我错过了什么。根据这个enter image description here

我尝试实现它,但似乎有些问题。这里是代码。值得指出的是,这是我第一次接触Python,没有学习基础知识。

import numpy as npimport matplotlib.pyplot as pltplt.ion()x = [1,2,3,4,5]y = [1,2,3,4,5]def Gradient_Descent(x, y, learning_rate, iterations):  theta_1=np.random.randint(low=2, high=5);   theta_0=np.random.randint(low=2, high=5);  m = x.shape[0]def mean_error(a, b, factor):  sum_mean = 0  for i in range(m):    sum_mean += (theta_0 + theta_1 * a[i]) - b[i]  # h(x) = (theta0 + theta1 * x) - y     if factor:      sum_mean *= a[i]  return sum_meandef perform_cal(theta_0, theta_1, m):  temp_0 = theta_0 - learning_rate * ((1 / m) * mean_error(x, y, False))  temp_1 = theta_1 - learning_rate * ((1 / m) * mean_error(x, y, True))  return temp_0 , temp_1fig = plt.figure()ax = fig.add_subplot(111)for i in range(iterations):    theta_0, theta_1 = perform_cal(theta_0, theta_1, m)    ax.clear()    ax.plot(x, y, linestyle='None', marker='o')    ax.plot(x, theta_0 + theta_1*x)    fig.canvas.draw()x = np.array(x)y = np.array(y)Gradient_Descent(x,y, 0.1, 500)input("Press enter to close program")

我哪里做错了?


回答:

import numpy as npimport matplotlib.pyplot as pltplt.ion()x = [1,2,3,4,5]y = [1,2,3,4,5]def Gradient_Descent(x, y, learning_rate, iterations):  theta_1=0  theta_0=0  m = x.shape[0]  for i in range(iterations):      theta_0, theta_1 = perform_cal(theta_0, theta_1, m, learning_rate)      ax.clear()      ax.plot(x, y, linestyle='None', marker='o')      ax.plot(x, theta_0 + theta_1*x)      fig.canvas.draw()def mean_error(a, b, factor, m, theta_0, theta_1):  sum_mean = 0  for i in range(m):    sum_mean += (theta_0 + theta_1 * a[i]) - b[i]  # h(x) = (theta0 + theta1 * x) - y     if factor:      sum_mean *= a[i]  print(sum_mean)  return sum_meandef perform_cal(theta_0, theta_1, m, learning_rate):  temp_0 = theta_0 - learning_rate * ((1 / m) * mean_error(x, y, False, m, theta_0, theta_1))  temp_1 = theta_1 - learning_rate * ((1 / m) * mean_error(x, y, True, m, theta_0, theta_1))  return temp_0 , temp_1fig = plt.figure()ax = fig.add_subplot(111)x = np.array(x)y = np.array(y)Gradient_Descent(x,y, 0.01, 100)

我对你的代码做了一些修改(主要是重新排列了几行,没有改变你做的任何事情,以免看起来令人困惑),现在它可以工作了。我建议你先学习语言的基础知识,因为大多数错误都是非常基本的,比如参数传递等。然而,你自己尝试实现Andrew Ng课程的内容是值得称赞的。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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