吴恩达471机器学习入门课程1第1周——梯度下降

news2024/11/24 4:01:32

文章目录

  • 1加载数据集
  • 2计算COST(均值平方差,1/2m(y_pre - y)²)
  • 3计算梯度
  • 4画出成本曲线
  • 5梯度下降

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

1加载数据集

# train data
x = np.array([1.0,2.0])
y = np.array([300.0,500.0])

2计算COST(均值平方差,1/2m(y_pre - y)²)

#通过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

3计算梯度

∂ 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} wJ(w,b)bJ(w,b)=m1i=0m1(fw,b(x(i))y(i))x(i)=m1i=0m1(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

4画出成本曲线

plt_gradients(x,y,costFun,gradientFun)
plt.show()

png

5梯度下降

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()


png

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/648684.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

华为OD机试真题 JavaScript 实现【找出通过车辆最多颜色】【2023Q1 100分】

一、题目描述 在一个狭小的路口&#xff0c;每秒只能通过一辆车&#xff0c;假如车辆的颜色只有3种&#xff0c;找出n秒内经过的最多颜色的车辆数量。 三种颜色编号为0、1、2。 二、输入描述 第一行输入的是通过的车辆颜色信息 [0 1 1 2] 代表4秒钟通过的车辆颜色分别是0 1…

手把手教你使用CONN(预处理)

CONN软件介绍 &#xff08;1&#xff09;CONN是一个基于Matlab的跨平台软件&#xff0c;用于计算、显示和分析功能磁共振成像&#xff08;fcMRI&#xff09;中的功能连通性。也可用于静息状态数据&#xff08;rsfMRI&#xff09;以及任务相关设计。 &#xff08;2&#xff09…

Vue的组合式

1. 概念 选项式API&#xff1a;将相同类型的代码放在一起&#xff08;比如所有数据、所有用到的方法等等&#xff09;当代码业务板块过多时&#xff0c;不方便写代码和后期维护 组合式API&#xff1a;将同一业务的相关代码放在一起&#xff08;比如说数据&#xff0c;方法&am…

什么是同源策略

文章目录 同源策略同源策略的目的同源策略分类 同源策略 同源策略是指浏览器的一种安全机制&#xff0c;用于限制来自不同源&#xff08;即域、协议或端口&#xff09;的文档或脚本之间的交互操作。 根据同源策略&#xff0c;浏览器只允许当前网页与同一源下的其他资源进行交…

Linux之CentOS 7.9部署Oracle 11g r2 静默安装实测验证(无桌面模式)

前言&#xff1a;因前段时间一直部署的windows环境的oracle&#xff0c;这次记录下linux下的部署方式&#xff0c;当然还有更多的其他部署&#xff0c;大家可根据自身环境及学习来了解。一般静默安装主要还是要提前准备源包&#xff0c;还有很多依赖包&#xff0c;另外就是配置…

如何显示文件后缀名,这4个方法很简单!

Anna最近想对电脑里的文件进行分类&#xff0c;但有些未知类型的文件&#xff0c;她想查看文件的类型并进行分类&#xff0c;可是她不知道如何显示文件后缀名&#xff0c;因此向大家求助。 在计算机操作中&#xff0c;文件的后缀名是文件名的一部分&#xff0c;用于标识文件的类…

FlinkSQL写入iceberg—Windows环境下

前置条件 Flink运行版本13.1&#xff0c;iceberg依赖版本&#xff1a;1.0.0 依赖 FlinkSQL运行环境略。 注意版本匹配&#xff0c;采用不合适版本可能导致无法读写。 <!-- Flink 操作Iceberg 需要的Iceberg依赖 --><dependency><groupId>org.apache.iceb…

shell脚本变量-特殊变量

目录 特殊变量&#xff1a;$n案例需求 特殊变量&#xff1a;$#案例需求 特殊变量&#xff1a;$*、$案例需求 特殊变量&#xff1a;$&#xff1f;特殊变量&#xff1a;$$ 特殊变量&#xff1a;$n 语法 $n含义&#xff1a; 用于接收脚本文件执行时传入的参数 $0 用于获取当前脚…

机器人系统中的六大漏洞

原创 | 文 BFT机器人 在过去的几十年里&#xff0c;创新和技术导致机器人技术不断发展。 机器人系统正在迅速变得更加多产、复杂、有能力、智能化和网络化&#xff0c;并被用于越来越多的任务。 最初&#xff0c;机器人技术领域仅限于制造领域&#xff0c;但现在机器人可以与人…

KMP算法 - 确定有限状态自动机

KMP神在哪里&#xff1f; 子串匹配问题&#xff0c;拍脑袋一下子想出来的暴力解法大抵都是两重for循环&#xff0c;不断重复扫描主串&#xff0c;与子窜进行匹配&#xff0c;重复换句话讲就是冗余&#xff0c;会有很高的时间复杂度 我先前博客大作业发的模糊查找算法就是如此&…

三分钟告诉你如何和智能ai聊天

有一个名叫艾丽的年轻女孩&#xff0c;她生活在一个科技发达的未来世界。在这个世界里&#xff0c;人们与人工智能伙伴共同生活。艾丽对ai技术充满好奇&#xff0c;尤其是对ai对话聊天工具的运作方式。为了知道ai对话聊天工具怎么用&#xff0c;艾丽决定展开探索。 方案一&…

智能无线监测器的工作原理及应用优势

在现代工业生产中&#xff0c;设备状态监测对于确保生产的安全性、效率和可靠性至关重要。随着科技的不断发展&#xff0c;智能无线监测器成为工业设备状态监测的利器。本文将介绍智能无线监测器在工业领域中的应用&#xff0c;以及其带来的优势和价值。 图.设备状态监测&#…

智驾风向标|卷、乱、难,如何穿越多极分化新周期?

竞争越来越卷&#xff0c;企业越来越难&#xff0c;市场处于混乱期。对于大多数供应商来讲&#xff0c;穿越新周期的战略一定是先有规模&#xff08;市场份额&#xff09;&#xff0c;然后才是利润。 在6月8日召开的2023&#xff08;第十四届&#xff09;高工智能汽车开发者大…

8个你必须知道的Java8新特性,让你的代码变得优雅!

Java 8 是一次重大的发行版更新&#xff0c;引入了大量新特性和改进&#xff0c;以下是 Java 8 的主要特性&#xff1a; 文章目录 Java 8 是一次重大的发行版更新&#xff0c;引入了大量新特性和改进&#xff0c;以下是 Java 8 的主要特性&#xff1a;1.Lambda 表达式2.Stream …

云平台 stm32连接阿里云2023最新版本保姆级别教学只看这一篇就够了~

注册账号 阿里云平台点击直达 点击控制台 鼠标悬浮会出现下拉栏 点击物联网 再点击物联网平台 点击公共实例 新用户需要开通 开通需要五分钟的时间 点击创建产品 蓝色显眼字体 参数设置 仔细比对下图 点击查看产品详情 蓝色显眼字体 点击功能定义 点击编辑草图 实际上就是定义…

如何通过Android平台的API实现5G网络的支持 安卓系统版本和5g网络相关【一】

前面分享了两篇5G基带相关的移植修改博文。 安卓高通机型的基带移植 修改 编译的相关 增加信号 支持5G等【一】 安卓高通机型的基带移植 修改 编译的相关 增加信号 支持5G等【二】 今天的帖子聊聊安卓版本与5G网络与机型和修改之间相关的话题。众所周知&#xff0c;目前的机型…

如何获取签章定位信息

在合同系统中&#xff0c;经常需要在合同文档的特定位置放置签名/印章图片。在合同拟稿过程中&#xff0c;放置签名/印章图片只是为了获取一个精确的定位信息&#xff0c;在合同定稿阶段才根据拟稿阶段得到的位置信息&#xff0c;去插入真正的签名/印章。那么如何在合同系统中高…

基于OpenMV的疲劳驾驶检测系统的设计

一、前言 借助平台将毕业设计记录下来&#xff0c;方便以后查看以及与各位大佬朋友们交流学习。如有问题可以私信哦。 本文主要从两个方面介绍毕业设计&#xff1a;硬件&#xff0c;软件&#xff08;算法&#xff09;。以及对最后的实验结果进行分析。感兴趣的朋友们可以评论区…

创新案例|专注在线 协作平台 设计产品中国首家PLG独角兽企业蓝湖如何实现98%的头部企业渗透率

蓝湖起步于2015年&#xff0c;是一款服务于产品经理、设计师、工程师的产品设计研发在线协作工具&#xff0c; 2021年10月&#xff0c;蓝湖宣布完成C轮融资&#xff0c;融资额高达10亿人民币&#xff0c;称为中国2B市场中首家采用PLG发展的独角兽企业&#xff0c;并实现了从100…

conda虚拟环境列表错误module ‘attr‘ has no attribute ‘s‘的解决方法

列出虚拟环境列表命令&#xff1a;conda info -e 或者conda env listconda info -e 这个可以正常显示&#xff0c;conda env list却报错了&#xff0c;以前是没有问题的&#xff0c;因为这个命令我更习惯使用&#xff0c;所以这个小问题必须解决掉&#xff0c;或许其他读者可能…