亚洲精品中文免费|亚洲日韩中文字幕制服|久久精品亚洲免费|一本之道久久免费

      
      

            <dl id="hur0q"><div id="hur0q"></div></dl>

                手撕SVM(公式推導(dǎo)+代碼實(shí)現(xiàn))(三)

                手撕SVM(公式推導(dǎo)+代碼實(shí)現(xiàn))(三)

                前言

                前面我們進(jìn)行了很多的理論性研究,下面我們開(kāi)始用代碼進(jìn)行實(shí)現(xiàn)。

                編程求解線(xiàn)性SVM

                import matplotlib.pyplot as pltimport numpy as np#讀取數(shù)據(jù)def loadDataSet(fileName): dataMat = []; labelMat = [] fr = open(fileName) for line in fr.readlines(): #逐行讀取,濾除空格等 lineArr = line.strip().split(”) dataMat.append([float(lineArr[0]),float(lineArr[1])]) #添加數(shù)據(jù) labelMat.append(float(lineArr[2])) #添加標(biāo)簽 return dataMat,labelMat#數(shù)據(jù)可視化def showDataSet(dataMat,labelMat): data_plus = [] data_minus = [] for i in range(len(dataMat)): if labelMat[i] > 0: data_plus.append(dataMat[i]) else: data_minus.append(dataMat[i]) data_plus_np = np.array(data_plus) #轉(zhuǎn)換為numpy矩陣 data_minus_np = np.array(data_minus)#轉(zhuǎn)換為numpy矩陣 plt.scatter(np.transpose(data_plus_np)[0],np.transpose(data_plus_np)[1]) #正樣本 plt.scatter(np.transpose(data_minus_np)[0],np.transpose(data_minus_np)[1]) #負(fù)樣本 plt.show()dataMat,labelMat = loadDataSet(‘testSet.txt’)showDataSet(dataMat,labelMat)

                這個(gè)數(shù)據(jù)集顯然線(xiàn)性可分。

                應(yīng)用簡(jiǎn)化版SMO算法處理小規(guī)模數(shù)據(jù)集

                import randomdef selectJrand(i,m): j=i while(j==i): #選擇一個(gè)不等于i的j j = int(random.uniform(0,m)) return jdef clipAlpha(aj,H,L): if aj > H: aj = H if L > aj: aj = L return ajdataArr,labelArr =loadDataSet(‘testSet.txt’)labelArr

                [-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0]

                data = np.mat(dataArr)data[2,:]matrix([[ 7.55151, -1.58003]])

                可以看出來(lái),這里使用的類(lèi)別標(biāo)簽是-1和1

                SMO算法的偽代碼:

                創(chuàng)建一個(gè)alpha向量并將其初始化為 0 向量 當(dāng)?shù)?span id="zq7hqw5" class="wpcom_tag_link">次數(shù)小于最大迭代次數(shù)時(shí) (外循環(huán)) 對(duì)數(shù)據(jù)集中的每個(gè)數(shù)據(jù)向量 (內(nèi)循環(huán)): 如果該數(shù)據(jù)向量可以被優(yōu)化: 隨機(jī)選擇另外一個(gè)數(shù)據(jù)向量 同時(shí)優(yōu)化這兩個(gè)向量 如果兩個(gè)向量都不能被優(yōu)化, 退出內(nèi)循環(huán)如果所有向量都沒(méi)被優(yōu)化, 增加迭代數(shù)目, 繼續(xù)下一次循環(huán)#簡(jiǎn)化版SMO算法def smoSimple(dataMatIn,classLabels,C,toler,maxIter): dataMatrix = np.mat(dataMatIn); labelMat = np.mat(classLabels).transpose() b = 0; m,n = np.shape(dataMatrix) alphas = np.mat(np.zeros((m,1)))#初始化alpha參數(shù),設(shè)置為0 iterSmo = 0 #初始化迭代次數(shù) while(iterSmo < maxIter): alphaPairsChanged = 0#用于記錄alpha是否已經(jīng)進(jìn)行優(yōu)化 #步驟1. 計(jì)算誤差Ei for i in range(m): fXi = float(np.multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[i,:].T)) + b Ei = fXi – float(labelMat[i]) if ((labelMat[i]*Ei 0)): j = selectJrand(i,m) #步驟1. 計(jì)算誤差Ej fXj = float(np.multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[j,:].T)) + b Ej = fXj – float(labelMat[j]) #保存更新前的alpha值,使用淺拷貝 alphaIold = alphas[i].copy() alphaJold = alphas[j].copy() #步驟2:計(jì)算上界H和下界L if (labelMat[i] != labelMat[j]): L = max(0,alphas[j] – alphas[i]) H = min(C,C + alphas[j] – alphas[i]) else: L = max(0,alphas[j] + alphas[i] – C) H = min(C,alphas[j] + alphas[i]) if L==H: print(“L==H”); continue #步驟3:計(jì)算eta eta = 2.0 * dataMatrix[i,:]*dataMatrix[j,:].T – dataMatrix[i,:]*dataMatrix[i,:].T – dataMatrix[j,:]*dataMatrix[j,:].T if eta >=0 : print(“eta>=0”);continue #步驟4:更新alpha_j alphas[j] -= labelMat[j]*(Ei-Ej)/eta #步驟5:修剪alpha_j alphas[j] = clipAlpha(alphas[j],H,L) if (abs(alphas[j] – alphaJold) < 0.00001) : print("j not moving enough") ; continue #步驟6:更新alpha_i alphas[i] += labelMat[j]*labelMat[i]*(alphaJold – alphas[j]) #步驟7:更新b_1和b_2 b1 = b – Ei – labelMat[i]*(alphas[i] – alphaIold)*dataMatrix[i,:]*dataMatrix[i,:].T – labelMat[j]*(alphas[j] – alphaJold)*dataMatrix[i,:]*dataMatrix[j,:].T b2 = b – Ej – labelMat[i]*(alphas[i] – alphaIold)*dataMatrix[i,:]*dataMatrix[j,:].T – labelMat[j]*(alphas[j] – alphaJold)*dataMatrix[j,:]*dataMatrix[j,:].T #步驟8:根據(jù)b_1和b_2更新b if (0 alphas[i]) : b = b1 elif (0 alphas[j]): b = b2 else: b = (b1 + b2)/2.0 #統(tǒng)計(jì)優(yōu)化次數(shù) #如果程序執(zhí)行到for循環(huán)的最后一行都不執(zhí)行continue語(yǔ)句,那么就已經(jīng)成功地改變了一對(duì)alpha,同時(shí)可以增加alphaPairsChanged的值 alphaPairsChanged += 1 #打印統(tǒng)計(jì)信息 print("第%d次迭代 樣本:%d, alpha優(yōu)化次數(shù):%d" % (iterSmo,i,alphaPairsChanged)) #更新迭代次數(shù) #在for循環(huán)之外,需要檢查alpha值是否做了更新,如果有更新則將iterSmo設(shè)為0后繼續(xù)運(yùn)行程序。只有在所有數(shù)據(jù)集上遍歷maxIter次,且不再發(fā)生任何alpha修改之后,程序才會(huì)停止并退出while循環(huán) if (alphaPairsChanged == 0): iterSmo += 1 else: iterSmo = 0 print("迭代次數(shù):%d" % iterSmo) return b,alphasb,alphas = smoSimple(dataArr,labelArr,0.6,0.001,500)

                L==H第0次迭代 樣本:1, alpha優(yōu)化次數(shù):1第0次迭代 樣本:3, alpha優(yōu)化次數(shù):2第0次迭代 樣本:5, alpha優(yōu)化次數(shù):3L==H第0次迭代 樣本:8, alpha優(yōu)化次數(shù):4L==Hj not moving enoughj not moving enoughL==HL==Hj not moving enoughL==H第0次迭代 樣本:30, alpha優(yōu)化次數(shù):5第0次迭代 樣本:31, alpha優(yōu)化次數(shù):6L==HL==H第0次迭代 樣本:54, alpha優(yōu)化次數(shù):7L==HL==H第0次迭代 樣本:71, alpha優(yōu)化次數(shù):8L==HL==HL==H第0次迭代 樣本:79, alpha優(yōu)化次數(shù):9L==H第0次迭代 樣本:92, alpha優(yōu)化次數(shù):10j not moving enoughL==H迭代次數(shù):0第0次迭代 樣本:1, alpha優(yōu)化次數(shù):1j not moving enoughj not moving enoughj not moving enoughj not moving enoughj not moving enoughL==HL==Hj not moving enoughj not moving enoughj not moving enoughj not moving enoughL==Hj not moving enoughL==HL==Hj not moving enoughj not moving enough第0次迭代 樣本:37, alpha優(yōu)化次數(shù):2第0次迭代 樣本:39, alpha優(yōu)化次數(shù):3第0次迭代 樣本:52, alpha優(yōu)化次數(shù):4j not moving enoughj not moving enoughj not moving enoughj not moving enoughj not moving enough第0次迭代 樣本:71, alpha優(yōu)化次數(shù):5j not moving enoughj not moving enoughj not moving enoughj not moving enoughj not moving enough迭代次數(shù):0j not moving enoughj not moving enoughj not moving enough第0次迭代 樣本:8, alpha優(yōu)化次數(shù):1L==Hj not moving enough第0次迭代 樣本:23, alpha優(yōu)化次數(shù):2L==Hj not moving enoughj not moving enoughL==Hj not moving enoughj not moving enoughj not moving enough第0次迭代 樣本:39, alpha優(yōu)化次數(shù):3L==Hj not moving enough第0次迭代 樣本:52, alpha優(yōu)化次數(shù):4j not moving enough第0次迭代 樣本:55, alpha優(yōu)化次數(shù):5L==HL==HL==HL==HL==Hj not moving enough第0次迭代 樣本:79, alpha優(yōu)化次數(shù):6第0次迭代 樣本:92, alpha優(yōu)化次數(shù):7迭代次數(shù):0j not moving enoughL==Hj not moving enoughj not moving enoughL==Hj not moving enough第0次迭代 樣本:23, alpha優(yōu)化次數(shù):1j not moving enoughj not moving enoughj not moving enoughj not moving enoughj not moving enoughj not moving enoughj not moving enoughL==HL==H第0次迭代 樣本:51, alpha優(yōu)化次數(shù):2j not moving enoughj not moving enoughj not moving enoughj not moving enoughL==H第0次迭代 樣本:69, alpha優(yōu)化次數(shù):3L==Hj not moving enough第0次迭代 樣本:94, alpha優(yōu)化次數(shù):4j not moving enoughj not moving enough迭代次數(shù):0j not moving enoughj not moving enoughj not moving enoughj not moving enoughj not moving enough

                迭代次數(shù):497j not moving enoughj not moving enoughj not moving enough迭代次數(shù):498j not moving enoughj not moving enoughj not moving enough迭代次數(shù):499j not moving enoughj not moving enoughj not moving enough迭代次數(shù):500

                bmatrix([[-3.83785102]])alphas[alphas>0]matrix([[0.1273855 , 0.24131542, 0.36872064]])np.shape(alphas[alphas>0])(1, 3)for i in range(100): if alphas[i] > 0.0: print(dataArr[i],labelArr[i])[4.658191, 3.507396] -1.0[3.457096, -0.082216] -1.0[6.080573, 0.418886] 1.0#分類(lèi)結(jié)果可視化def showClassifer(dataMat, w, b): #繪制樣本點(diǎn) data_plus = [] #正樣本 data_minus = [] #負(fù)樣本 for i in range(len(dataMat)): if labelMat[i] > 0: data_plus.append(dataMat[i]) else: data_minus.append(dataMat[i]) data_plus_np = np.array(data_plus) #轉(zhuǎn)換為numpy矩陣 data_minus_np = np.array(data_minus) #轉(zhuǎn)換為numpy矩陣 plt.scatter(np.transpose(data_plus_np)[0], np.transpose(data_plus_np)[1], s=30, alpha=0.7) #正樣本散點(diǎn)圖 plt.scatter(np.transpose(data_minus_np)[0], np.transpose(data_minus_np)[1], s=30, alpha=0.7) #負(fù)樣本散點(diǎn)圖 #繪制直線(xiàn) x1 = max(dataMat)[0] x2 = min(dataMat)[0] a1, a2 = w b = float(b) a1 = float(a1[0]) a2 = float(a2[0]) y1, y2 = (-b- a1*x1)/a2, (-b – a1*x2)/a2 plt.plot([x1, x2], [y1, y2]) #找出支持向量點(diǎn) for i, alpha in enumerate(alphas): if abs(alpha) > 0: x, y = dataMat[i] plt.scatter([x], [y], s=150, c=’none’, alpha=0.7, linewidth=1.5, edgecolor=’red’) plt.show()#計(jì)算wdef get_w(dataMat, labelMat, alphas): alphas, dataMat, labelMat = np.array(alphas), np.array(dataMat), np.array(labelMat) w = np.dot((np.tile(labelMat.reshape(1, -1).T, (1, 2)) * dataMat).T, alphas) return w.tolist()w = get_w(dataMat,labelMat,alphas)showClassifer(dataMat,w,b)

                鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場(chǎng),版權(quán)歸原作者所有,如有侵權(quán)請(qǐng)聯(lián)系管理員(admin#wlmqw.com)刪除。
                用戶(hù)投稿
                上一篇 2022年6月15日 06:02
                下一篇 2022年6月15日 06:02

                相關(guān)推薦

                • cad連續(xù)標(biāo)注快捷鍵(cad連續(xù)標(biāo)注快捷鍵)

                  本文主要講的是cad連續(xù)標(biāo)注快捷鍵,以及和cad連續(xù)標(biāo)注快捷鍵相關(guān)的知識(shí),如果覺(jué)得本文對(duì)您有所幫助,不要忘了將本文分享給朋友。 cad中連續(xù)標(biāo)注快捷鍵 CAD尺寸標(biāo)準(zhǔn)快捷命令:DL…

                  2022年11月27日
                • 奶茶的做法和配方(草莓奶茶的做法和配方)

                  今天小編給各位分享奶茶的做法和配方的知識(shí),其中也會(huì)對(duì)草莓奶茶的做法和配方進(jìn)行解釋?zhuān)绻芘銮山鉀Q你現(xiàn)在面臨的問(wèn)題,別忘了關(guān)注本站,現(xiàn)在開(kāi)始吧! 奶茶的怎么做法 主料 牛奶200ml…

                  2022年11月26日
                • 怎么轉(zhuǎn)行總結(jié)出成功轉(zhuǎn)行的3個(gè)步驟

                  01 前段時(shí)間,由麥可思研究院發(fā)布的《就業(yè)藍(lán)皮書(shū):2019年中國(guó)大學(xué)生就業(yè)報(bào)告》顯示,2018大學(xué)畢業(yè)生半年內(nèi)的離職率為33%,主動(dòng)離職的主要原因是“個(gè)人發(fā)展空間不夠”和“薪資福利…

                  2022年11月26日
                • 什么是推廣cpa一篇文章帶你看懂CPA推廣渠道

                  CPA渠道 CPA指的是按照指定的行為結(jié)算,可以是搜索,可以是注冊(cè),可以是激活,可以是搜索下載激活,可以是綁卡,實(shí)名認(rèn)證,可以是付費(fèi),可以是瀏覽等等。甲乙雙方可以根據(jù)自己的情況來(lái)定…

                  2022年11月25日
                • 百度關(guān)鍵詞快速排名的4大原理解析(百度怎么刷關(guān)鍵詞)

                  近期百度公告驚雷算法2.0,升級(jí)之快還是第一次吧,看來(lái)百度對(duì)于刷點(diǎn)擊行為是零容忍了。之前尹華峰SEO技術(shù)博客介紹過(guò)一篇如何使用刷點(diǎn)擊工具,其實(shí)市面上有很多這類(lèi)SEO快速排名的軟件,…

                  2022年11月25日
                • 抖音直播帶貨有哪些方法技巧(抖音直播帶貨有哪些痛點(diǎn))

                  如今抖音這個(gè)短視頻的變現(xiàn)能力越來(lái)越突顯了,尤其是在平臺(tái)上開(kāi)通直播,更具有超強(qiáng)的帶貨屬性,已經(jīng)有越來(lái)越多的普通人加入到其中了。不過(guò)直播帶貨雖然很火,但是也不是每個(gè)人都能做好的,那么在…

                  2022年11月24日
                • 把一個(gè)新產(chǎn)品成功推向市場(chǎng)的6個(gè)步驟解析(如何推廣新產(chǎn)品)

                  據(jù)說(shuō),給人留下第一印象的機(jī)會(huì)只有一次。無(wú)論是新工作的第一天,還是見(jiàn)你的另一半的父母,或是把你自己介紹給別人,你都希望能表現(xiàn)得風(fēng)度翩翩、討人喜歡、優(yōu)雅。 推出一個(gè)新產(chǎn)品也是如此。即使…

                  2022年11月24日
                • 明查|美國(guó)新冠后遺癥患者中有16%癥狀嚴(yán)重以致無(wú)法工作?

                  點(diǎn)擊進(jìn)入澎湃新聞全球事實(shí)核查平臺(tái) 速覽 – 網(wǎng)傳數(shù)據(jù)比例無(wú)權(quán)威信源佐證,該比例有可能是結(jié)合了美國(guó)疾病防控中心和布魯金斯學(xué)會(huì)的數(shù)據(jù)得出,但這兩個(gè)機(jī)構(gòu)的調(diào)研目的和樣本都不同…

                  2022年11月24日
                • edge默認(rèn)主頁(yè)的網(wǎng)址怎么改 edge默認(rèn)360搜索更改教程

                  Edge默認(rèn)主頁(yè)是360搜索怎么取消?最近有用戶(hù)詢(xún)問(wèn)這個(gè)問(wèn)題,Edge瀏覽器是微軟內(nèi)置的瀏覽軟件,很多用戶(hù)會(huì)選擇使用,在使用時(shí)發(fā)現(xiàn)瀏覽器主頁(yè)變成了360搜索,有沒(méi)有方法可以更改回來(lái)…

                  2022年11月23日
                • 122交通安全知識(shí)線(xiàn)上競(jìng)賽答題次數(shù)限制嗎?(附獎(jiǎng)品設(shè)置)

                  不限制答題次數(shù) ①掃描下方二維碼進(jìn)入H5答題頁(yè)面,即可開(kāi)始答題。 ②每日答題次數(shù)不限 ③答題得分90分及以上即可獲得抽獎(jiǎng)資格。 ④獲獎(jiǎng)名單分別于12月2日、12月12日、12月22…

                  2022年11月22日

                聯(lián)系我們

                聯(lián)系郵箱:admin#wlmqw.com
                工作時(shí)間:周一至周五,10:30-18:30,節(jié)假日休息