python離散建模之感知器學(xué)習(xí)算法
我們將研究一種判別式分類方法,其中直接學(xué)習(xí)評估 g(x)所需的 w 參數(shù)。我們將使用感知器學(xué)習(xí)算法。
感知器學(xué)習(xí)算法很容易實(shí)現(xiàn),但為了節(jié)省時間,我在下面為您提供了一個實(shí)現(xiàn)。該函數(shù)有幾個輸入:訓(xùn)練數(shù)據(jù)、訓(xùn)練標(biāo)簽、對權(quán)重的初始猜測和學(xué)習(xí)率。注意,對于這兩個類,類標(biāo)簽的值必須為+1和-1。
它將返回一個元組,其中包含:
- 1.學(xué)習(xí)w參數(shù)
- 2.執(zhí)行的迭代次數(shù)
- 3.錯誤分類的樣本數(shù)
花些時間檢查代碼。如果不清楚每一行是如何工作的,不要擔(dān)心,只要讓你自己知道每一行的目的是什么就可以了。代碼中有一些注釋可以幫助大家。
def perce(X, y, w_init, rho, max_iter=1000): ? ?? ? ? (N, nfeatures) = X.shape ? ? # Augment the feature vectors by adding a 1 to each one. (see lecture notes) ? ? X = np.hstack((X, np.ones((N, 1)))) ? ? nfeatures += 1 ? ? w = w_init ?# initialise weights ? ? iter = 0 ? ? mis_class = N ?# start by assuming all samples are misclassified ? ? while mis_class > 0 and iter < max_iter: ? ? ? ? iter += 1 ? ? ? ? mis_class = 0 ? ? ? ? gradient = np.zeros(nfeatures) ?# initaliase the gradients to 0 ? ? ? ? # loop over every training sample. ? ? ? ? for i in range(N): ? ? ? ? ? ? # each misclassified point will cause the gradient to change ? ? ? ? ? ? if np.inner(X[i, :], w) * y[i] <= 0: ? ? ? ? ? ? ? ? mis_class += 1 ? ? ? ? ? ? ? ? gradient += -y[i] * X[i, :] ? ? ? ? # update the weight vector ready for the next iteration ? ? ? ? # Note, also that the learning rate decays over time (rho/iter) ? ? ? ? w -= rho / iter * gradient ? ? return w, iter, mis_class
解釋:
X-數(shù)據(jù)矩陣。每行代表一個單獨(dú)的樣本
y-與X-標(biāo)簽行對應(yīng)的整數(shù)類標(biāo)簽的一維數(shù)組必須為+1或-1
w_init-初始權(quán)重向量
rho-標(biāo)量學(xué)習(xí)率
最大迭代次數(shù)-最大迭代次數(shù)(默認(rèn)為1000)
def perce_fast(X, y, w_init, rho, max_iter=10000): ?? ? ? (N, nfeatures) = X.shape ? ? X = np.hstack((X, np.ones((N, 1)))) ? ? nfeatures += 1 ? ? w = w_init ? ? iter = 0 ? ? mis_class = N ? ? yy = np.tile(y, (nfeatures, 1)).T ? ? while mis_class > 0 and iter < max_iter: ? ? ? ? iter += 1 ? ? ? ? # Compute set of misclassified points ? ? ? ? mc = (np.dot(X, w.transpose()) * y) <= 0 ? ? ? ? mis_class = np.sum(mc) ? ? ? ? # Update weights. Note, the learning rate decays over time (rho/iter) ? ? ? ? w -= rho / iter * (np.sum(-yy[mc, :] * X[mc, :], axis=0)) ? ? return w, iter, np.sum(mc)
- 感知器算法的高效實(shí)現(xiàn)
- 對于筆記本電腦數(shù)據(jù),此版本的工作速度將提高x100!
到此這篇關(guān)于python離散建模之感知器學(xué)習(xí)算法的文章就介紹到這了,更多相關(guān)python感知器學(xué)習(xí)算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中的實(shí)例方法、靜態(tài)方法、類方法、類變量和實(shí)例變量淺析
這篇文章主要介紹了python中的實(shí)例方法、靜態(tài)方法、類方法、類變量和實(shí)例變量淺析,需要的朋友可以參考下2014-04-04Python Pygame實(shí)戰(zhàn)之塔防游戲的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Python中的Pygame模塊制作簡單的塔防小游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動手試一試2022-03-03TensorFlow keras卷積神經(jīng)網(wǎng)絡(luò) 添加L2正則化方式
這篇文章主要介紹了TensorFlow keras卷積神經(jīng)網(wǎng)絡(luò) 添加L2正則化方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05利用Python?實(shí)現(xiàn)分布式計(jì)算
這篇文章主要介紹了利用Python?實(shí)現(xiàn)分布式計(jì)算,文章通過借助于?Ray展開對分布式計(jì)算的實(shí)現(xiàn),感興趣的小伙伴可以參考一下2022-05-05使用python代碼進(jìn)行身份證號校驗(yàn)的實(shí)現(xiàn)示例
這篇文章主要介紹了使用python代碼進(jìn)行身份證號校驗(yàn)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Python實(shí)現(xiàn)提取和去除數(shù)據(jù)中包含關(guān)鍵詞的行
這篇文章主要介紹了Python如何提取數(shù)據(jù)中包含關(guān)鍵詞的行已經(jīng)如何去除數(shù)據(jù)中包含關(guān)鍵詞的行,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-08-08Python學(xué)習(xí)pygal繪制線圖代碼分享
這篇文章主要介紹了Python學(xué)習(xí)pygal繪制線圖代碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12