2.4 程序示例--线性决策边界-机器学习笔记-斯坦福吴恩达教授
生活随笔
收集整理的這篇文章主要介紹了
2.4 程序示例--线性决策边界-机器学习笔记-斯坦福吴恩达教授
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
程序示例–線性決策邊界
回歸模塊
在邏輯回歸模塊 logical_regression.py 中,實(shí)現(xiàn)了批量梯度下降法(bgd)以及隨機(jī)梯度下降法(sgd),同時(shí),支持正規(guī)化方程
# coding: utf-8 # logical_regression/logical_regression.py import numpy as np import matplotlib as plt import timedef exeTime(func):"""耗時(shí)計(jì)算裝飾器Args:func 待裝飾函數(shù)Returns:newFunc 裝飾后的函數(shù)"""def newFunc(*args, **args2):t0 = time.time()back = func(*args, **args2)return back, time.time() - t0return newFuncdef loadDataSet(filename):"""讀取數(shù)據(jù)集數(shù)據(jù)以TAB進(jìn)行分割A(yù)rgs:filename 文件名Returns:X 訓(xùn)練樣本集矩陣y 標(biāo)簽集矩陣"""numFeat = len(open(filename).readline().split('\t')) - 1X = []y = []file = open(filename)for line in file.readlines():lineArr = []curLine = line.strip().split('\t')for i in range(numFeat):lineArr.append(float(curLine[i]))X.append([1.0, float(lineArr[0]), float(lineArr[1])])y.append(float(curLine[-1]))return np.mat(X), np.mat(y).Tdef sigmoid(z):"""sigmoid函數(shù)"""return 1.0/(1.0+np.exp(-z))def J(theta, X, y, theLambda=0):"""預(yù)測(cè)代價(jià)函數(shù)"""m, n = X.shapeh = sigmoid(X.dot(theta))J = (-1.0/m)*(np.log(h).T.dot(y)+np.log(1-h).T.dot(1-y)) + (theLambda/(2.0*m))*np.sum(np.square(theta[1:]))if np.isnan(J[0]):return(np.inf)return J.flatten()[0,0]@exeTime def gradient(X, y, options):"""隨機(jī)梯度下降法Args:X 樣本矩陣y 標(biāo)簽矩陣rate 學(xué)習(xí)率options.theLambda 正規(guī)參數(shù)options.maxLoop 最大迭代次數(shù)options.epsilon 收斂精度options.method- 'sgd' 隨機(jī)梯度下降法- 'bgd' 批量梯度下降法Returns:(thetas, errors), timeConsumed"""m,n = X.shape# 初始化參數(shù)矩陣theta = np.ones((n,1))count = 0 # 迭代次數(shù)# 初始化誤差無(wú)限大error = float('inf')# 保存誤差變化狀況errors = []# 保存參數(shù)的變化狀況thetas = []rate = options.get('rate', 0.01)epsilon = options.get('epsilon', 0.1)maxLoop = options.get('maxLoop', 1000)theLambda = options.get('theLambda', 0)method = options['method']def _sgd(theta):converged = Falsefor i in range(maxLoop):if converged:breakfor j in range(m):h = sigmoid(X[j] *theta)diff = h - y[j]theta = theta - rate*(1.0/m)*X[j].T*differror = J(theta, X, y)errors.append(error)if error < epsilon:converged = Truebreakthetas.append(theta)return thetas, errors, i+1def _bgd(theta):for i in range(maxLoop):h = sigmoid(X.dot(theta))diff = h - y# theta0 should not be regularizedtheta = theta - rate*((1.0/m)*X.T*diff + (theLambda/m)*np.r_[[[0]], theta[1:]])error = J(theta, X, y, theLambda)errors.append(error)if error < epsilon:breakthetas.append(theta)return thetas, errors, i+1methods = {'sgd': _sgd,'bgd': _bgd}return methods[method](theta)測(cè)試
# coding: utf-8 # logical_regression/test_linear_boundry.py import numpy as np import logical_regression as regression import matplotlib.pyplot as plt import matplotlib.ticker as mtickif __name__ == "__main__":X, y = regression.loadDataSet('data/linear.txt')m, n = X.shapeoptions = [{'rate': 0.1,'epsilon': 0.01,'maxLoop': 500,'method': 'bgd'},{'rate': 1,'epsilon': 0.01,'maxLoop': 200,'method': 'sgd'}]for option in options:result, timeConsumed = regression.gradient(X, y, option)thetas, errors, iterationCount = resulttheta = thetas[-1]print theta, errors[-1], iterationCount# 繪制數(shù)據(jù)點(diǎn)fittingFig = plt.figure()title = '%s: rate=%.2f, iterationCount=%d, error=%.2f \n time: %.2fs' % (option['method'], option['rate'], iterationCount, errors[-1], timeConsumed)ax = fittingFig.add_subplot(111, title=title)ax.set_xlabel('X1')ax.set_ylabel('X2')for i in range(m):x = X[i].A[0]if y[i] == 1:ax.scatter(x[1], x[2], marker='*', color='black', s=50)else:ax.scatter(x[1], x[2], marker='o', color='green', s=50)# 繪制決策邊界x1Min = X[:, 1].min()x1Max = X[:, 1].max()x2Min = X[:, 2].min()x2Max = X[:, 2].max()xx1, xx2 = np.meshgrid(np.linspace(x1Min, x1Max),np.linspace(x2Min, x2Max))h = regression.sigmoid(np.c_[np.ones((xx1.ravel().shape[0],1)), xx1.ravel(), xx2.ravel()].dot(theta))h = h.reshape(xx1.shape)plt.contour(xx1, xx2, h, [0.5], colors='b', linewidth=.5)plt.show()# 繪制誤差曲線errorsFig = plt.figure()ax = errorsFig.add_subplot(111)ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.4f'))ax.plot(range(len(errors)), errors)ax.set_xlabel('Number of iterations')ax.set_ylabel('Cost J')plt.show()# 繪制theta的變化情況thetasFig, ax = plt.subplots(len(thetas[0]))thetas = np.asarray(thetas)for idx, sp in enumerate(ax):thetaList = thetas[:, idx]sp.plot(range(len(thetaList)), thetaList)sp.set_xlabel('Number of iteration')sp.set_ylabel(r'$\theta_%d$'%idx)plt.show()批量梯度下降法獲得的決策邊界如下,測(cè)試中,迭代次數(shù)為 500 次,學(xué)習(xí)率為 0.1:
批量梯度下降法中,代價(jià)函數(shù)隨迭代次數(shù)變化狀況如下:
隨機(jī)梯度下降法獲得決策邊界如下,測(cè)試中,迭代次數(shù)為 200 次,學(xué)習(xí)率為 1 :
隨機(jī)梯度下降法中,參數(shù) θθθ 的變化情況如下圖所示:
總結(jié)
以上是生活随笔為你收集整理的2.4 程序示例--线性决策边界-机器学习笔记-斯坦福吴恩达教授的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2.3 利用正规化解决过拟合问题-机器学
- 下一篇: 2.5 程序示例--非线性决策边界-机器