前言
机器学习的梯度向量求解代码实现练习
一、使用步骤
1.引入库
代码如下(示例):
from numpy import *
import matplotlib.pyplot as plt
2.全部代码
代码如下(示例):
from numpy import *
import matplotlib.pyplot as plt
data_size = 20
X0 = ones((data_size,1))
X1 = arange(1,data_size + 1).reshape(data_size,1)
X = hstack((X0,X1))
Y = array([6,4,21,5,2,22,7,14,11,8,12,18,13,10,16,14,18,23,19,20]).reshape(data_size,1)
alpha = 0.01
def cost_function(theta,X,Y):
diff = dot(X,theta) - Y
return (1/(2*data_size)) * dot(diff.transpose(),diff)
def gradient_function(theta,X,Y):
diff = dot(X,theta) - Y
return (1/data_size) * dot(X.transpose(),diff)
def gradient_descent(X,Y,alpha):
theta = array([1,1]).reshape(2,1)
gradient = gradient_function(theta,X,Y)
while not all(abs(gradient) <= 0.0005):
theta = theta - alpha * gradient
#theta -=alpha * gradient这中表达方式会报错
gradient = gradient_function(theta,X,Y)
return theta
optimaized = gradient_descent(X,Y,alpha)
print("optimal:",optimal)
print("cost function:",cost_function(optimal,X,Y)[0][0])
def plot(X,Y,theta):
ax = plt.subplot() #ax = plt.subplot(111)
ax.scatter(X,Y,s = 30,c = "black",marker = "s")
plt.xlabel("X")
plt.ylabel("Y")
x = arange(0,21,0.1)
y = theta[0] + theta[1] * x
ax.plot(x,y)
#下面这段代码调试前少了,靠
plt.show()
plot(X1,Y,optimized)