import numpy as np
import random
def genData(numPoints,bias,variance):
x = np.ones(shape=(numPoints,2)) #zeros(shape=(a,b)) == zeros((a,b))
y = np.zeros((numPoints))
for i in range(numPoints):
x[i][1] = i
y[i] = (i+bias)+random.uniform(0,1)*variance #构建以(i+bias)为主的矩阵,搞不懂为什么要加入variance,与矩阵y究竟什么关系
return x,y
'''
x,y:矩阵
theta:一组向量和x相乘的值
alpha:梯度下降时的参数,即每一步下降多少
m:实例的个数
numIterations:迭代计算的次数,可以理解为梯度下降多少步
'''
def gradientDescent(x,y,theta,alpha,m,numIterations):
xTrans = x.T
for i in range(numIterations):
hypothesis = np.dot(x,theta) #返回数值hypothesis,最终收敛值与初设theta无关
loss = hypothesis - y
cost = np.sum(loss**2)/(2*m)
print ('Iteration %d | Cost:%f'%(i,cost))
gradient = np.dot(xTrans,loss)/m
theta = theta - alpha*gradient
return theta
x,y = genData(100,25,10)
m,n =np.shape(x)
n_y = np.shape(y)
numIterations = 100000
alpha = 0.0005
theta = 100*np.ones(n)
theta = gradientDescent(x,y,theta,alpha,m,numIterations)
print (theta)
转载于:https://my.oschina.net/u/3786144/blog/1802929