吴恩达471机器学习入门课程1第1周

news2024/11/15 19:28:07

文章目录

  • 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/635144.html

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

相关文章

OpenMMLab-AI实战营第二期——4-2.MMDetection代码课

文章目录 1. MMDetection介绍&#xff08;vs MMSegmentation&#xff0c;mmdetection3d&#xff09;2. 代码2.1 使用pycocotools配合exif可视化图像2.2 mmdetection中config的继承2.3 mmdet的数据集可视化问题&#xff08;VISUALIZERS&#xff09;2.4 pin_memory参数设置2.5 ma…

康佳液晶电视(非智能)Mstar V56 芯片

&#xfffc;&#xfffc; &#xfffc; &#xfffc; &#xfffc; 2023/6/11 下午8:10:01 康佳用的地面波数字电视解码芯片是&#xff1f; &#xfffc; 2023/6/11 下午8:10:08 康佳电视使用的地面波数字电视解码芯片可能因不同的产品型号而有所不同。然而&#xff0c;根据…

【AI作画】使用DiffusionBee with stable-diffusion在mac M1平台玩AI作画

DiffusionBee是一个完全免费、离线的工具。它简洁易用&#xff0c;你只需输入一些标签或文本描述&#xff0c;它就能生成艺术图像。 DiffusionBee下载地址 运行DiffusionBee的硬性要求&#xff1a;MacOS系统版本必须在12.3及以上 DBe安装完成后&#xff0c;去C站挑选自己喜欢…

ur机器人在moveit中运行环境搭建(保姆级)

ur机器人在moveit中运行环境搭建 我的系统是ubuntu20.04 rosb版本: noetic 1.安装运动学插件 sudo apt-get install ros-noetic-trac-ik-kinematics-plugin 2.安装 eigenpy 需要单独编译,EigenPy是一个用于在Python中使用Eigen库的绑定库 git clone https://github.com/…

[MAUI]写一个跨平台富文本编辑器

文章目录 原理创建编辑器定义实现复合样式选择范围字号字体颜色与背景色字体下划线字体加粗与斜体 序列化和反序列化跨平台实现集成至编辑器 创建控件使用控件最终效果已知问题项目地址 富文本编辑器是一种所见即所得(what you see is what you get 简称 WYSIWYG)文本编辑器&am…

visual studio 2022,ADO.NET 实体数据模型添加 sqlite数据库对象

文章目录 前言前期环境博客github 文档解析文件安装说明文件下载省流版nuget环境配置成功标志sqlite连接测试 前言 我们知道ADO.NET 实体数据模型特别适合动态开发数据库。因为ADO.NET可以使用DB First 开发 我们在开发一个程序的时候&#xff0c;经常会动态更新数据库字段&a…

Python的基础语法知识

1、变量 变量是一个代号&#xff0c;它代表的是一个数据。 在Python中&#xff0c;定义一个变量的操作包含两个步骤&#xff1a; ①为变量起一个名字 ②为变量指定其所代表的数据 这两个步骤在同一行代码中完成。 1.1 变量的命名规则 变量名可以由任意数量的字母、数字、下划…

Unity 简易UI管理器

首先我们需要先定义这么一个UIManager类。 public class UIManager { } UI管理器嘛&#xff0c;顾名思义肯定是用来管理我们游戏中的UI的&#xff0c;而我们游戏当中的UI呢一般是以面板为单位来进行划分的。所以我们还需要一个UI面板类。然后通过我们的UI管理器来管理我们的U…

Linux——创建容器并将本地调试完全的前后端分离项目打包上传docker运行

前言 在上传之前需要有一个已经搭建好的前后端分离的项目&#xff0c;下面是后端的项目结构图和前端页面图 在服务器上利用准备好的docker镜像配置一个新的容器 创建容器 这里使用的docker镜像的OS是ubuntu20.04.需要自备。 注意好端口映射: 通常前端项目使用的端口号一般都…

【DeepSpeed 教程翻译】二,Megatron-LM GPT2,Zero Redundancy Optimizer 和 ZeRO-Offload

文章目录 0x0. 前言0x1. Megatron-LM GPT2使用原始的 Megatron-LM 训练 GPT2设置训练数据运行未修改的Megatron-LM GPT2模型开启DeepSpeed参数解析初始化和训练初始化使用训练API前向传播 反向传播更新模型参数损失缩放检查点保存和加载 DeepSpeed Activation Checkpoints&…

异常检测学习笔记 三、线性回归方法、主成分分析、支持向量机

一、线性回归方法 类似这样的函数是线性回归模型和支持向量机的基础,线性函数很简单,如果原始问题是非线性的,那么将其转化为线性问题更容易处理,比如下面的方程。 线性映射是主成分分析的重要组成部分。 寻找响应(因变量)和解释变量(自变量)之间的线性关系,…

python实现图片、gif转为字符样式图与gif,pyqt5、opencv、PIL

使用pyqt5将图片转换为字符样式的图片步骤如下&#xff1a; 设计pyqt5界面&#xff0c;使用Qt Designer 设计界面样式 将ui文件转换为py代码 书写相关按钮信号槽代码打开图片按钮需要一下逻辑步骤弹出选择路径的界面来选择文件保存 图片的路径&#xff0c;能在转换保存的按钮…

数字IC前端学习笔记:FIFO的Verilog实现(二)

相关文章 数字IC前端学习笔记&#xff1a;LSFR&#xff08;线性反馈移位寄存器&#xff09; 数字IC前端学习笔记&#xff1a;跨时钟域信号同步 数字IC前端学习笔记&#xff1a;信号同步和边沿检测 数字IC前端学习笔记&#xff1a;锁存器Latch的综合 数字IC前端学习笔记&am…

最新站长必备在线工具箱系统源码 含上百款工具 带后台版本

&#x1f388; 限时活动领体验会员&#xff1a;可下载程序网创项目短视频素材 &#x1f388; 最新站长必备在线工具箱系统源码 含上百款工具 带后台版本 自适应模板 优化修复版 系统一切正常可用&#xff0c;后台登录方式是QQ扫码登录的,建议有能力的可以改一改 此工具箱系统…

【LVS + Keepalived 群集】

目录 一、Keepalived 案列分析二、Keeoalived 工具介绍1、keepalived 实现原理剖析2、VRRP &#xff08;虚拟路由冗余协议&#xff09;是指对路由器的一种备份解决方案3、keepalived 案例讲解4、keepalived 的安装与启动5、Keepalived及其工作原理Keepalived体系主要模块及其作…

SQL锁总结

一、概述 介绍 锁是计算机协调多个进程或线程并发访问某一资源的机制。在数据库中&#xff0c;除传统的计算资源(CPU、RAM、I/O)的争用以外&#xff0c;数据也是一种供许多用户共享的资源。如何保证数据并发访问的一致性、有效性是所有数据库必须解决的一个问题&#xff0c;锁…

springboot3嵌入式容器源码解析

问题分析 不同于使用springmvc,在我们使用springboot时无需配置tomcat就可以直接使用&#xff0c;这就说明springboot已经在我们启动项目时将tomcat配置好了&#xff0c;接下来我们就来看看springboot底层是怎么实现的。 源码解析 ServletWebServerFactoryAutoConfiguratio…

【Python爬虫】整站40万条房价数据并行抓取,可更换抓取城市

目录 前言一、获取索引一级位置&#xff1a;区域信息二级位置&#xff1a;板块信息三级位置&#xff1a;地铁信息&#xff08;搜索地铁周边房源信息&#xff09; 二、获取索引页最大页数三、抓取房源信息Tag四、分配任务&#xff0c;并行抓取五、将抓取结果存储到excel中&#…

go map源码探索(查找、插入、删除、扩容、遍历)

文章目录 概要一、Go map结构二、Go map初始化2.1、不带容量初始化2.2、带容量初始化 三、Go map查找四、Go map插入4.1、插入源码分析4.2、溢出桶申请策略 五、删除源码分析六、扩容与迁移源码分析6.1、扩容条件6.1.1、当前负载因子大于6.56.1.2、有过多的溢出桶 6.2、扩容6.3…

LVS+Keepalivedd

Keepalived 一、Keepalived及其工作原理二、实验非抢占模式的设置 三、脑裂现象四、Nginx高可用模式 一、Keepalived及其工作原理 keepalived是一个基于VRRP协议来实现的LVS服务高可用方案&#xff0c;可用解决静态路由出现的单点故障问题。 在一个LVS服务集群中通常有主服务器…