码迷,mamicode.com
首页 > 编程语言 > 详细

机器学习之逐次下降法(机器学习算法原理与实践)郑捷

时间:2018-05-13 19:52:08      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:return   erro   img   atp   计算   common   res   odi   原理   

逐次下降法的定义:

  1. 对于给定的方程组技术分享图片,使用公式: 
    技术分享图片 
    其中k为迭代次数(k=0,1,2,…) 
    逐步代入求近似解的方法称为迭代法
  2. 如果技术分享图片存在(记为技术分享图片),称此迭代法收敛,显然技术分享图片就是方程组的解,否则称此迭代法发散。
  3. 研究{技术分享图片}的收敛性。引进误差向量: 
    技术分享图片 
    得到: 
    技术分享图片 
    递推得到: 
    技术分享图片 
    要考察{技术分享图片}的收敛性,就要研究B在技术分享图片技术分享图片的条件。

下面给出Python实现

# -*- coding: utf-8 -*-
import numpy as np
from numpy import *
from common_libs import *
import matplotlib.pyplot as plt

#消元发求解方程组的解
#求解元方程
def method_nomal():
A=mat([[8,-3,2],
[4,11,-1],
[6,3,12]])
b=mat([20,33,36])

result=linalg.solve(A,b.T)
print result

技术分享图片

 

#迭代法进行计算
def interationMethod(n,B0,f):
error = 1.0e-6 # 迭代阈值
steps = 100 # 迭代次数
xk=zeros((n,1))
errorlist=[]
for i in range(steps):
xk_1=xk
xk=B0*xk+f
errorlist.append(linalg.norm(xk_1-xk))
if errorlist[-1]<error:
print i+1
break
print xk
return i,errorlist

 

 

method_nomal()
B0=mat([[0.0,3.0/8,-2.0/8],
[-4.0/11,0.0,1.0/11],
[-6.0/12,-3.0/12,0.0]])
print(B0)
f=mat([20.0/8,33.0/11,36.0/12])
[k,errorlist]=interationMethod(3,B0,f)

 

技术分享图片


# 绘制散点图
matpts= zeros((2,k+1))
matpts[0]=linspace(1,k+1,k+1)
print matpts[0]
matpts[1]=array(errorlist)
drawScatter(plt,matpts)
plt.show()

技术分享图片

 

机器学习之逐次下降法(机器学习算法原理与实践)郑捷

标签:return   erro   img   atp   计算   common   res   odi   原理   

原文地址:https://www.cnblogs.com/judejie/p/9033008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!