【吴恩达】机器学习作业ex5-->偏差与方差(过拟合/欠拟合)Python
一.前言
這次的作業(yè)主要目的是研究偏差和方差也就是過(guò)擬合和欠擬合的關(guān)系,數(shù)據(jù)分別是水位的變化來(lái)預(yù)測(cè)大壩流出的水量,其實(shí)和房?jī)r(jià)預(yù)測(cè)相差不大,要說(shuō)區(qū)別就是這次將X分為了三部分,分別是訓(xùn)練集,交叉集,測(cè)試集(X,Xval,Xtest)
二.代碼部分
1.數(shù)據(jù)導(dǎo)入
還是導(dǎo)入scipy,numpy,matplotlib三個(gè)工具庫(kù),分別是用于高級(jí)算法,加載數(shù)據(jù),矩陣計(jì)算以及畫(huà)圖
import numpy as np import matplotlib.pyplot as plt from scipy.io import loadmat import scipy.optimize as opt2.獲取數(shù)據(jù)
這里先加載ex5data1.mat的數(shù)據(jù),然后分別將訓(xùn)練集,交叉集,測(cè)試集分別取出做備用,同樣在三個(gè)分類(lèi)之后的數(shù)據(jù)集第一列插入1用來(lái)和theta進(jìn)行矩陣運(yùn)算
# 獲取路徑 data = loadmat('ex5data1.mat') # print(data) # 一共三組數(shù)據(jù),分別是訓(xùn)練集,交叉集以及測(cè)試集 X, y = data['X'], data['y'] # 訓(xùn)練集 Xval, yval = data['Xval'], data['yval'] # 交叉集 Xtest, ytest = data['Xtest'], data['ytest'] # 測(cè)試集 # print(f"Xtest:{Xtest},ytest:{ytest}")# 下面在頭部插入一列1,方便與theta矩陣相乘從而得到常數(shù)項(xiàng) X = np.insert(X, 0, 1, axis=1) # 此處的含義是在X矩陣中的第0列插入1,axis為0時(shí)為行,1為列 Xval = np.insert(Xval, 0, 1, axis=1) Xtest = np.insert(Xtest, 0, 1, axis=1)下面是已經(jīng)插入1之后的X數(shù)據(jù)展示
?看一下每一部分的數(shù)據(jù)集的維度
# 查看一下矩陣維度 # print(f"X.shape:{X.shape} Xval.shape:{Xval.shape} Xtest.shape:{Xtest.shape}") # print(f"y.shape:{y.shape} yval.shape:{yval.shape} ytest.shape:{ytest.shape}")3.生成一下X,y對(duì)應(yīng)的視圖
需要注意的是這里X并不是全部都取,我們剛剛插入了一列一,所以這里只取第二列的數(shù)據(jù),也就是X訓(xùn)練集的原生數(shù)據(jù)
# 原始圖片(數(shù)據(jù)的位置) def plotX():plt.figure(figsize=(13, 6))plt.scatter(X[:, 1], y, label='test')plt.xlabel('waterHeight')plt.xlabel('waterover')plt.legend()plt.show() plotX()4.代價(jià)函數(shù)
下面可以看出三部分?jǐn)?shù)據(jù)集所用的代價(jià)函數(shù)都是相同的,但我們?cè)趯?xiě)的時(shí)候還是需要帶上正則化的一項(xiàng),方便以后處理高階過(guò)擬合的狀態(tài)以及尋找最合適的theta。
別忘了這里加上正則項(xiàng)?
# 代價(jià)函數(shù)(erro) def costFunc(theta, X, y, lamda):theta.shape = (X.shape[1], 1)costFront = np.square(X @ theta - y).sum()cost_reg = lamda * (np.square(theta)).sum()return (costFront + cost_reg) / (2 * len(X))看一下第一次的代價(jià)函數(shù)值
theta = np.zeros(X.shape[1]) print(costFunc(theta,X,y,0))5.梯度偏導(dǎo)公式
以前我們用的都是梯度下降算法 ,用theta去對(duì)自身進(jìn)行更新,但這次我們要用一下高級(jí)算法,只需要提供梯度的偏導(dǎo)公式即可,高級(jí)算法會(huì)自動(dòng)幫我們找到最優(yōu)解
?這里我的思路是先把theta設(shè)置為一個(gè)倆行一列的矩陣,梯度下降公式的前半部分會(huì)得到一個(gè)倆行一列的矩陣與后半部分的theta正則化相加,具體就是X.T(2 * 12) * (X(12 * 2) * theta(2 * 1) - y(12 * 1)) -->gradientFront(2*1)? + gradient_reg(2 * 1)
這里如果不明白我要這樣寫(xiě)的,我建議自己在紙上把所有矩陣畫(huà)一下,再像上面一樣矩陣相乘就會(huì)懂啦
# 梯度偏導(dǎo)公式 def gradientFunc(theta, X, y, lamda):theta.shape = (X.shape[1], 1)gradientFront = X.T @ (X @ theta - y)gradient_reg = lamda * theta;gradient_reg[0] = 0return (gradientFront + gradient_reg) / (1 * len(X))看一下第一次梯度的值?
print(gradientFunc(theta,X,y,1))?6.使用高級(jí)算法來(lái)找出最優(yōu)theta
注意,這里傳入的theta一定要是ndarray的類(lèi)型,否則會(huì)報(bào)錯(cuò)
簡(jiǎn)單說(shuō)一下這個(gè)算法,其實(shí)可以理解為你給他提供了一個(gè)代價(jià)函數(shù)(costFunc),同時(shí)又提供了一個(gè)梯度偏導(dǎo)函數(shù)(gradientFunc),那么這個(gè)opt.minimize方法就可以自行迭代然后找出最優(yōu)的解返回給你,你可以把這個(gè)過(guò)程想象成梯度下降函數(shù),只是他幫你實(shí)現(xiàn)了,當(dāng)然minimize這個(gè)方法一定使用了比梯度下降更好的一些算法,只是我們不再需要深入了解了
# 使用優(yōu)化方法自行找出優(yōu)化后的theta def trainFindMin(theta, X, y, lamda):result = opt.minimize(fun=costFunc, x0=theta, args=(X, y, lamda), method='TNC',jac=gradientFunc)return result.x優(yōu)化之后的theta值為:可以看出優(yōu)化后的theta返回值也是ndarray的類(lèi)型
train_theta = trainFindMin(theta,X,y,0) print(train_theta)?7.看一下第一次優(yōu)化所得到的theta對(duì)應(yīng)的圖像
需要注意的是,這里的橫坐標(biāo)選取是有些說(shuō)到的,雖然選取的是X的第一列也就是訓(xùn)練集的原數(shù)據(jù),但是在選取高階的特征值時(shí)就不能這樣寫(xiě)橫坐標(biāo)了,原因是X的里的數(shù)據(jù)是隨機(jī)分布的,并不是從左到右遞增的(可以看第2條獲取數(shù)據(jù)那里的X展示),這里能圖像沒(méi)出問(wèn)題的原因是我們?cè)O(shè)置的函數(shù)是一個(gè)一元函數(shù),也就是一條直線,所以無(wú)論X的數(shù)據(jù)如何排列,得到的都是一條線的值,如果是高階就會(huì)有很多彎路或者折返會(huì)顯的特別亂
# d為2時(shí)候欠擬合/高偏差的圖像 def plot_theta(train_theta, X):train_theta.shape = (X.shape[1], 1)predict_num = X @ train_theta # (12*1)plotX()plt.plot(X[:, 1:], predict_num)plt.show()return plot_theta(train_theta,X)?8.學(xué)習(xí)曲線(用于分析是否是欠擬合還是過(guò)擬合狀態(tài))
學(xué)習(xí)曲線的X軸訓(xùn)練集的數(shù)量,Y軸就是代價(jià)函數(shù)的值,就是看隨著訓(xùn)練集數(shù)量的不斷增加,看訓(xùn)練集的代價(jià)函數(shù)值與交叉集的代價(jià)函數(shù)值之間的關(guān)系,這里X_each是用來(lái)獲取訓(xùn)練集的長(zhǎng)度,依次增加數(shù)量,cost_train與cost_cv用來(lái)存儲(chǔ)訓(xùn)練集與交叉集的代價(jià)函數(shù)值,for循環(huán)中分倆個(gè)步驟走:1)通過(guò)高級(jí)算法找出當(dāng)前所給出的訓(xùn)練集的最優(yōu)theta 。2)用得到的theta分別帶入訓(xùn)練集與交叉集的代價(jià)函數(shù)中
注意:這里在記錄訓(xùn)練集與交叉集的代價(jià)函數(shù)時(shí)不需要懲罰,lamda置為0
# 看一下學(xué)習(xí)曲線是欠擬合還是過(guò)擬合 def learningCurve(theta, X, y, Xval, yval, lamda):x_each = np.array(range(1, len(X) + 1)) # 這里加一是因?yàn)橛覅^(qū)間是開(kāi)區(qū)間,所以這里x_each存的是(1-12)cost_train = []cost_cv = []# theta_temp = []for i in x_each:theta_temp = trainFindMin(theta, X[:i, :], y[:i, :], lamda) # 同樣這里右邊也是開(kāi)區(qū)間,行一直取得是0-(i-1)cost_train.append(costFunc(theta_temp, X[:i, :], y[:i, :], 0))cost_cv.append(costFunc(theta_temp, Xval, yval, 0))plt.plot(x_each, cost_train, label='cost_train', c='r')plt.plot(x_each, cost_cv, label='cost_cv', c='b')plt.xlabel('nums of trainset')plt.ylabel('cost_erro')plt.legend()plt.show()學(xué)習(xí)曲線視圖如下:
下面圖片可以看出隨著訓(xùn)練集數(shù)量增加,訓(xùn)練集的代價(jià)函數(shù)與交叉集的代價(jià)函數(shù)都會(huì)偏高,這明顯是欠擬合的狀態(tài)(高偏差),因?yàn)闊o(wú)論怎么增加訓(xùn)練集,倆個(gè)數(shù)據(jù)集誤差都很大,不擬合任何一方。如果是訓(xùn)練集的代價(jià)函數(shù)為零,而交叉集偏高就是過(guò)擬合的狀態(tài)(高方差)
?9.修正欠擬合(高偏差)
通過(guò)學(xué)習(xí)曲線可得知是欠擬合的狀態(tài),我們就可以提高特征值的維度的方法來(lái)修正
# 下面提高d的維度,用以修正欠擬合的狀態(tài) def upDegree(X_upDegree, degree):for i in range(2, degree + 1):X_upDegree = np.insert(X_upDegree, i, np.power(X_upDegree[:, 1], i), axis=1)return X_upDegree?
?10.歸一化高維度的數(shù)據(jù)集
首先獲取訓(xùn)練集的平均值與標(biāo)準(zhǔn)差為后面歸一化做準(zhǔn)備,這里用到了numpy庫(kù)的倆個(gè)經(jīng)典方法,mean->平均值,std->方差
這里補(bǔ)充一下為什么需要?dú)w一化,如果這里不用歸一化來(lái)讓數(shù)據(jù)控制在更小的范圍內(nèi),就會(huì)出現(xiàn)有的數(shù)據(jù)集非常大,有的很小,導(dǎo)致計(jì)算機(jī)運(yùn)算耗費(fèi)更多資源,且用lamda在懲罰過(guò)擬合狀態(tài)時(shí)也會(huì)非常不明顯,比如4階的過(guò)擬合的訓(xùn)練集,可能lamda需要跟到10多萬(wàn)才能達(dá)到懲罰的效果,如果進(jìn)行了歸一化,就算6階的過(guò)擬合訓(xùn)練集也僅僅需要將lamda賦值10多就會(huì)達(dá)到修正的效果
# 取得平均值和標(biāo)準(zhǔn)差 def getMeanAndStd(X):X_mean = np.mean(X, axis=0)X_std = np.std(X, axis=0, ddof=1)return X_mean, X_std下面進(jìn)行歸一化操作:
公式為:(數(shù)據(jù)集 - 平均值)/ 標(biāo)準(zhǔn)差,還是很直觀的
# 進(jìn)行歸一化 def normalization(X, mean, std):X[:, 1:] = (X[:, 1:] - mean[1:]) / std[1:]return X11.進(jìn)行高緯度測(cè)驗(yàn)
# 高階測(cè)驗(yàn) X_degree = upDegree(X, 6) X_mean, X_std = getMeanAndStd(X_degree) X_normalize = normalization(X_degree, X_mean, X_std)X_val_degree = upDegree(Xval, 6) X_val_normalize = normalization(X_val_degree,X_mean,X_std)theta = np.zeros(X_degree.shape[1]) learningCurve(theta, X_normalize, y, X_val_normalize, yval, 1)這里讓訓(xùn)練集達(dá)到6階并歸一化,lamda設(shè)為1,學(xué)習(xí)曲線如下:
?下面看一下6維訓(xùn)練集得到的theta在XY軸的圖像(此時(shí)lamda設(shè)的為0,沒(méi)做懲罰,可以看出擬合度很高)
theta1 = trainFindMin(theta,X_normalize,y,0) def plot_fit_curve():x = np.linspace(-80,60,100)print("x:{}".format(x.shape))x1 = x.reshape(100,1)x1 = np.insert(x1,0,values=1,axis=1)x1 = upDegree(x1,6)x1 = normalization(x1,X_mean,X_std)y1 = x1 @ theta1plt.figure(figsize=(13,8),dpi=50)plt.scatter(X[:,1],y,marker='x',color = 'red')# 坐標(biāo)軸范圍plt.xlim(-100,80)plt.ylim(-80,60)# 坐標(biāo)軸刻度xtick = np.arange(-100,80,20)ytick = np.arange(-80,60,20)plt.xticks(xtick)plt.yticks(ytick)plt.xlabel("water height")plt.ylabel("Water overflow")plt.plot(x,y1,'b')plt.show() plot_fit_curve()?接下來(lái)看一下當(dāng)lamda為100時(shí),學(xué)習(xí)曲線的圖像,可以看出訓(xùn)練集與交叉集的代價(jià)都非常大,出現(xiàn)了懲罰過(guò)大,欠擬合的狀態(tài)(高偏差)
?下面是lamda為100時(shí)的擬合曲線,更直觀了
?11.試驗(yàn)不同的lamda來(lái)確定最合適的theta
我簡(jiǎn)單說(shuō)一下這里代碼的步驟,首先我們?cè)O(shè)置不同的lamda,我是按照吳恩達(dá)老師給的建議從0.01開(kāi)始,每次乘2遞增,將這些lamda依次帶入算法中優(yōu)化出最好的theta,用這些得到的theta帶入訓(xùn)練集和交叉集的代價(jià)函數(shù)中去并存儲(chǔ)起來(lái),然后我們?cè)谶@一列交叉集的代價(jià)函數(shù)中找出最小值所對(duì)應(yīng)的lamda值,調(diào)用高級(jí)算法帶入lamda得到對(duì)應(yīng)theta(trainFindMin(theta,X_normalize,y,2.56)),注意這里是歸一化的訓(xùn)練集,這個(gè)最后得到的theta再帶入測(cè)試集的代價(jià)函數(shù)中就得到最終結(jié)果了
一句話(huà)總結(jié)就是:在一群lamda當(dāng)中,找出能讓交叉集代價(jià)函數(shù)值最小的那個(gè)lamda,此時(shí)這個(gè)lamda所對(duì)應(yīng)的theta可能是最好的狀態(tài),帶入測(cè)試集即可
注意:1.這里記錄不同lamda的訓(xùn)練集和交叉集的代價(jià)函數(shù)值時(shí),同樣不需要懲罰
? ? ? ? ? ?2.我選擇的是7階的訓(xùn)練集,因?yàn)槲野l(fā)現(xiàn)7階所得到的測(cè)試集代價(jià)函數(shù)值和原題給的結(jié)果最接近
# 下面試驗(yàn)10個(gè)lamda,看看哪一個(gè)最貼合 def diffLamda(theta, X_normalize, y, X_val_normalize, yval):lamda_list = []cost_train_lamda = []cost_val_lamda = []sum1 = 0.01;for i in range(1, 11):lamda_list.append(sum1)sum1 *= 2for i in lamda_list:theta_lamda_i = trainFindMin(theta, X_normalize, y, i)cost_train_lamda.append(costFunc(theta_lamda_i, X_normalize, y, 0))cost_val_lamda.append(costFunc(theta_lamda_i, X_val_normalize, yval, 0))plt.plot(lamda_list, cost_train_lamda, c='r')plt.plot(lamda_list, cost_val_lamda, c='b')plt.xlabel('lamda')plt.ylabel('cost_value')plt.show()print(cost_val_lamda) diffLamda(theta, X_normalize, y, X_val_normalize, yval)從下面代價(jià)集合中也可以看出當(dāng)lamda等于2.56時(shí),交叉集的代價(jià)函數(shù)值最小,所以我們選取lamda = 2.56來(lái)作為測(cè)試集的參數(shù)?
?下面是lamda與倆個(gè)訓(xùn)練集的代價(jià)函數(shù)關(guān)系圖象,可以看出當(dāng)lamda處在2-3之間時(shí),cost_val(交叉集)有最小值
?12.獲取測(cè)試集的代價(jià)函數(shù)
這里選取上面最適合的lamda = 2.56最為參數(shù),并求出測(cè)試集的代價(jià)函數(shù),原題的答案為3.8599
# 求測(cè)試集的代價(jià)函數(shù) theta2 = trainFindMin(theta,X_normalize,y,2.56) cost_test = costFunc(theta2,X_test_normalize,ytest,0) print(cost_test)?三.全部代碼
import numpy as np import matplotlib.pyplot as plt from scipy.io import loadmat import scipy.optimize as opt# 獲取路徑 data = loadmat('ex5data1.mat') # print(data) # 一共三組數(shù)據(jù),分別是訓(xùn)練集,交叉集以及測(cè)試集 X, y = data['X'], data['y'] # 訓(xùn)練集 Xval, yval = data['Xval'], data['yval'] # 交叉集 Xtest, ytest = data['Xtest'], data['ytest'] # 測(cè)試集 # print(f"Xtest:{Xtest},ytest:{ytest}")# 下面在頭部插入一列1,方便與theta矩陣相乘從而得到常數(shù)項(xiàng) X = np.insert(X, 0, 1, axis=1) # 此處的含義是在X矩陣中的第0列插入1,axis為0時(shí)為行,1為列 Xval = np.insert(Xval, 0, 1, axis=1) Xtest = np.insert(Xtest, 0, 1, axis=1)# print(X) # 查看一下矩陣維度 # print(f"X.shape:{X.shape} Xval.shape:{Xval.shape} Xtest.shape:{Xtest.shape}") # print(f"y.shape:{y.shape} yval.shape:{yval.shape} ytest.shape:{ytest.shape}")# 原始圖片(數(shù)據(jù)的位置) def plotX():plt.figure(figsize=(13, 6))plt.scatter(X[:, 1], y, label='test')plt.xlabel('waterHeight')plt.xlabel('waterover')plt.legend()# plt.show()# plotX()# 代價(jià)函數(shù)(erro) def costFunc(theta, X, y, lamda):theta.shape = (X.shape[1], 1)costFront = np.square(X @ theta - y).sum()cost_reg = lamda * (np.square(theta)).sum()return (costFront + cost_reg) / (2 * len(X))theta = np.zeros(X.shape[1]) # print(costFunc(theta,X,y,0))# 梯度偏導(dǎo)公式 def gradientFunc(theta, X, y, lamda):theta.shape = (X.shape[1], 1)gradientFront = X.T @ (X @ theta - y)gradient_reg = lamda * theta;gradient_reg[0] = 0return (gradientFront + gradient_reg) / (1 * len(X))# print(gradientFunc(theta,X,y,1))# 使用優(yōu)化方法自行找出優(yōu)化后的theta def trainFindMin(theta, X, y, lamda):result = opt.minimize(fun=costFunc, x0=theta, args=(X, y, lamda), method='TNC',jac=gradientFunc)return result.x# train_theta = trainFindMin(theta,X,y,0) # print(train_theta)# d為2時(shí)候欠擬合/高偏差的圖像 def plot_theta(train_theta, X):train_theta.shape = (X.shape[1], 1)predict_num = X @ train_theta # (12*1)plotX()plt.plot(X[:, 1:], predict_num)plt.show()return# plot_theta(train_theta,X) # 看一下學(xué)習(xí)曲線是欠擬合還是過(guò)擬合 def learningCurve(theta, X, y, Xval, yval, lamda):x_each = np.array(range(1, len(X) + 1)) # 這里加一是因?yàn)橛覅^(qū)間是開(kāi)區(qū)間,所以這里x_each存的是(1-12)cost_train = []cost_cv = []# theta_temp = []for i in x_each:theta_temp = trainFindMin(theta, X[:i, :], y[:i, :], lamda) # 同樣這里右邊也是開(kāi)區(qū)間,行一直取得是0-(i-1)cost_train.append(costFunc(theta_temp, X[:i, :], y[:i, :], 0))cost_cv.append(costFunc(theta_temp, Xval, yval, 0))plt.plot(x_each, cost_train, label='cost_train', c='r')plt.plot(x_each, cost_cv, label='cost_cv', c='b')plt.xlabel('nums of trainset')plt.ylabel('cost_erro')plt.legend()plt.show()# plot_theta(theta_temp,X)# theta = np.zeros(X.shape[1]) # learningCurve(theta,X,y,Xval,yval,0) # 可以看出訓(xùn)練集和交叉集的誤差都挺大,所以是高偏差也就是欠擬合的狀態(tài)# 下面提高d的維度,用以修正欠擬合的狀態(tài) def upDegree(X_upDegree, degree):for i in range(2, degree + 1):X_upDegree = np.insert(X_upDegree, i, np.power(X_upDegree[:, 1], i), axis=1)return X_upDegree# 取得平均值和標(biāo)準(zhǔn)差 def getMeanAndStd(X):X_mean = np.mean(X, axis=0)X_std = np.std(X, axis=0, ddof=1)return X_mean, X_std# print(getMeanAndStd(X)) # 進(jìn)行歸一化 def normalization(X, mean, std):X[:, 1:] = (X[:, 1:] - mean[1:]) / std[1:]return X# 高階測(cè)驗(yàn) X_degree = upDegree(X, 7) X_mean, X_std = getMeanAndStd(X_degree) X_normalize = normalization(X_degree, X_mean, X_std)X_val_degree = upDegree(Xval, 7) X_val_normalize = normalization(X_val_degree,X_mean,X_std)X_test_degree = upDegree(Xtest, 7) X_test_normalize = normalization(X_test_degree,X_mean,X_std)theta = np.zeros(X_degree.shape[1]) # learningCurve(theta, X_normalize, y, X_val_normalize, yval, 100) # 在這里可以調(diào)節(jié)lamda的值來(lái)看學(xué)習(xí)曲線圖像的狀態(tài) # print(X_normalize)# plotUpdate(theta,X_normalize,y,1)# 下面試驗(yàn)10個(gè)lamda,看看哪一個(gè)最貼合 def diffLamda(theta, X_normalize, y, X_val_normalize, yval):lamda_list = []cost_train_lamda = []cost_val_lamda = []sum1 = 0.01;for i in range(1, 11):lamda_list.append(sum1)sum1 *= 2for i in lamda_list:theta_lamda_i = trainFindMin(theta, X_normalize, y, i)cost_train_lamda.append(costFunc(theta_lamda_i, X_normalize, y, 0))cost_val_lamda.append(costFunc(theta_lamda_i, X_val_normalize, yval, 0))print(f"{i},",end="")plt.plot(lamda_list, cost_train_lamda, c='r',label = "cost_train")plt.plot(lamda_list, cost_val_lamda, c='b',label = "cost_val")plt.xlabel('lamda')plt.ylabel('cost_value')plt.legend()plt.show()print()print(cost_val_lamda)# diffLamda(theta, X_normalize, y, X_val_normalize, yval)# theta1 = trainFindMin(theta,X_normalize,y,0) # 在這里可以調(diào)節(jié)lamda的值來(lái)看擬合圖像的狀態(tài) def plot_fit_curve():x = np.linspace(-80,60,100)print("x:{}".format(x.shape))x1 = x.reshape(100,1)x1 = np.insert(x1,0,values=1,axis=1)x1 = upDegree(x1,6)x1 = normalization(x1,X_mean,X_std)y1 = x1 @ theta1plt.figure(figsize=(13,8),dpi=50)plt.scatter(X[:,1],y,marker='x',color = 'red')# 坐標(biāo)軸范圍plt.xlim(-100,80)plt.ylim(-80,60)# 坐標(biāo)軸刻度xtick = np.arange(-100,80,20)ytick = np.arange(-80,60,20)plt.xticks(xtick)plt.yticks(ytick)plt.xlabel("water height")plt.ylabel("Water overflow")plt.plot(x,y1,'b')plt.show() # plot_fit_curve()# 求測(cè)試集的代價(jià)函數(shù) theta2 = trainFindMin(theta,X_normalize,y,2.56) cost_test = costFunc(theta2,X_test_normalize,ytest,0) print(f"測(cè)試集的代價(jià)函數(shù)為:{cost_test}")總結(jié)
以上是生活随笔為你收集整理的【吴恩达】机器学习作业ex5-->偏差与方差(过拟合/欠拟合)Python的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 商号是知识产权客体吗
- 下一篇: TI_BLE软件开发者指导6——L2CA