感知器基础原理及python实现过程详解


Posted in Python onSeptember 30, 2019

简单版本,按照李航的《统计学习方法》的思路编写

感知器基础原理及python实现过程详解

数据采用了著名的sklearn自带的iries数据,最优化求解采用了SGD算法。

预处理增加了标准化操作。

'''
perceptron classifier

created on 2019.9.14
author: vince
'''
import pandas 
import numpy 
import logging
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

'''
perceptron classifier

Attributes
w: ld-array = weights after training
l: list = number of misclassification during each iteration 
'''
class Perceptron:
  def __init__(self, eta = 0.01, iter_num = 50, 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 sample_index in range(X.shape[0]): 
        if (self.activation(X[sample_index]) != Y[sample_index]):
          logging.debug("%s: pred(%s), label(%s), %s, %s" % (sample_index, 
            self.net_input(X[sample_index]) , Y[sample_index],
            X[sample_index, 0], X[sample_index, 1]));
          self.l[iter_index] += 1;
      for sample_index in range(X.shape[0]): 
        if (self.activation(X[sample_index]) != Y[sample_index]):
          self.w[0] += self.eta * Y[sample_index];
          self.w[1:] += self.eta * numpy.dot(X[sample_index], Y[sample_index]);
          break;
      logging.info("iter %s: %s, %s, %s, %s" %
          (iter_index, self.w[0], self.w[1], self.w[2], self.l[iter_index]));

  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)));

  p = Perceptron();

  p.train(train_features, train_labels);
    
  test_predict = numpy.array([]);
  for feature in test_features:
    predict_label = p.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 = - p.w[1] / p.w[2];
  d = - p.w[0] / p.w[2];

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

  plt.show();
  

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

感知器基础原理及python实现过程详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python每隔N秒运行指定函数的方法
Mar 16 Python
Python黑帽编程 3.4 跨越VLAN详解
Sep 28 Python
Python多线程实现同步的四种方式
May 02 Python
python3.4用循环往mysql5.7中写数据并输出的实现方法
Jun 20 Python
python2.7 json 转换日期的处理的示例
Mar 07 Python
python 编写简单网页服务器的实例
Jun 01 Python
Python面向对象之接口、抽象类与多态详解
Aug 27 Python
详解python使用pip安装第三方库(工具包)速度慢、超时、失败的解决方案
Dec 02 Python
Python实现二叉树的常见遍历操作总结【7种方法】
Mar 06 Python
Django中在xadmin中集成DjangoUeditor过程详解
Jul 24 Python
Python getattr()函数使用方法代码实例
Aug 10 Python
python在协程中增加任务实例操作
Feb 28 Python
基于python的BP神经网络及异或实现过程解析
Sep 30 #Python
Window10下python3.7 安装与卸载教程图解
Sep 30 #Python
Python检查图片是否损坏及图片类型是否正确过程详解
Sep 30 #Python
Python3 合并二叉树的实现
Sep 30 #Python
自适应线性神经网络Adaline的python实现详解
Sep 30 #Python
softmax及python实现过程解析
Sep 30 #Python
python根据时间获取周数代码实例
Sep 30 #Python
You might like
php计算title标题相似比的方法
2015/07/29 PHP
WordPress中用于获取及自定义头像图片的PHP脚本详解
2015/12/17 PHP
JavaScript Perfection kill 测试及答案
2010/03/23 Javascript
js操作时间(年-月-日 时-分-秒 星期几)
2010/06/20 Javascript
JavaScript中的console.trace()函数介绍
2014/12/29 Javascript
ionic实现可滑动的tab选项卡切换效果
2020/04/15 Javascript
JavaScript实现自动切换图片代码
2016/10/11 Javascript
jQuery的ready方法实现原理分析
2016/10/26 Javascript
JavaScript利用Date实现简单的倒计时实例
2017/01/12 Javascript
实现div滚动条默认最底部以及默认最右边的示例代码
2017/11/15 Javascript
vue.js单文件组件中非父子组件的传值实例
2018/09/13 Javascript
JS实现的小火箭发射动画效果示例
2018/12/08 Javascript
js实现图片区域可点击大小随意改变(适用移动端)代码实例
2019/09/11 Javascript
vue使用@scroll监听滚动事件时,@scroll无效问题的解决方法详解
2019/10/15 Javascript
[38:42]完美世界DOTA2联赛循环赛 Matador vs Forest BO2第二场 11.05
2020/11/05 DOTA
python简单获取数组元素个数的方法
2015/07/13 Python
详解Python爬虫的基本写法
2016/01/08 Python
python去除拼音声调字母,替换为字母的方法
2018/11/28 Python
python 直接赋值和copy的区别详解
2019/08/07 Python
Python queue队列原理与应用案例分析
2019/09/27 Python
python 用 xlwings 库 生成图表的操作方法
2019/12/22 Python
django2.2 和 PyMySQL版本兼容问题
2020/02/17 Python
使用python3 实现插入数据到mysql
2020/03/02 Python
如何基于windows实现python定时爬虫
2020/05/01 Python
Django解决frame拒绝问题的方法
2020/12/18 Python
自然健康的概念:Natural Healthy Concepts
2020/01/26 全球购物
一套英文Java笔试题面试题
2016/04/21 面试题
浙江文明网签名寄语
2014/01/18 职场文书
派出所所长先进事迹
2014/05/19 职场文书
奥巴马当选演讲稿
2014/09/10 职场文书
党在我心中的演讲稿
2014/09/13 职场文书
2014年乡镇纪委工作总结
2014/12/19 职场文书
值班管理制度范本
2015/08/06 职场文书
导游词之青岛太清宫
2019/12/13 职场文书
golang slice元素去重操作
2021/04/30 Golang
Nginx源码编译安装过程记录
2021/11/17 Servers