首先,有一些关于此的主题,但它们涉及已弃用的 pandas 包等。假设我试图用变量 x,y
预测变量 w
和z
。我想运行多元线性回归来尝试预测 w。有很多解决方案可以产生系数,但我不确定如何使用它们。所以,在伪代码中;
import numpy as np
from scipy import stats
w = np.array((1,2,3,4,5,6,7,8,9,10)) # Time series I'm trying to predict
x = np.array((1,3,6,1,4,6,8,9,2,2)) # The three variables to predict w
y = np.array((2,7,6,1,5,6,3,9,5,7))
z = np.array((1,3,4,7,4,8,5,1,8,2))
def model(w,x,y,z):
# do something!
return guess # where guess is some 10 element array formed
# using multiple linear regression of x,y,z
guess = model(w,x,y,z)
r = stats.pearsonr(w,guess) # To see how good guess is
希望这是有意义的,因为我是 MLR 新手。 scipy 中可能有一个包可以完成所有这些工作,欢迎任何帮助!
最佳答案
您可以使用正规方程方法。 让你的方程的形式为: ax+by+cz +d =w 然后
import numpy as np
x = np.asarray([[1,3,6,1,4,6,8,9,2,2],
[2,7,6,1,5,6,3,9,5,7],
[1,3,4,7,4,8,5,1,8,2],
[1,1,1,1,1,1,1,1,1,1]]).T
y = numpy.asarray([1,2,3,4,5,6,7,8,9,10]).T
a,b,c,d = np.linalg.pinv((x.T).dot(x)).dot(x.T.dot(y))
关于python - 使用 Python 进行多元线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47528498/