python實(shí)現(xiàn)感知機(jī)模型的示例
from sklearn.linear_model import Perceptronimport argparse #一個(gè)好用的參數(shù)傳遞模型import numpy as npfrom sklearn.datasets import load_iris #數(shù)據(jù)集from sklearn.model_selection import train_test_split #訓(xùn)練集和測(cè)試集分割from loguru import logger #日志輸出,不清楚用法#python is also oop class PerceptronToby(): ''' n_epoch:迭代次數(shù) learning_rate:學(xué)習(xí)率 loss_tolerance:損失閾值,即損失函數(shù)達(dá)到極小值的變化量 ''' def __init__(self, n_epoch = 500, learning_rate = 0.1, loss_tolerance = 0.01): self._n_epoch = n_epoch self._lr = learning_rate self._loss_tolerance = loss_tolerance '''訓(xùn)練模型,即找到每個(gè)數(shù)據(jù)最合適的權(quán)重以得到最小的損失函數(shù)''' def fit(self, X, y): # X:訓(xùn)練集,即數(shù)據(jù)集,每一行是樣本,每一列是數(shù)據(jù)或標(biāo)簽,一樣本包括一數(shù)據(jù)和一標(biāo)簽 # y:標(biāo)簽,即1或-1 n_sample, n_feature = X.shape #剝離矩陣的方法真帥 #均勻初始化參數(shù) rnd_val = 1/np.sqrt(n_feature) rng = np.random.default_rng() self._w = rng.uniform(-rnd_val,rnd_val,size = n_feature) #偏置初始化為0 self._b = 0 #開始訓(xùn)練了,迭代n_epoch次 num_epoch = 0 #記錄迭代次數(shù) prev_loss = 0 #前損失值 while True: curr_loss = 0 #現(xiàn)在損失值 wrong_classify = 0 #誤分類樣本 #一次迭代對(duì)每個(gè)樣本操作一次 for i in range(n_sample):#輸出函數(shù)y_pred = np.dot(self._w,X[i]) + self._b#損失函數(shù)curr_loss += -y[i] * y_pred# 感知機(jī)只對(duì)誤分類樣本進(jìn)行參數(shù)更新,使用梯度下降法if y[i] * y_pred <= 0: self._w += self._lr * y[i] * X[i] self._b += self._lr * y[i] wrong_classify += 1 num_epoch += 1 loss_diff = curr_loss - prev_loss prev_loss = curr_loss # 訓(xùn)練終止條件: # 1. 訓(xùn)練epoch數(shù)達(dá)到指定的epoch數(shù)時(shí)停止訓(xùn)練 # 2. 本epoch損失與上一個(gè)epoch損失差異小于指定的閾值時(shí)停止訓(xùn)練 # 3. 訓(xùn)練過程中不再存在誤分類點(diǎn)時(shí)停止訓(xùn)練 if num_epoch >= self._n_epoch or abs(loss_diff) < self._loss_tolerance or wrong_classify == 0:break '''預(yù)測(cè)模型,顧名思義''' def predict(self, x): '''給定輸入樣本,預(yù)測(cè)其類別''' y_pred = np.dot(self._w, x) + self._b return 1 if y_pred >= 0 else -1#主函數(shù)def main(): #參數(shù)數(shù)組生成 parser = argparse.ArgumentParser(description='感知機(jī)算法實(shí)現(xiàn)命令行參數(shù)') parser.add_argument('--nepoch', type=int, default=500, help='訓(xùn)練多少個(gè)epoch后終止訓(xùn)練') parser.add_argument('--lr', type=float, default=0.1, help='學(xué)習(xí)率') parser.add_argument('--loss_tolerance', type=float, default=0.001, help='當(dāng)前損失與上一個(gè)epoch損失之差的絕對(duì)值小于該值時(shí)終止訓(xùn)練') args = parser.parse_args() #導(dǎo)入數(shù)據(jù) X, y = load_iris(return_X_y=True) # print(y) y[:50] = -1 # 分割數(shù)據(jù) xtrain, xtest, ytrain, ytest = train_test_split(X[:100], y[:100], train_size=0.8, shuffle=True) # print(xtest) #調(diào)用并訓(xùn)練模型 model = PerceptronToby(args.nepoch, args.lr, args.loss_tolerance) model.fit(xtrain, ytrain) n_test = xtest.shape[0] # print(n_test) n_right = 0 for i in range(n_test): y_pred = model.predict(xtest[i]) if y_pred == ytest[i]: n_right += 1 else: logger.info('該樣本真實(shí)標(biāo)簽為:{},但是toby模型預(yù)測(cè)標(biāo)簽為:{}'.format(ytest[i], y_pred)) logger.info('toby模型在測(cè)試集上的準(zhǔn)確率為:{}%'.format(n_right * 100 / n_test)) skmodel = Perceptron(max_iter=args.nepoch) skmodel.fit(xtrain, ytrain) logger.info('sklearn模型在測(cè)試集上準(zhǔn)確率為:{}%'.format(100 * skmodel.score(xtest, ytest)))if __name__ == '__main__': main()```
視頻參考地址
以上就是python實(shí)現(xiàn)感知機(jī)模型的示例的詳細(xì)內(nèi)容,更多關(guān)于python 實(shí)現(xiàn)感知機(jī)模型的示例代碼的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 利用CSS3新特性創(chuàng)建透明邊框三角2. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺(tái)】3. PHP 使用 Trait 解決 PHP 單繼承問題詳解4. Python collections模塊的使用方法5. ajax post下載flask文件流以及中文文件名問題6. python dict如何定義7. 使用css實(shí)現(xiàn)全兼容tooltip提示框8. WML語(yǔ)言的基本情況9. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能10. 如何通過vscode運(yùn)行調(diào)試javascript代碼
