感知器基础原理及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 排序算法总结及实例详解
Sep 28 Python
Flask框架的学习指南之开发环境搭建
Nov 20 Python
python奇偶行分开存储实现代码
Mar 19 Python
基于python requests库中的代理实例讲解
May 07 Python
python3中类的继承以及self和super的区别详解
Jun 26 Python
python Django 创建应用过程图示详解
Jul 29 Python
pycharm编写spark程序,导入pyspark包的3中实现方法
Aug 02 Python
python如何从文件读取数据及解析
Sep 19 Python
Django之form组件自动校验数据实现
Jan 14 Python
Pytho爬虫中Requests设置请求头Headers的方法
Sep 22 Python
Python+Selenium实现读取网易邮箱验证码
Mar 13 Python
在NumPy中深拷贝和浅拷贝相关操作的定义和背后的原理
Apr 14 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 分页类实现代码
2009/12/03 PHP
PHP 柱状图实现代码
2009/12/04 PHP
php 传值赋值与引用赋值的区别
2010/12/29 PHP
PHP中创建和验证哈希的简单方法实探
2015/07/06 PHP
使用php-timeit估计php函数的执行时间
2015/09/06 PHP
PHP hex2bin()函数用法讲解
2019/02/25 PHP
php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例
2019/05/09 PHP
jquery png 透明解决方案(推荐)
2010/08/21 Javascript
jQuery实现友好的轮播图片特效
2015/01/12 Javascript
director.js实现前端路由使用实例
2015/02/03 Javascript
jQuery表格插件datatables用法详解
2020/11/23 Javascript
实例详解AngularJS实现无限级联动菜单
2016/01/15 Javascript
jQuery+css实现的换页标签栏效果
2016/01/27 Javascript
javascript实现移动端上的触屏拖拽功能
2016/03/04 Javascript
JS轮播图中缓动函数的封装
2020/11/25 Javascript
jQuery实现的下雪动画效果示例【附源码下载】
2018/02/02 jQuery
Vue 让元素抖动/摆动起来的实现代码
2018/05/31 Javascript
js指定日期增加指定月份的实现方法
2018/12/19 Javascript
详解Vue调用手机相机和相册以及上传
2019/05/05 Javascript
原生js实现随机点餐效果
2019/12/10 Javascript
在HTML中使用JavaScript的两种方法
2020/12/24 Javascript
在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程
2015/04/25 Python
九步学会Python装饰器
2015/05/09 Python
python3+selenium实现qq邮箱登陆并发送邮件功能
2019/01/23 Python
python简单贪吃蛇开发
2019/01/28 Python
Weekendesk意大利:探索多种引人入胜的周末主题
2016/10/14 全球购物
Lacoste(法国鳄鱼)加拿大官网:以标志性的POLO衫而闻名
2019/05/15 全球购物
酒店前台接待岗位职责
2013/12/03 职场文书
微笑服务演讲稿
2014/05/13 职场文书
创先争优标语
2014/06/27 职场文书
2014新生大学四年计划书
2014/09/21 职场文书
班子群众路线教育实践个人对照检查材料思想汇报
2014/09/30 职场文书
入党宣誓仪式主持词
2015/06/29 职场文书
《夸父追日》教学反思
2016/02/20 职场文书
Python使用random模块实现掷骰子游戏的示例代码
2021/04/29 Python
Pytest中skip skipif跳过用例详解
2021/06/30 Python