使用线性回归估计python中的斜率和截距

时间:2015-10-10 10:07:41

标签: python linear-regression

对于作业,我们被要求运行以下代码。

# Import numpy
import numpy as np
import numpy.random as npr
import numpy.linalg as npl
# Simulate some data as in the end of lecture 4 - but this time of higher dimension
n = 1000
npr.seed(123)
x_1 = npr.uniform(0,10,n)
x_2 = npr.uniform(0,10,n)
x_3 = npr.uniform(0,10,n)
x_4 = npr.uniform(0,10,n)
y = 3 + 2*x_1 - x_2 + 0.5*x_3 - 0.1*x_4 + npr.normal(0,1,n)
# Create a design matrix X with 5 columns, the first of which is all 1 the   subsequent ones are x_1, x_2, etc
X = np.array(([1]*n,x_1,x_2,x_3,x_4)).T

我们被要求使用inv函数和标准方程β=(X T X) -1 X T y 以获得斜率和截距参数的估计值。

然后我们会问你对x_1到3d.p的截距和系数有什么价值?

如果有人能够解释这一点,那将是一个很好的帮助,因为我是Python的新手,并会很感激一些指导。我们还被要求使用solve函数来获得β的另一个估计值?

0 个答案:

没有答案