您的位置:首页 > 编程语言

线性回归的正规方程解法与梯度下降解法的代码

2015-07-24 20:56 537 查看
理论参见:/article/9606365.html

梯度公式:



__author__ = 'Chen'

from numpy import *
def linearRegresion(x,y,type=True,alpha=0.01):

xrow = shape(x)[0]
xcol = shape(x)[1]
x = matrix(x)
Y = matrix(y)
# fill ones
xone = ones((xrow,1))
X = hstack((xone,x))
X = matrix(X)
# normal equiation
if type == True:

theta = (X.T*X).I*X.T*Y
return theta
else:

# gradiant
theta = matrix(random.random(xcol+1))
# iterations
for iteration in range(1,10000):
sums = 0
#gradient method
for i in range(xrow):
sums += (theta*X[i,:].T-Y[i,:])*X[i,:]
theta -= alpha*sums/xrow
return theta

x= [[0,1,0],[0,0,1],[0,1,1],[1,1,1]]
y= [[1],[2],[3],[4]]

# calculate linearRegression by normal equation
theta1 = linearRegresion(x,y)
print theta1

#gradient descent
theta2 = linearRegresion(x,y,False)

print theta2


C:\Python27\python.exe C:/Users/Chen/PycharmProjects/mypython/courseraML/LinearRegression.py
[[ 0.]
[ 1.]
[ 1.]
[ 2.]]
[[ 0.00709026  1.00390811  0.99480722  1.99480246]]

Process finished with exit code 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: