Python 3.x 我的简单线性回归有什么问题?
我用这个代码找不到θ 我添加了绘图代码来帮助可视化问题 请帮我找到这段代码中的错误 谢谢Python 3.x 我的简单线性回归有什么问题?,python-3.x,numpy,linear-regression,Python 3.x,Numpy,Linear Regression,我用这个代码找不到θ 我添加了绘图代码来帮助可视化问题 请帮我找到这段代码中的错误 谢谢 错误出现在打印线中,应该是 plot(arr(N), arr(N)**2 * theta[2] + arr(N) * theta[1] + theta[0], y) 根据二次多项式模型 还有,;我认为出于解释性原因,您以这种方式计算最小二乘解,但在实践中,可以使用np.linalg.lstsq获得线性最小二乘拟合,如下所示,代码更短、效率更高: N = 20 x = np.arange(1, N+1)
错误出现在打印线中,应该是
plot(arr(N), arr(N)**2 * theta[2] + arr(N) * theta[1] + theta[0], y)
根据二次多项式模型
还有,;我认为出于解释性原因,您以这种方式计算最小二乘解,但在实践中,可以使用np.linalg.lstsq
获得线性最小二乘拟合,如下所示,代码更短、效率更高:
N = 20
x = np.arange(1, N+1)
y = x**2 + 3
basis = np.vstack((x**0, x**1, x**2)).T # basis for the space of quadratic polynomials
theta = np.linalg.lstsq(basis, y)[0] # least squares approximation to y in this basis
plt.plot(x, y, 'ro') # original points
plt.plot(x, basis.dot(theta)) # best fit
plt.show()
N = 20
x = np.arange(1, N+1)
y = x**2 + 3
basis = np.vstack((x**0, x**1, x**2)).T # basis for the space of quadratic polynomials
theta = np.linalg.lstsq(basis, y)[0] # least squares approximation to y in this basis
plt.plot(x, y, 'ro') # original points
plt.plot(x, basis.dot(theta)) # best fit
plt.show()