感知器基础原理及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解析xml文件实例分析
May 27 Python
Python 12306抢火车票脚本 Python京东抢手机脚本
Feb 06 Python
详谈在flask中使用jsonify和json.dumps的区别
Mar 26 Python
Anaconda下安装mysql-python的包实例
Jun 11 Python
Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例
Jul 18 Python
详解Python打包分发工具setuptools
Aug 05 Python
Python 仅获取响应头, 不获取实体的实例
Aug 21 Python
pytorch中的自定义反向传播,求导实例
Jan 06 Python
详解python命令提示符窗口下如何运行python脚本
Sep 11 Python
python反爬虫方法的优缺点分析
Nov 25 Python
Python 如何实现文件自动去重
Jun 02 Python
Python 避免字典和元组的多重嵌套问题
Jul 15 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
把77A收信机改造成收音机
2021/03/02 无线电
将OICQ数据转成MYSQL数据
2006/10/09 PHP
PHP 中文乱码解决办法总结分析
2009/07/30 PHP
解析PHP 使用curl提交json格式数据
2013/06/29 PHP
phpmyadmin提示The mbstring extension is missing的解决方法
2014/12/17 PHP
php获取文件类型和文件信息的方法
2015/07/10 PHP
PHP预定义变量9大超全局数组用法详解
2016/04/23 PHP
源码分析 Laravel 重复执行同一个队列任务的原因
2017/12/25 PHP
laravel 验证错误信息到 blade模板的方法
2019/09/29 PHP
JavaScript获取FCK编辑器信息的具体方法
2013/07/12 Javascript
jquery事件与函数的使用介绍
2013/09/29 Javascript
javascript获取选中的文本的方法代码
2013/10/30 Javascript
JavaScript原型链示例分享
2014/01/26 Javascript
node.js中的fs.chownSync方法使用说明
2014/12/16 Javascript
js动态创建及移除div的方法
2015/06/03 Javascript
在vscode中统一vue编码风格的方法
2018/02/22 Javascript
基于Vue实现微信小程序的图文编辑器
2018/07/25 Javascript
Seajs源码详解分析
2019/04/02 Javascript
jquery实现聊天机器人
2020/02/08 jQuery
深入解析Python中的list列表及其切片和迭代操作
2016/03/13 Python
python fabric实现远程部署
2017/01/05 Python
Python编程实现二分法和牛顿迭代法求平方根代码
2017/12/04 Python
十个Python练手的实战项目,学会这些Python就基本没问题了(推荐)
2019/04/26 Python
PyCharm 2020.2下配置Anaconda环境的方法步骤
2020/09/23 Python
Python本地及虚拟解释器配置过程解析
2020/10/13 Python
解决python3中os.popen()出错的问题
2020/11/19 Python
整理HTML5中支持的URL编码与字符编码
2016/02/23 HTML / CSS
canvas 阴影和图形变换的示例代码
2018/01/02 HTML / CSS
PacSun官网:加州生活方式服装、鞋子和配饰
2018/03/10 全球购物
会计专业自我鉴定范文
2013/12/29 职场文书
加工操作管理制度
2014/01/19 职场文书
大学生学习面向未来的赶考思想汇报
2014/09/12 职场文书
运动会报道稿300字
2014/10/02 职场文书
2014年园林绿化工作总结
2014/12/11 职场文书
心得体会该怎么写呢?
2019/06/27 职场文书
PHP策略模式写法
2021/04/01 PHP