Intuitive definitions
Iterative method (Iterative Method), simple terms, in fact, continue to use the old variable values, recursive calculate the new value of the variable. cycle.
concrete application
- Find the value of the exact / approximate solution
- Dichotomy (Bisection method)
- Newton iterative method (Newton's method)
- Find the target value within a certain range
- Binary search
- Iterative algorithm in machine learning
- K- means algorithm (K-means clustering)
- PageRank Markov chain (Markov chain)
- Gradient descent method (Gradient descent)
Detailed application
- A Equation exact / approximate solution
- dichotomy
#'''计算某个给定正整数 n(n>1)的平方根,如果不使用编程语言'''
# delta_threshold:允许的误差的阈值
# max_try:最大尝试次数
def get_squre_root(n,delta_threshold=0.000000000000001,max_try=1000):
if n <= 1:
return -1
min = 1.0
n = float(n)
max = n
mid = (max+min)/2.0
print(mid)
for i in range(max_try):
_n = mid * mid
delta = _n-n
if delta == 0:
print("精确值")
return mid
abs_delta = abs(delta)
if abs_delta <= delta_threshold:
print("近似值")
return mid
else:
if delta>0:
max = mid
else:
min = mid
mid = (max+min)/2.0
print(mid)
return min
get_squre_root(16)
复制代码
- Newton iterative method
to add after
- Find matching records
quickly find records, in addition to a dictionary, you can use well-known binary search method (the premise is ordered). This is a typical case of iterative approximation.
- Binary search, the first edition
#在排好序的单词列表中查找某个单词
#@ param words_list,target_word
#@ return bool
def search(words_list,target_word):
if not words_list:
return False
min = 1
max = len(words_list)
while True:
mid = (max + min)/2
mid_word = words_list[mid]
if target_word == mid_word:
print(mid)
return True
elif target_word > mid_word:
min = mid
else:
max = mid
if max <= min:
return False
return False
# words_list = ["i","love","my","wife","than","myself's","body","."]
words_list = ["e"]
words_list = sorted(words_list)
print(words_list)
print(search(words_list,"i"))
复制代码
- Binary search, after the completion of bug, Second Edition
#在排好序的单词列表中查找某个单词
#@ param words_list,target_word
#@ return bool
# 优化1: min和max的初始化,从0开始,这样避免只有len(list)=1时的bug
# 优化2: mid = min + (max - min)/2 ,减少了内存溢出的风险
def search(words_list,target_word):
if not words_list:
return False
min = 0
max = len(words_list) - 1
while True:
mid = min + (max - min)/2
mid_word = words_list[mid]
if target_word == mid_word:
print(mid)
return True
elif target_word > mid_word:
min = mid
else:
max = mid
if max <= min:
return False
return False
words_list = ["i","love","my","wife","than","myself's","body","."]
# words_list = ["e"]
words_list = sorted(words_list)
print(words_list)
print(search(words_list,"i"))
复制代码
- Binary search, and then change the bug, third edition (it should not bug ..)
#在排好序的单词列表中查找某个单词
#@ param words_list,target_word
#@ return bool
# 优化1: min和max的初始化,从0开始,这样避免只有len(list)=1时的bug
# 优化2: mid = min + (max - min)/2 ,减少了内存溢出的风险
# 优化3: 循环时,min = mid + 1。和max = mid - 1。减少重复检查边界
# 优化4: 跳出循环的条件改为max < min,避免最后一步出现max=min=target的潜在bug
def search(words_list,target_word):
if not words_list:
return False
min = 0
max = len(words_list) - 1
while True:
mid = min + (max - min)/2
mid_word = words_list[mid]
if target_word == mid_word:
print(mid)
return True
elif target_word > mid_word:
min = mid + 1
else:
max = mid - 1
if max < min:
print(max)
return False
return False
words_list = ["i","love","my","wife","than","myself's","body","."]
# words_list = ["e"]
words_list = sorted(words_list)
print(words_list)
print(search(words_list,"i"))
复制代码
Think
Features iterative method is the "divide and rule", repeating a similar behavior, step by step to narrow the target range. The computer is very good for this repetitive work, but human beings are not good, sometimes not so sensitive. When programming, we can deliberately pay attention to this difference.
WechatIMG37.jpeg