溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python實(shí)現(xiàn)感知機(jī)模型的示例

發(fā)布時(shí)間:2020-10-06 19:45:15 來源:腳本之家 閱讀:144 作者:鄙人劍人肖 欄目:開發(fā)技術(shù)
from sklearn.linear_model import Perceptron
import argparse #一個(gè)好用的參數(shù)傳遞模型
import numpy as np
from 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)注億速云其它相關(guān)文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI