感知器基础原理及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修改Excel数据的实例代码
Nov 01 Python
使用Python实现下载网易云音乐的高清MV
Mar 16 Python
Python编程之字符串模板(Template)用法实例分析
Jul 22 Python
Python KMeans聚类问题分析
Feb 23 Python
自学python的建议和周期预算
Jan 30 Python
利用python在excel里面直接使用sql函数的方法
Feb 08 Python
Python中psutil的介绍与用法
May 02 Python
python3.6根据m3u8下载mp4视频
Jun 17 Python
使用python实现画AR模型时序图
Nov 20 Python
Python hmac模块使用实例解析
Dec 24 Python
python 控制台单行刷新,多行刷新实例
Feb 19 Python
Python安装OpenCV的示例代码
Mar 05 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实现的英文名字全拼随机排号脚本
2014/07/04 PHP
php实现无限级分类查询(递归、非递归)
2016/03/10 PHP
简单谈谈PHP中的include、include_once、require以及require_once语句
2016/04/23 PHP
laravel7学习之无限级分类的最新实现方法
2020/09/30 PHP
Javascript开发包大全整理
2006/12/22 Javascript
改进:论坛UBB代码自动插入方式
2006/12/22 Javascript
利用404错误页面实现UrlRewrite的实现代码
2008/08/20 Javascript
Js从头学起(基本数据类型和引用类型的参数传递详细分析)
2012/02/16 Javascript
js 操作select和option常用代码整理
2012/12/13 Javascript
createTextRange()的使用示例含文本框选中部分文字内容
2014/02/24 Javascript
JavaScript 实现简单的倒计时弹窗DEMO附图
2014/03/05 Javascript
全面解析Bootstrap排版使用方法(文字样式)
2015/11/30 Javascript
JS表单验证的代码(常用)
2016/04/08 Javascript
js实现图片左右滚动效果
2017/02/27 Javascript
node.js实现登录注册页面
2017/04/08 Javascript
详解vue.js 开发环境搭建最简单攻略
2017/06/12 Javascript
Windows下Node.js安装及环境配置方法
2017/09/18 Javascript
JavaScript类型相关的常用操作总结
2019/02/14 Javascript
js 计算图片内点个数的示例代码
2019/04/04 Javascript
JS实现简单日历特效
2020/01/03 Javascript
Nodejs环境实现socket通信过程解析
2020/07/03 NodeJs
基于vue实现简易打地鼠游戏
2020/08/21 Javascript
Python正则简单实例分析
2017/03/21 Python
如何安装多版本python python2和python3共存以及pip共存
2018/09/18 Python
Python3中bytes类型转换为str类型
2018/09/27 Python
Python3调用百度AI识别图片中的文字功能示例【测试可用】
2019/03/13 Python
python批量修改ssh密码的实现
2019/08/08 Python
python 实现多线程下载m3u8格式视频并使用fmmpeg合并
2019/11/15 Python
Tensorflow tf.nn.atrous_conv2d如何实现空洞卷积的
2020/04/20 Python
Linux如何压缩可执行文件
2013/10/21 面试题
为什么需要版本控制?
2013/08/08 面试题
毕业生找工作的自我评价
2013/10/18 职场文书
党章学习思想汇报
2014/01/14 职场文书
市场营销调查计划书
2014/05/02 职场文书
讲座新闻稿
2015/07/18 职场文书
新闻稿怎么写
2015/07/18 职场文书