# -*- coding:utf-8 -*-
#!/usr/bin/python
import numpy
import operator
import pylab
#从文本文件中读取数值并转换为mx2矩阵,返回矩阵和矩阵的行数
def File2Matrix(filename):
fr = open(filename)
arrayOfLines = fr.readlines()
numberOfLines = len(arrayOfLines)
returnMat = numpy.zeros((numberOfLines,2))
index = 0
for line in arrayOfLines:
line = line.strip()
listFromLine = line.split('\t')
returnMat[index,:] = listFromLine[0:2]
index+=1
return returnMat
#Hypothesis
def Hypothesis(theta0,theta1,x):
return theta0+theta1*x
#Cost Function
#param: 两个thetha初始值
def CostFun(theta0,theta1):
numberOfLines = matrix.shape[0]
sum = 0
for mat in matrix:
sum = sum + (Hypothesis(theta0,theta1,mat[0])-mat[1])**2
return 1.0/(2.0*numberOfLines)*sum
#theta0 的偏导数
def Pd_theta0(theta0,theta1):
numberOfLines = matrix.shape[0]
sum = 0
for mat in matrix:
sum = sum + Hypothesis(theta0,theta1,mat[0])-mat[1]
return 1.0/(numberOfLines)*sum
def Pd_theta1(theta0,theta1):
numberOfLines = matrix.shape[0]
sum = 0
for mat in matrix:
sum = sum + (Hypothesis(theta0,theta1,mat[0])-mat[1])*mat[0]
return 1.0/(numberOfLines)*sum
#梯度下降
def GradientDesent():
theta0 = 3
theta1 = 3
alpha = 0.001 #梯度步长
previousCost = CostFun(theta0,theta1)+1
while 1:
temp_theta0 = theta0 - alpha*Pd_theta0(theta0,theta1)
temp_theta1 = theta1 - alpha*Pd_theta1(theta0,theta1)
#取小数点后两位,如果为零,那么就退出循环
thisCost = CostFun(temp_theta0,temp_theta1)
if thisCost >= previousCost:
return theta0,theta1
break
else:
theta0 = temp_theta0
theta1 = temp_theta1
previousCost = thisCost
matrix = File2Matrix("dataSet.txt")
theta0,theta1 = GradientDesent()
X = numpy.linspace(-6, 6, 2,endpoint=True)
Y = numpy.ones((len(X),1))
i = 0
for x in X:
Y[i] = Hypothesis(theta0,theta1,x)
i+=1
pylab.plot(X,Y)
pylab.plot(matrix[:,0],matrix[:,1],'o')
pylab.show()
print("theta0:",theta0,"theta1:",theta1)
print("cost=",CostFun(theta0,theta1))
转载于:https://my.oschina.net/chengyunquan/blog/413812