摘要:在排好序的單詞列表中查找某個(gè)單詞優(yōu)化和的初始化,從開(kāi)始,這樣避免只有時(shí)的優(yōu)化減少了內(nèi)存溢出的風(fēng)險(xiǎn)優(yōu)化循環(huán)時(shí),。
直觀定義
迭代法(Iterative Method),簡(jiǎn)單來(lái)說(shuō),其實(shí)就是不斷地用舊的變量值,遞推計(jì)算新的變量值。循環(huán)。
具體應(yīng)用
求數(shù)值的精確/近似解
二分法(Bisection method)
牛頓迭代法(Newton’s method)
在一定范圍內(nèi)查找目標(biāo)值
二分查找
機(jī)器學(xué)習(xí)中的迭代算法
K-均值算法(K-means clustering)
PageRank 的馬爾科夫鏈(Markov chain)
梯度下降法(Gradient descent)
應(yīng)用詳解求方程的精確/近似解
二分法
#"""計(jì)算某個(gè)給定正整數(shù) n(n>1)的平方根,如果不使用編程語(yǔ)言""" # delta_threshold:允許的誤差的閾值 # max_try:最大嘗試次數(shù) 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)
牛頓迭代法
之后補(bǔ)充
查找匹配記錄
快速查找記錄,除了用字典,還可以用著名的 二分查找法(前提是有序)。這也是迭代逼近的典型案例。
二分查找,第一版
#在排好序的單詞列表中查找某個(gè)單詞 #@ 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"))
二分查找,改完bug后,第二版
#在排好序的單詞列表中查找某個(gè)單詞 #@ param words_list,target_word #@ return bool # 優(yōu)化1: min和max的初始化,從0開(kāi)始,這樣避免只有l(wèi)en(list)=1時(shí)的bug # 優(yōu)化2: mid = min + (max - min)/2 ,減少了內(nèi)存溢出的風(fēng)險(xiǎn) 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"))
二分查找,再改bug后,第三版(應(yīng)該沒(méi)bug了吧。。)
#在排好序的單詞列表中查找某個(gè)單詞 #@ param words_list,target_word #@ return bool # 優(yōu)化1: min和max的初始化,從0開(kāi)始,這樣避免只有l(wèi)en(list)=1時(shí)的bug # 優(yōu)化2: mid = min + (max - min)/2 ,減少了內(nèi)存溢出的風(fēng)險(xiǎn) # 優(yōu)化3: 循環(huán)時(shí),min = mid + 1。和max = mid - 1。減少重復(fù)檢查邊界 # 優(yōu)化4: 跳出循環(huán)的條件改為max < min,避免最后一步出現(xiàn)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"))思考
迭代法的特點(diǎn)是“分而治之”,不斷重復(fù)一個(gè)相似的行為,一步步地縮小目標(biāo)范圍。計(jì)算機(jī)很適合處理這種重復(fù)的工作,而人類并不擅長(zhǎng),所以有時(shí)候不敏感。在編程的時(shí)候,可以特意留意這一差異。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/43940.html
摘要:在這里我分享下我個(gè)人入門機(jī)器學(xué)習(xí)的經(jīng)歷,希望能對(duì)大家能有所幫助。相關(guān)學(xué)習(xí)鏈接,,入門后的體驗(yàn)在入門了機(jī)器學(xué)習(xí)之后,在實(shí)際工作中,絕大多數(shù)的情況下你并不需要去創(chuàng)造一個(gè)新的算法。 機(jī)器學(xué)習(xí)在很多眼里就是香餑餑,因?yàn)闄C(jī)器學(xué)習(xí)相關(guān)的崗位在當(dāng)前市場(chǎng)待遇不錯(cuò),但同時(shí)機(jī)器學(xué)習(xí)在很多人面前又是一座大山,因?yàn)榘l(fā)現(xiàn)它太難學(xué)了。在這里我分享下我個(gè)人入門機(jī)器學(xué)習(xí)的經(jīng)歷,希望能對(duì)大家能有所幫助。 PS:這篇文章...
閱讀 2060·2021-09-07 10:14
閱讀 1492·2019-08-30 15:53
閱讀 2281·2019-08-30 12:43
閱讀 2870·2019-08-29 16:37
閱讀 766·2019-08-26 13:29
閱讀 2012·2019-08-26 13:28
閱讀 450·2019-08-23 18:33
閱讀 3535·2019-08-23 16:09