scikit-learn求解多元线性回归问题

http://www.shareditor.com/blogshow/?blogId=54


>>> from numpy.linalg import inv
>>> from numpy import dot,transpose
>>> x = [[1,1,1],[1,1,2],[1,2,1]]
>>> y = [[6],[9],[8]]
>>> print dot(inv(dot(transpose(x),x),dot(transpose(x),y)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: inv() takes exactly 1 argument (2 given)
>>> print dot(inv(dot(transpose(x),x)),dot(transpose(x),y))
[[ 1.]
[ 2.]
[ 3.]]
>>> from numpy.linalg import lstsq
>>> print lstsq(x,y)[0]
[[ 1.]
[ 2.]
[ 3.]]
>>> from sklearn.linear_model import LinearRegression
x>>> x = [[1,1,1],[1,1,2],[1,2,1]]
>>> y = [[6,[9],[8]]
... 
... 
... 
... ;
File "<stdin>", line 5
;
^
SyntaxError: invalid syntax
>>> y - [[6],[9],[8]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'list' and 'list'
>>> y = [[6],[9],[8]]
>>> model = LinearRegression()
>>> model.fit(x,y)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
>>> x2 = [[1,3,5]]
>>> y2 = model.predict(x2)
>>> print y2
[[ 22.]]

设计二元一次方程:y=1+2x1+3x2

取样本为(1,1,1),(1,1,2),(1,2,1),计算得y=(6,9,8)

注意:这里面常数项1相当于1*x0,只不过这里的x0永远取1

所以我们的

X = [[1,1,1],[1,1,2],[1,2,1]]

y = [[6],[9],[8]]

刚好y=1+2x1+3x2=1+23+35=22

Loading Disqus comments...
Table of Contents