Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 我的简单线性回归有什么问题?_Python 3.x_Numpy_Linear Regression - Fatal编程技术网

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()