import math, copy
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('./deeplearning.mplstyle')
from lab_utils_uni import plt_house_x, plt_contour_wgrad, plt_divergence, plt_gradients
# train data
x = np.array([1.0,2.0])
y = np.array([300.0,500.0])
#通过w*x+b得到y_pre,
def costFun(x,y,w,b):
m = x.shape[0] #数据长度,一维数组的长度
cost = 0 #初始化
for i in range(m):
f_wb = w*x[i]+b
cost += (f_wb-y[i])**2
return 1/(2*m)*cost
∂ J ( w , b ) ∂ w = 1 m ∑ i = 0 m − 1 ( f w , b ( x ( i ) ) − y ( i ) ) x ( i ) ∂ J ( w , b ) ∂ b = 1 m ∑ i = 0 m − 1 ( f w , b ( x ( i ) ) − y ( i ) ) \begin{align} \frac{\partial J(w,b)}{\partial w} &= \frac{1}{m} \sum\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) - y^{(i)})x^{(i)} \tag{4}\\ \frac{\partial J(w,b)}{\partial b} &= \frac{1}{m} \sum\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) - y^{(i)}) \tag{5}\\ \end{align} ∂w∂J(w,b)∂b∂J(w,b)=m1i=0∑m−1(fw,b(x(i))−y(i))x(i)=m1i=0∑m−1(fw,b(x(i))−y(i))(4)(5)
def gradientFun(x,y,w,b):
m = x.shape[0]
dj_dw = 0
dj_db = 0
for i in range(m):
f_wb = w*x[i]+b
# dj_dw += (f_wb - y[i])*x[i]
# dj_db += f_wb - y[i]
# dj_dw /= m
# dj_dw /= m
dj_dw_i = (f_wb - y[i]) * x[i]
dj_db_i = f_wb - y[i]
dj_db += dj_db_i
dj_dw += dj_dw_i
dj_dw = dj_dw / m
dj_db = dj_db / m
return dj_dw,dj_db
plt_gradients(x,y,costFun,gradientFun)
plt.show()
def gradient_descent(x,y,w_in,b_in,a,num_item,costFun,gradientFun):
w = copy.deepcopy(w_in) #避免改变原来的w
# 用数组存储用于在每次迭代时存储成本 J 和 w,主要用于以后绘制图形
J_history = []
p_history = []
b = b_in
w = w_in
for i in range(num_item):
#计算梯度
dj_dw,dj_db = gradientFun(x,y,w,b)
# 更新w,b
b = b - a * dj_db
w = w - a * dj_dw
# 保存每一次成本J
if i < 100000:#避免资源浪费
J_history.append(costFun(x,y,w,b))
p_history.append([w,b])
#输出10次结果,打印出来
#math.ceil(a),取a的最小整数 4.2取5
if i % math.ceil(num_item/10) == 0:
'''
Iteration {i:4}::表示当前循环迭代次数,用 4 位数字的方式呈现,比如第一次迭代就是 "Iteration 1:"。
Cost {J_history[-1]:0.2e}:表示模型当前的代价函数值,用科学计数法表示,保留两位小数。
dj_dw: {dj_dw: 0.3e}, dj_db: {dj_db: 0.3e}:表示代价函数对权值和偏置的梯度值,用科学计数法表示,保留三位小数。
w: {w: 0.3e}, b:{b: 0.5e}:表示当前的权值 w 和偏置 b 的值,用科学计数法表示,分别保留三位和五位小数。
'''
print(f"Iteration {i:4}: Cost {J_history[-1]:0.2e} ",
f"dj_dw: {dj_dw: 0.3e}, dj_db: {dj_db: 0.3e} ",
f"w: {w: 0.3e}, b:{b: 0.5e}")
return w,b,J_history,p_history
# 初始化
w_init = 0
b_init = 0
iterations = 10000
tmp_alpha = 1.0e-2
#
w_final, b_final, J_hist, p_hist = gradient_descent(x ,y, w_init, b_init, tmp_alpha, iterations, costFun, gradientFun)
print(f"(w,b) found by gradient descent: ({w_final:8.4f},{b_final:8.4f})")
Iteration 0: Cost 7.93e+04 dj_dw: -6.500e+02, dj_db: -4.000e+02 w: 6.500e+00, b: 4.00000e+00
Iteration 1000: Cost 3.41e+00 dj_dw: -3.712e-01, dj_db: 6.007e-01 w: 1.949e+02, b: 1.08228e+02
Iteration 2000: Cost 7.93e-01 dj_dw: -1.789e-01, dj_db: 2.895e-01 w: 1.975e+02, b: 1.03966e+02
Iteration 3000: Cost 1.84e-01 dj_dw: -8.625e-02, dj_db: 1.396e-01 w: 1.988e+02, b: 1.01912e+02
Iteration 4000: Cost 4.28e-02 dj_dw: -4.158e-02, dj_db: 6.727e-02 w: 1.994e+02, b: 1.00922e+02
Iteration 5000: Cost 9.95e-03 dj_dw: -2.004e-02, dj_db: 3.243e-02 w: 1.997e+02, b: 1.00444e+02
Iteration 6000: Cost 2.31e-03 dj_dw: -9.660e-03, dj_db: 1.563e-02 w: 1.999e+02, b: 1.00214e+02
Iteration 7000: Cost 5.37e-04 dj_dw: -4.657e-03, dj_db: 7.535e-03 w: 1.999e+02, b: 1.00103e+02
Iteration 8000: Cost 1.25e-04 dj_dw: -2.245e-03, dj_db: 3.632e-03 w: 2.000e+02, b: 1.00050e+02
Iteration 9000: Cost 2.90e-05 dj_dw: -1.082e-03, dj_db: 1.751e-03 w: 2.000e+02, b: 1.00024e+02
(w,b) found by gradient descent: (199.9929,100.0116)
w_final
199.99285075131766
b_final
100.011567727362
np.arange(len(J_hist[1000:]))
array([ 0, 1, 2, ..., 8997, 8998, 8999])
J_hist[1000:]
[3.4125109319154174,
3.4075339615520726,
3.4025642498421234,
3.3976017861991608,
3.3926465600524,
3.3876985608460113,
3.3827577780401223,
3.3778242011100392,
3.37289781954613,
3.367978622854467,
3.3630666005563263,
3.358161742188174,
3.353264037301804,
3.348373475464178,
3.3434900462575907,
3.3386137392793622,
3.333744544142247,
3.3288824504740164,
3.3240274479172975,
3.319179526130373,
3.31433867478602,
3.3095048835726657,
3.3046781421931897,
3.2998584403659352,
3.2950457678240683,
3.290240114315667,
3.2854414696039216,
3.2806498234668764,
3.275865165697401,
3.2710874861033212,
3.266316774507336,
3.261553020747037,
3.2567962146747838,
3.2520463461576252,
3.2473034050776732,
3.2425673813314084,
3.2378382648303865,
3.2331160455007186,
3.2284007132832904,
3.223692258133517,
3.2189906700216535,
3.214295938932412,
3.209608054865089,
3.204927007833841,
3.200252787867142,
3.1955853850080693,
3.1909247893141415,
3.186270990857514,
3.1816239797248214,
3.1769837460170907,
3.172350279849788,
3.1677235713527,
3.16310361067029,
3.158490387961093,
3.1538838933981594,
3.1492841171689037,
3.1446910494748352,
3.1401046805321284,
3.135525000570744,
3.1309519998352693,
3.1263856685845015,
3.1218259970911504,
3.117272975642429,
3.11272659453956,
3.108186844097903,
3.10365371464715,
3.0991271965307154,
3.094607280106324,
3.0900939557459446,
3.0855872138351903,
3.0810870447741032,
3.0765934389762064,
3.072106386869655,
3.067625878896016,
3.0631519055111207,
3.0586844571846203,
3.054223524399916,
3.049769097654613,
3.0453211674598286,
3.040879724340739,
3.036444758836324,
3.032016261499277,
3.0275942228961528,
3.0231786336070874,
3.0187694842263766,
3.0143667653613875,
3.0099704676337558,
3.0055805816786347,
3.00119709814459,
2.996820007694243,
2.992449301003424,
2.9880849687619087,
2.9837270016726674,
2.97937539045276,
2.9750301258322214,
2.9706911985550035,
2.966358599378348,
2.9620323190731908,
2.957712348423575,
2.953398678227285,
2.949091299295567,
2.944790202452743,
2.940495378536765,
2.936206818398955,
2.931924512903808,
2.9276484529294367,
2.9233786293668778,
2.9191150331208293,
2.914857655108877,
2.9106064862620995,
2.9063615175249096,
2.90212273985456,
2.8978901442218676,
2.8936637216105865,
2.8894434630176526,
2.885229359453147,
2.8810214019403677,
2.876819581515622,
2.8726238892281297,
2.868434316140496,
2.864250853328098,
2.860073491879459,
2.855902222895912,
2.851737037492091,
2.847577926795407,
2.8434248819460874,
2.8392778940975587,
2.8351369544159173,
2.8310020540800975,
2.8268731842822388,
2.8227503362270365,
2.8186335011320143,
2.814522670227773,
2.8104178347572795,
2.806318985976677,
2.8022261151546015,
2.7981392135725653,
2.7940582725246617,
2.7899832833178166,
2.785914237271539,
2.7818511257181373,
2.77779394000235,
2.7737426714817293,
2.769697311526311,
2.76565785151871,
2.761624282854264,
2.757596596940634,
2.753574785198297,
2.7495588390599024,
2.7455487499708995,
2.741544509389043,
2.7375461087845703,
2.7335535396401065,
2.7295667934509513,
2.7255858617245337,
2.721610735980633,
2.7176414077517803,
2.713677868582396,
2.70972011002953,
2.7057681236624123,
2.701821901062663,
2.6978814338240595,
2.6939467135527417,
2.6900177318670173,
2.6860944803975206,
2.682176950786979,
2.6782651346903776,
2.6743590237748984,
2.6704586097197365,
2.666563884216295,
2.6626748389682704,
2.6587914656911997,
2.654913756112791,
2.6510417019729715,
2.6471752950233878,
2.6433145270279583,
2.63945938976262,
2.635609875015228,
2.6317659745855457,
2.6279276802855907,
2.6240949839388854,
2.620267877381224,
2.6164463524601738,
2.612630401035191,
2.60882001497762,
2.605015186170629,
2.601215906509361,
2.597422167900566,
2.593633962262954,
2.5898512815269963,
2.5860741176348876,
2.5823024625405218,
2.5785363082096873,
2.5747756466197433,
2.571020469759903,
2.567270769630955,
2.5635265382453083,
2.559787767627146,
2.5560544498122124,
2.5523265768477907,
2.548604140792952,
2.5448871337181913,
2.5411755477056905,
2.537469374849053,
2.533768607253344,
2.530073237035558,
2.52638325632365,
2.5226986572574615,
2.519019431988079,
2.515345572678102,
2.511677071501515,
2.5080139206437826,
2.5043561123018296,
2.500703638683789,
2.497056492009247,
2.4934146645092006,
2.4897781484257164,
2.486146936012548,
2.4825210195345155,
2.4789003912678376,
2.4752850434997966,
2.4716749685291957,
2.46807015866589,
2.46447060623093,
2.4608763035566334,
2.457287242986589,
2.453703416875383,
2.4501248175888346,
2.4465514375039277,
2.442983269008734,
2.4394203045023666,
2.435862536395175,
2.4323099571082762,
2.4287625590742548,
2.425220334736461,
2.4216832765493344,
2.4181513769782312,
2.4146246284996296,
2.4111030236009796,
2.4075865547805897,
2.4040752145476896,
2.4005689954225744,
2.3970678899364284,
2.3935718906312182,
2.390080990059745,
2.386595180786016,
2.383114455384297,
2.379638806440345,
2.376168226550228,
2.372702708321022,
2.3692422443706516,
2.365786827327556,
2.3623364498311763,
2.3588911045315886,
2.355450784089657,
2.3520154811766756,
2.3485851884751305,
2.3451598986776694,
2.341739604487863,
2.3383242986199373,
2.3349139737986553,
2.331508622759344,
2.3281082382481375,
2.324712813021514,
2.321322339846591,
2.3179368115012076,
2.3145562207733343,
2.311180560461945,
2.3078098233762563,
2.30444400233589,
2.3010830901711126,
2.297727079722494,
2.2943759638412833,
2.291029735388923,
2.2876883872373894,
2.284351912268903,
2.281020303376362,
2.2776935534626928,
2.274371655441451,
2.271054602236327,
2.2677423867813506,
2.2644350020210045,
2.2611324409099987,
2.2578346964132208,
2.2545417615058256,
2.2512536291734704,
2.2479702924117033,
2.244691744226397,
2.241417977633784,
2.2381489856601484,
2.2348847613418443,
2.231625297725593,
2.228370587868132,
2.225120624836327,
2.2218754017072255,
2.218634911567877,
2.2153991475154458,
2.212168102657224,
2.2089417701105214,
2.2057201430026483,
2.202503214470976,
2.1992909776628493,
2.196083425735718,
2.1928805518568018,
2.189682349203448,
2.1864888109630334,
2.183299930332582,
2.1801157005193144,
2.1769361147401867,
2.1737611662222185,
2.1705908482020946,
2.1674251539265432,
2.1642640766520986,
2.1611076096450224,
2.15795574618153,
2.1548084795475098,
2.1516658030388336,
2.1485277099609896,
2.145394193629265,
2.142265247368767,
2.1391408645141885,
2.136021038410157,
2.132905762410877,
2.1297950298801616,
2.1266888341916568,
2.1235871687286156,
2.120490026883905,
2.1173974020601136,
2.1143092876693728,
2.111225677133439,
2.1081465638836554,
2.105071941361052,
2.1020018030160617,
2.0989361423087174,
2.0958749527087157,
2.0928182276950484,
2.0897659607564503,
2.086718145390985,
2.0836747751063642,
2.080635843419612,
2.0776013438571983,
2.074571269955143,
2.0715456152589113,
2.068524373323161,
2.0655075377123877,
2.0624951019998896,
2.059487059768802,
2.0564834046115057,
2.053484130129518,
2.0504892299340494,
2.0474986976452003,
2.0445125268928077,
2.041530711315742,
2.038553244562165,
2.0355801202894965,
2.032611332164592,
2.0296468738633022,
2.0266867390708017,
2.0237309214816013,
2.020779414799092,
2.0178322127362094,
2.014889309014802,
2.01195069736592,
2.009016371529871,
2.006086325255993,
2.003160552302867,
2.0002390464378976,
1.997321801437852,
1.9944088110884546,
1.9915000691845917,
1.988595569530015,
1.9856953059376923,
1.9827992722295154,
1.9799074622365413,
1.9770198697984813,
1.9741364887644,
1.9712573129921345,
1.9683823363485624,
1.9655115527094034,
1.9626449559593937,
1.9597825399923057,
1.9569242987104816,
1.9540702260255203,
1.9512203158576344,
1.9483745621360915,
1.9455329587988581,
1.942695499792889,
1.9398621790738722,
1.9370329906061428,
1.9342079283633562,
1.9313869863273618,
1.9285701584892319,
1.9257574388485352,
1.9229488214136352,
1.9201443002016894,
1.9173438692386826,
1.9145475225591013,
1.9117552542062457,
1.9089670582321099,
1.9061829286972212,
1.9034028596711572,
1.9006268452315997,
1.8978548794652974,
1.8950869564674286,
1.8923230703417846,
1.8895632152007935,
1.8868073851655645,
1.8840555743656453,
1.8813077769392428,
1.878563987032887,
1.8758241988019926,
1.8730884064103055,
1.87035660403003,
1.8676287858419174,
1.8649049460352924,
1.862185078807907,
1.8594691783659552,
1.85675723892397,
1.854049254705223,
1.851345219941089,
1.848645128871501,
1.8459489757449323,
1.8432567548179017,
1.8405684603556471,
1.8378840866315196,
1.8352036279274606,
1.8325270785333965,
1.8298544327480077,
1.8271856848779207,
1.8245208292384134,
1.8218598601526925,
1.8192027719525867,
1.8165495589778478,
1.8139002155767514,
1.8112547361057687,
1.8086131149294817,
1.8059753464208401,
1.803341424960808,
1.8007113449387844,
1.79808510075221,
1.7954626868066532,
1.792844097515881,
1.7902293273020318,
1.78761837059495,
1.7850112218329697,
1.7824078754623822,
1.7798083259375819,
1.77721256772102,
1.7746205952833016,
1.7720324031030823,
1.769447985667044,
1.7668673374698827,
1.7642904530144579,
1.761717326811438,
1.759147953379632,
1.7565823272458836,
1.7540204429449089,
1.751462295019411,
1.7489078780200729,
1.7463571865056304,
1.7438102150425374,
1.7412669582054303,
1.7387274105764998,
1.7361915667462797,
1.7336594213128358,
1.73113096888231,
1.72860620406864,
1.7260851214935966,
1.7235677157868667,
1.7210539815859498,
1.7185439135360734,
1.7160375062903341,
1.7135347545098139,
1.7110356528629596,
1.7085401960263709,
1.7060483786843244,
1.7035601955287452,
1.701075641259325,
1.6985947105835264,
1.6961173982166333,
1.6936436988814605,
1.6911736073084986,
1.6887071182361537,
1.6862442264103228,
1.6837849265846034,
1.681329213520194,
1.6788770819860743,
1.6764285267587202,
1.6739835426223646,
1.6715421243685622,
1.669104266796815,
1.6666699647140555,
1.664239212934623,
1.6618120062808357,
1.6593883395820843,
1.6569682076757115,
1.6545516054062444,
1.652138527625929,
1.6497289691945825,
1.6473229249793078,
1.6449203898549272,
1.6425213587034375,
1.6401258264146046,
1.6377337878854554,
1.635345238020602,
1.632960171731907,
1.630578583938836,
1.6282004695681471,
1.6258258235540328,
1.6234546408380819,
1.6210869163692216,
1.6187226451038308,
1.616361822005583,
1.6140044420454933,
1.6116505002019037,
1.6092999914605386,
1.6069529108143537,
1.6046092532636582,
1.6022690138160431,
1.5999321874864234,
1.5975987692968634,
1.5952687542768906,
1.5929421374630774,
1.5906189138993043,
1.5882990786366733,
1.585982626733636,
1.5836695532556258,
1.5813598532754276,
1.5790535218729636,
1.5767505541353755,
1.5744509451568602,
1.5721546900388066,
1.5698617838900035,
1.567572221825921,
1.5652859989694847,
1.563003110450553,
1.5607235514062854,
1.5584473169807624,
1.556174402325217,
1.5539048025979285,
1.5516385129641963,
1.5493755285964275,
1.5471158446741222,
1.5448594563837705,
1.5426063589188308,
1.5403565474797043,
1.5381100172740463,
1.5358667635162564,
1.533626781427843,
1.5313900662371887,
1.5291566131797654,
1.5269264174979456,
1.524699474440873,
1.522475779264901,
1.520255327233093,
1.51803811361555,
1.5158241336891256,
1.5136133827377607,
1.511405856052038,
1.5092015489296404,
1.5070004566749475,
1.5048025745992417,
1.5026078980206394,
1.5004164222641405,
1.4982281426614503,
1.496043054551199,
1.4938611532786708,
1.49168243419608,
1.4895068926624404,
1.4873345240433435,
1.4851653237113676,
1.4829992870456001,
1.4808364094321556,
1.4786766862635359,
1.4765201129392729,
1.474366684865467,
1.4722163974549,
1.4700692461271443,
1.4679252263082538,
1.4657843334312441,
1.463646562935595,
1.4615119102673775,
1.4593803708794242,
1.4572519402312574,
1.4551266137889225,
1.453004387025065,
1.45088525541893,
1.4487692144564601,
1.446656259630086,
1.4445463864388597,
1.4424395903882805,
1.4403358669906043,
1.4382352117645019,
1.436137620235172,
1.4340430879344495,
1.43195161040052,
1.4298631831781898,
1.427777801818768,
1.4256954618799715,
1.423616158926086,
1.4215398885278132,
1.4194666462623688,
1.4173964277132174,
1.415329228470643,
1.4132650441310104,
1.411203870297291,
1.409145702578773,
1.4070905365912179,
1.4050383679568126,
1.4029891923040174,
1.4009430052676994,
1.3988998024891177,
1.3968595796159702,
1.3948223323021787,
1.3927880562079595,
1.3907567470000353,
1.3887284003513123,
1.3867030119410846,
1.3846805774548796,
1.3826610925845944,
1.3806445530283473,
1.3786309544906261,
1.37662029268193,
1.3746125633192727,
1.3726077621259465,
1.3706058848312033,
1.3686069271707617,
1.3666108848865361,
1.364617753726538,
1.3626275294450223,
1.3606402078025064,
1.3586557845656568,
1.3566742555072377,
1.3546956164062718,
1.3527198630478918,
1.3507469912234076,
1.3487769967302792,
1.3468098753719793,
1.3448456229582688,
1.3428842353049881,
1.3409257082339119,
1.3389700375730968,
1.337017219156566,
1.3350672488245074,
1.3331201224231588,
1.331175835804773,
1.329234384827643,
1.3272957653561517,
1.3253599732607455,
1.3234270044178347,
1.3214968547096886,
1.319569520024975,
1.3176449962579895,
1.3157232793092075,
1.3138043650850635,
1.3118882494978095,
1.309974928465819,
1.30806439791351,
1.3061566537709282,
1.3042516919743812,
1.302349508465887,
1.3004500991935006,
1.2985534601110136,
1.296659587178417,
1.2947684763613327,
1.2928801236313876,
1.290994524966104,
1.2891116763487125,
1.2872315737684674,
1.285354213220384,
1.283479590705395,
1.2816077022302463,
1.2797385438074165,
1.2778721114552822,
1.2760084011979709,
1.274147409065569,
1.2722891310937656,
1.2704335633239912,
1.2685807018037523,
1.2667305425859943,
1.2648830817296013,
1.2630383152990985,
1.2611962393648586,
1.2593568500028798,
1.25752014329508,
1.2556861153287238,
1.2538547621972091,
1.2520260799992444,
1.250200064839556,
1.2483767128284062,
1.246556020081602,
1.2447379827208493,
1.2429225968734208,
1.2411098586721325,
1.239299764255558,
1.2374923097679744,
1.2356874913590599,
1.2338853051842307,
1.2320857474045877,
1.2302888141867179,
1.2284945017028241,
1.2267028061306955,
1.2249137236537586,
1.2231272504608803,
1.2213433827465896,
1.2195621167108983,
1.2177834485594536,
1.2160073745033246,
1.214233890759172,
1.2124629935491698,
1.2106946791009705,
1.20892894364779,
1.2071657834282215,
1.2054051946864932,
1.2036471736721954,
1.2018917166404846,
1.2001388198518725,
1.198388479572395,
1.1966406920735004,
1.1948954536321668,
1.193152760530591,
1.1914126090566306,
1.1896749955033932,
1.1879399161695128,
1.186207367358945,
1.1844773453810713,
1.182749846550515,
1.1810248671875265,
1.17930240361755,
1.1775824521714267,
1.1758650091853928,
1.1741500710009298,
1.1724376339649383,
1.1707276944296472,
1.1690202487526256,
1.1673152932965862,
1.1656128244296982,
1.1639128385254875,
1.1622153319625963,
1.1605203011250547,
1.1588277424021574,
1.1571376521884815,
1.1554500268837704,
1.15376486289319,
1.1520821566268833,
1.150401904500526,
1.1487241029347557,
1.1470487483556762,
1.1453758371944749,
1.1437053658874488,
1.142037330876294,
1.140371728607763,
1.1387085555338181,
1.1370478081116455,
1.1353894828034976,
1.133733576076895,
1.1320800844044623,
1.1304290042639669,
1.1287803321382546,
1.1271340645154038,
1.1254901978886067,
1.1238487287561205,
1.122209653621252,
1.1205729689925636,
1.1189386713836014,
1.1173067573129924,
1.1156772233045027,
1.1140500658869255,
1.112425281594152,
1.110802866965032,
1.1091828185435935,
1.10756513287884,
1.1059498065247835,
1.104336836040472,
1.1027262179900998,
1.1011179489427017,
1.0995120254723296,
1.09790844415816,
1.0963072015841706,
1.0947082943395892,
1.0931117190183115,
1.0915174722193979,
1.089925550546888,
1.088335950609625,
1.086748669021485,
1.0851637024013003,
1.0835810473728023,
1.0820007005646266,
1.0804226586104457,
1.078846918148629,
1.0772734758225808,
1.0757023282807339,
1.0741334721761047,
1.0725669041668058,
1.071002620915807,
1.069440619090837,
1.0678808953645933,
1.0663234464145748,
1.0647682689231859,
1.0632153595775682,
1.0616647150697145,
1.060116332096571,
1.0585702073597147,
1.057026337565733,
1.0554847194257944,
1.0539453496560622,
1.0524082249772952,
1.0508733421153125,
1.049340697800398,
1.0478102887678598,
1.0462821117575114,
1.0447561635141844,
1.0432324407873335,
1.041710940331041,
1.040191658904375,
1.0386745932709511,
1.0371597401991184,
1.0356470964619875,
1.034136658837413,
1.0326284241077668,
1.0311223890603904,
1.0296185504870623,
1.0281169051843762,
1.026617449953529,
1.0251201816004454,
1.0236250969356342,
1.0221321927744187,
1.0206414659364906,
1.019152913246478,
1.0176665315333766,
1.0161823176309357,
1.014700268377637,
1.0132203806163864,
1.0117426511946892,
1.010267076964861,
1.0087936547834966,
1.0073223815120849,
1.0058532540164724,
1.0043862691671812,
1.0029214238393325,
1.0014587149124454,
0.9999981392707153,
0.9985396938028983,
0.9970833754022091,
0.99562918096646,
0.9941771073979384,
0.9927271516034573,
0.9912793104944122,
0.9898335809865442,
0.9883899600002313,
0.9869484444603742,
0.9855090312962311,
0.9840717174416118,
0.9826364998347301,
0.9812033754183948,
0.9797723411397214,
0.978343393950438,
0.9769165308065633,
0.9754917486685946,
0.9740690445015885,
0.9726484152749199,
0.9712298579623377,
0.9698133695420526,
0.9683989469967724,
0.9669865873134529,
0.9655762874835606,
0.9641680445028672,
0.9627618553716659,
0.961357717094345,
0.959955626679996,
0.9585555811418633,
0.9571575774975649,
0.9557616127691551,
0.9543676839829662,
0.9529757881696965,
0.9515859223643464,
0.950198083606228,
0.9488122689390448,
0.9474284754107594,
0.9460467000735591,
0.9446669399841504,
0.943289192203318,
0.9419134537962062,
0.9405397218323366,
0.9391679933852926,
0.9377982655331222,
0.9364305353580523,
0.9350647999465929,
0.9337010563894155,
0.9323393017815624,
0.9309795332222555,
0.9296217478149463,
0.928265942667261,
0.9269121148911093,
0.92556026160266,
0.9242103799221932,
0.9228624669741721,
0.9215165198873739,
0.9201725357945696,
0.9188305118329926,
0.9174904451438306,
0.9161523328724968,
0.9148161721685155,
0.9134819601857576,
0.9121496940820248,
0.9108193710193233,
0.9094909881638846,
0.9081645426860162,
0.9068400317601364,
0.905517452564816,
0.904196802282702,
0.9028780781006494,
0.901561277209408,
0.9002463968040625,
0.8989334340836768,
0.8976223862513415,
0.8963132505143621,
0.8950060240840181,
0.8937007041756968,
0.8923972880087769,
0.8910957728068012,
0.8897961557973054,
0.8884984342118918,
0.887202605286179,
0.8859086662598021,
0.8846166143764344,
0.88332644688375,
0.8820381610335799,
0.8807517540815355,
0.8794672232873175,
0.8781845659147811,
0.8769037792314756,
0.8756248605092222,
0.8743478070236623,
0.8730726160544332,
0.8717992848851553,
0.870527810803368,
0.8692581911007052,
0.8679904230725347,
0.8667245040183496,
0.8654604312415366,
0.8641982020493466,
0.862937813753032,
0.8616792636677018,
0.8604225491124966,
0.8591676674103519,
0.8579146158881235,
0.8566633918766202,
0.8554139927104751,
0.8541664157283086,
0.8529206582725051,
0.8516767176894023,
0.8504345913291713,
0.84919427654588,
0.8479557706974297,
0.8467190711456148,
0.8454841752559574,
0.8442510803980122,
0.8430197839450063,
0.8417902832740182,
0.8405625757661197,
0.8393366588059579,
0.8381125297821685,
0.8368901860871673,
0.8356696251170724,
0.8344508442718771,
0.8332338409553929,
0.8320186125752782,
0.8308051565426767,
0.8295934702728918,
0.8283835511846916,
0.8271753967007793,
0.8259690042476352,
0.824764371255315,
0.8235614951577938,
0.8223603733927707,
0.8211610034015532,
0.8199633826293484,
0.8187675085249783,
0.8175733785410062,
0.8163809901337584,
0.8151903407632113,
0.8140014278930503,
0.8128142489907524,
0.8116288015273257,
0.8104450829775762,
0.8092630908200424,
0.8080828225368222,
0.8069042756136928,
0.8057274475402256,
0.8045523358095301,
0.8033789379183327,
0.8022072513672126,
0.8010372736601552,
0.7998690023050057,
0.7987024348130486,
0.7975375686993745,
0.7963744014825843,
0.7952129306848713,
0.7940531538321492,
...]
fig, (ax1, ax2) = plt.subplots(1, 2, constrained_layout=True, figsize=(12,4))
ax1.plot(J_hist[:100])
ax2.plot(1000 + np.arange(len(J_hist[1000:])), J_hist[1000:])
ax1.set_title("Cost vs. iteration(start)"); ax2.set_title("Cost vs. iteration (end)")
ax1.set_ylabel('Cost') ; ax2.set_ylabel('Cost')
ax1.set_xlabel('iteration step') ; ax2.set_xlabel('iteration step')
plt.show()
print(f"1000 sqft house prediction {w_final*1.0 + b_final:0.1f} Thousand dollars")
print(f"1200 sqft house prediction {w_final*1.2 + b_final:0.1f} Thousand dollars")
print(f"2000 sqft house prediction {w_final*2.0 + b_final:0.1f} Thousand dollars")
1000 sqft house prediction 300.0 Thousand dollars
1200 sqft house prediction 340.0 Thousand dollars
2000 sqft house prediction 500.0 Thousand dollars