Python实现感知机算法(原始形式、对偶形式)

本文提供了Python实现感知机算法的详细代码,包括原始形式和对偶形式,通过实例展示了运行效果,旨在帮助读者理解并应用感知机进行分类任务。
摘要由CSDN通过智能技术生成

上一篇文章给出了感知机算法的原理,这里给出Python实现感知机算法的代码:

感知机原始形式实现代码:

# -*- coding: utf-8 -*-
"""
Created on Sun Jul 14 09:48:09 2019
@author: 等等登登-Ande
E-mail:18356768364@163.com

感知机算法原始形式实现(perception)
data为输入数据
label为标签
n为学习率
inter为迭代次数
"""
import numpy as np
import matplotlib.pyplot as plt
def perception(data,label,n,inter):
  raw,col = np.shape(data)
  w = np.random.randn(1,col)
  b = np.random.randn(1)
  x1 = -1
  y1 = (-1/w[:,1])*(np.dot(w[:,0],x1)+b)
  x2 = 2
  y2 = (-1/w[:,1])*(np.dot(w[:,0],x2)+b)
  plt.plot([x1,x2],[y1,y2],color='red')
  plt.scatter(data[1:15,0],data[1:15,1],color='blue',marker='x')
  plt.scatter(data[16:,0],data[16:,1],color='red',marker='o')
  plt.show()
  for times in range(inter):
    for i in range(raw):
      if -label[i-1]*(np.dot(w,(data[:][i-1]))+b) < 0:
        w -= n*label[i-1]*data[:][i-1]
        b -= n*label[i-1]
      elif -label[i-1]*(np.dot(w,(data[:][i-1]))+b) == 0:
        break
  return w,b
    
if __name__ == '__main__':
  data = np.array([
      [-0.6508, 0.1097],
      [-1.4492, 0.8896],
      [2.0850, 0.6876],
      [0.2626, 1.1476],
      [0.6418, 1.0234],
      [0.2569, 0.6730],
      [1.1155, 0.6043],
      [0.0914, 0.3399],
      [0.0121, 0.5256],
      [-0.0429, 0.4660],
      [0.4340, 0.6870],
      [0.2735, 1.0287],
      [0.4839, 0.4851],
      [0.4089, -0.1267],
      [1.4391, 0.1614],
      [2.9115, 2.1973],
      [2.3654, 2.0475],
      [2.2144, 2.7515],
      [2.2013, 2.0014],
      [2.6483, 2.2183],
      [2.1147, 2.2242],
      [2.7970, 2.8795],
      [2.0625, 2.6366],
      [2.5307, 2.1285],
      [2.2200, 2.7777],
      [2.3957, 2.1076],
      [2.1013, 2.5989],
      [2.4482, 2.9455],
      [2.0149, 2.6192],
      [2.2012, 2.2611]
  
  ])
  label = np.array([-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
  w,b = perception(data,label,0.5,1000)
  x1 = -2
  y1 = (-1/w[:,1])*(np.dot(w[:,0],x1)+b)
  x2 = 3
  y2 = (-1/w[:,1])*(np.dot(w[:,0],x2)+b)
  plt.plot([x1,x2],[y1,y2],color='red')
  plt.scatter(data[1:15,0],data[1:15,1],color='blue',marker='x')
  plt.scatter(data[16:,0],data[16:,1],color='red',marker='o')
  plt.show()

运行效果:

 感知机对偶形式实现代码:

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 17 10:53:30 2019
@author: 等等登登-Ande
E-mail:18356768364@163.com

感知机算法对偶形式实现(perception)
data为输入数据
label为标签
n为学习率
inter为迭代次数
"""

import numpy as np
import matplotlib.pyplot as plt
def perception_an(data,label,n,inter):
   #初始化参数
   a = np.random.randn(len(data))
   b = 0
   Gram = np.dot(data,data.T)
   for k in range(inter):
     for i in range(len(data)):
       tmp = 0
       for j in range(len(data)):
         tmp += a[j]*label[j]*Gram[i][j]
       tmp += b
       if (label[i]*tmp <= 0):
         a[i] += n
         b += n*label[i]
   w = 0
   for i in range(len(a)):
     w += a[i]*label[i]*data[i,:]      
   return w,b
  
if __name__ == '__main__':
  data = np.array([
      [-0.6508, 0.1097],
      [-1.4492, 0.8896],
      [2.0850, 0.6876],
      [0.2626, 1.1476],
      [0.6418, 1.0234],
      [0.2569, 0.6730],
      [1.1155, 0.6043],
      [0.0914, 0.3399],
      [0.0121, 0.5256],
      [-0.0429, 0.4660],
      [0.4340, 0.6870],
      [0.2735, 1.0287],
      [0.4839, 0.4851],
      [0.4089, -0.1267],
      [1.4391, 0.1614],
      [2.9115, 2.1973],
      [2.3654, 2.0475],
      [2.2144, 2.7515],
      [2.2013, 2.0014],
      [2.6483, 2.2183],
      [2.1147, 2.2242],
      [2.7970, 2.8795],
      [2.0625, 2.6366],
      [2.5307, 2.1285],
      [2.2200, 2.7777],
      [2.3957, 2.1076],
      [2.1013, 2.5989],
      [2.4482, 2.9455],
      [2.0149, 2.6192],
      [2.2012, 2.2611]
  
  ])
  label = np.array([-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
  w,b = perception_an(data,label,0.5,1)
  x1 = -2
  y1 = (-1/w[1])*(np.dot(w[0],x1)+b)
  x2 = 3
  y2 = (-1/w[1])*(np.dot(w[0],x2)+b)
  plt.plot([x1,x2],[y1,y2],color='red')
  plt.scatter(data[1:15,0],data[1:15,1],color='blue',marker='x')
  plt.scatter(data[16:,0],data[16:,1],color='red',marker='o')
  plt.show()

运行效果:

 上面就是python实现感知机的代码,希望对大家有所帮助~

每天进步一点点~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值