最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

自適應(yīng)線性神經(jīng)網(wǎng)絡(luò)Adaline的python實現(xiàn)詳解

 更新時間:2019年09月30日 09:06:32   作者:沙克的世界  
這篇文章主要介紹了自適應(yīng)線性神經(jīng)網(wǎng)絡(luò)Adaline的python實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

自適應(yīng)線性神經(jīng)網(wǎng)絡(luò)Adaptive linear network, 是神經(jīng)網(wǎng)絡(luò)的入門級別網(wǎng)絡(luò)。

相對于感知器,采用了f(z)=z的激活函數(shù),屬于連續(xù)函數(shù)。

代價函數(shù)為LMS函數(shù),最小均方算法,Least mean square。

實現(xiàn)上,采用隨機梯度下降,由于更新的隨機性,運行多次結(jié)果是不同的。

'''
Adaline classifier

created on 2019.9.14
author: vince
'''
import pandas 
import math
import numpy 
import logging
import random
import matplotlib.pyplot as plt

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

'''
Adaline classifier

Attributes
w: ld-array = weights after training
l: list = number of misclassification during each iteration 
'''
class Adaline:
  def __init__(self, eta = 0.001, iter_num = 500, batch_size = 1):
    '''
    eta: float = learning rate (between 0.0 and 1.0).
    iter_num: int = iteration over the training dataset.
    batch_size: int = gradient descent batch number, 
      if batch_size == 1, used SGD; 
      if batch_size == 0, use BGD; 
      else MBGD;
    '''

    self.eta = eta;
    self.iter_num = iter_num;
    self.batch_size = batch_size;

  def train(self, X, Y):
    '''
    train training data.
    X:{array-like}, shape=[n_samples, n_features] = Training vectors, 
      where n_samples is the number of training samples and 
      n_features is the number of features.
    Y:{array-like}, share=[n_samples] = traget values.
    '''
    self.w = numpy.zeros(1 + X.shape[1]);
    self.l = numpy.zeros(self.iter_num);
    for iter_index in range(self.iter_num):
      for rand_time in range(X.shape[0]): 
        sample_index = random.randint(0, X.shape[0] - 1);
        if (self.activation(X[sample_index]) == Y[sample_index]):
          continue;
        output = self.net_input(X[sample_index]);
        errors = Y[sample_index] - output;
        self.w[0] += self.eta * errors;
        self.w[1:] += self.eta * numpy.dot(errors, X[sample_index]);
        break;
      for sample_index in range(X.shape[0]): 
        self.l[iter_index] += (Y[sample_index] - self.net_input(X[sample_index])) ** 2 * 0.5;
      logging.info("iter %s: w0(%s), w1(%s), w2(%s), l(%s)" %
          (iter_index, self.w[0], self.w[1], self.w[2], self.l[iter_index]));
      if iter_index > 1 and math.fabs(self.l[iter_index - 1] - self.l[iter_index]) < 0.0001: 
        break;

  def activation(self, x):
    return numpy.where(self.net_input(x) >= 0.0 , 1 , -1);

  def net_input(self, x): 
    return numpy.dot(x, self.w[1:]) + self.w[0];

  def predict(self, x):
    return self.activation(x);

def main():
  logging.basicConfig(level = logging.INFO,
      format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
      datefmt = '%a, %d %b %Y %H:%M:%S');

  iris = load_iris();

  features = iris.data[:99, [0, 2]];
  # normalization
  features_std = numpy.copy(features);
  for i in range(features.shape[1]):
    features_std[:, i] = (features_std[:, i] - features[:, i].mean()) / features[:, i].std();

  labels = numpy.where(iris.target[:99] == 0, -1, 1);

  # 2/3 data from training, 1/3 data for testing
  train_features, test_features, train_labels, test_labels = train_test_split(
      features_std, labels, test_size = 0.33, random_state = 23323);
  
  logging.info("train set shape:%s" % (str(train_features.shape)));

  classifier = Adaline();

  classifier.train(train_features, train_labels);
    
  test_predict = numpy.array([]);
  for feature in test_features:
    predict_label = classifier.predict(feature);
    test_predict = numpy.append(test_predict, predict_label);

  score = accuracy_score(test_labels, test_predict);
  logging.info("The accruacy score is: %s "% (str(score)));

  #plot
  x_min, x_max = train_features[:, 0].min() - 1, train_features[:, 0].max() + 1;
  y_min, y_max = train_features[:, 1].min() - 1, train_features[:, 1].max() + 1;
  plt.xlim(x_min, x_max);
  plt.ylim(y_min, y_max);
  plt.xlabel("width");
  plt.ylabel("heigt");

  plt.scatter(train_features[:, 0], train_features[:, 1], c = train_labels, marker = 'o', s = 10);

  k = - classifier.w[1] / classifier.w[2];
  d = - classifier.w[0] / classifier.w[2];

  plt.plot([x_min, x_max], [k * x_min + d, k * x_max + d], "go-");

  plt.show();
  

if __name__ == "__main__":
  main();

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Pytorch高階OP操作where,gather原理

    Pytorch高階OP操作where,gather原理

    這篇文章主要介紹了Pytorch高階OP操作where,gather原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • python實現(xiàn)梯度下降求解邏輯回歸

    python實現(xiàn)梯度下降求解邏輯回歸

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)梯度下降求解邏輯回歸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Numpy之reshape()使用詳解

    Numpy之reshape()使用詳解

    今天小編就為大家分享一篇Numpy之reshape()使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python使用docx模塊實現(xiàn)刷題功能代碼

    Python使用docx模塊實現(xiàn)刷題功能代碼

    今天小編就為大家分享一篇Python使用docx模塊實現(xiàn)刷題功能代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • 很酷的python表白工具 你喜歡我嗎

    很酷的python表白工具 你喜歡我嗎

    這篇文章主要為大家分享了一款很酷的python表白工具,可以發(fā)給女生表白用,界面簡單,實用性強,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Python腳本代碼加速運行優(yōu)化技巧

    Python腳本代碼加速運行優(yōu)化技巧

    這篇文章主要介紹了Python腳本代碼加速運行優(yōu)化技巧,Python是一種解釋性語言,雖然易于使用和閱讀,但在性能方面可能不如編譯語言如C或C++,然而,通過一些優(yōu)化技巧,可以顯著提高Python代碼的運行速度
    2021-04-04
  • 淺談Python pygame繪制機制

    淺談Python pygame繪制機制

    今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著Python pygame繪制機制展開,文中有非常詳細(xì)的介紹及圖文示例,需要的朋友可以參考下
    2021-06-06
  • python中的多重繼承實例講解

    python中的多重繼承實例講解

    這篇文章主要介紹了python中的多重繼承實例講解,本文著重講解尋找父類的順序,分為經(jīng)典類和新式類,需要的朋友可以參考下
    2014-09-09
  • Pandas 解決dataframe的一列進(jìn)行向下順移問題

    Pandas 解決dataframe的一列進(jìn)行向下順移問題

    今天小編就為大家分享一篇Pandas 解決dataframe的一列進(jìn)行向下順移問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python format 格式化輸出方法

    python format 格式化輸出方法

    今天小編就為大家分享一篇python format 格式化輸出方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07

最新評論

和平县| 阿坝县| 黄梅县| 泰和县| 深水埗区| 巴塘县| 福海县| 县级市| 聊城市| 舞钢市| 巴林左旗| 溆浦县| 庄浪县| 酉阳| 苏尼特右旗| 山阳县| 浮梁县| 响水县| 彰化县| 鄂托克前旗| 开化县| 精河县| 平邑县| 淮南市| 鹤峰县| 洛南县| 博爱县| 乌恰县| 定兴县| 石城县| 随州市| 安丘市| 浦县| 台安县| 买车| 瑞安市| 博野县| 固始县| 肥东县| 临沧市| 安丘市|