感知机算法python实现
来源:互联网 发布:照片白边框软件 编辑:程序博客网 时间:2024/05/17 23:28
1. 感知机
在机器学习中,感知机(Perceptron)是二分类的线性分类模型,属于监督学习算法。输入为实例的特征向量,输出为实例的类别(取+1和-1)。感知机对应于输入空间中将实例划分为两类的分离超平面。感知机旨在求出该超平面,为求得超平面导入了基于误分类的损失函数,利用随机梯度下降法(SGD)对损失函数进行最优化。
2. 感知机python实现
2.1 数据
在二维坐标图中表示如下:
2.2 python实现
#!/usr/bin/python3# -*- coding: utf-8 -*-# @Time : 2017/11/17 20:06# @Author : Z.C.Wang# @Email : iwangzhengchao@gmail.com# @File : PLA.py# @Software: PyCharm Community Edition"""Description :Perceptron learning algorithm"""import numpy as npimport matplotlib.pyplot as plt# load data from txtdata_set = []data_label = []file = open('DataSet_linear_separable.txt')for line in file: line = line.split('\t') for i in range(len(line)): line[i] = float(line[i]) data_set.append(line[0:2]) data_label.append(int(line[-1]))file.close()data = np.array(data_set)label = np.array(data_label)# 初始化w, b, alphaw = np.array([0, 0])b = 0alpha = 1# 计算 y*(w*x+b)f = (np.dot(data, w.T) + b) * labelidx = np.where(f <= 0)# 使用随机梯度下降法求解w, biteration = 1while f[idx].size != 0: point = np.random.randint((f[idx].shape[0])) x = data[idx[0][point], :] y = label[idx[0][point]] w = w + alpha * y * x b = b + alpha * y print('Iteration:%d w:%s b:%s' % (iteration, w, b)) f = (np.dot(data, w.T) + b) * label idx = np.where(f <= 0) iteration = iteration + 1# 绘图显示x1 = np.arange(0, 6, 0.1)x2 = (w[0] * x1 + b) / (-w[1])idx_p = np.where(label == 1)idx_n = np.where(label != 1)data_p = data[idx_p]data_n = data[idx_n]plt.scatter(data_p[:, 0], data_p[:, 1], color='red')plt.scatter(data_n[:, 0], data_n[:, 1], color='blue')plt.plot(x1, x2)plt.show()print('\nPerceptron learning algorithm is over')
2.3 运行结果
结果如下:
Iteration:1 w:[-4.2 -5. ] b:-1Iteration:2 w:[-2.2 -3.5] b:0Iteration:3 w:[-0.2 -2. ] b:1Iteration:4 w:[ 2.2 -0.7] b:2Iteration:5 w:[-0.4 -3.7] b:1Iteration:6 w:[ 1.4 -2.2] b:2Iteration:7 w:[ 0.9 -3.2] b:1Iteration:8 w:[ 3.3 -1.9] b:2Iteration:9 w:[ 1.65 -3.8 ] b:1Iteration:10 w:[ 4.65 -1.6 ] b:2Iteration:11 w:[ 2.05 -4.6 ] b:1Iteration:12 w:[ 4.05 -3.1 ] b:2Iteration:13 w:[ 2.4 -5. ] b:1Iteration:14 w:[ 5.5 -2.2] b:2Iteration:15 w:[ 3.85 -4.1 ] b:1Perceptron learning algorithm is over
阅读全文
0 0
- 感知机算法python实现
- [python]感知机学习算法实现
- 用python实现简单感知机算法
- Python感知机算法
- python实现感知器算法
- 感知机Python实现
- 感知机实现Python
- python感知机实现
- 感知机学习算法的简单实现(Python)
- 词性标注的python实现-基于平均感知机算法
- 感知机算法原理(PLA原理)及 Python 实现
- 词性标注的python实现-基于平均感知机算法
- 感知哈希算法--python实现
- 神经网络-感知器算法python代码实现
- python实现自适应线性感知器算法
- 感知机的 python 实现
- #感知机的Python实现
- 感知机学习算法实现
- redis的持久化机制
- pygame的学习以及python的巩固(窗口尺寸的显示)
- python 文件读写,删除指定行
- 九:模块初识
- java与数据库
- 感知机算法python实现
- 设计模式之:代理模式
- 例题8-7 唯一的雪花(Unique snowflakes, UVa 11572)
- Code Review 代码审查 不完全整理
- hibernate的一级缓存(六)
- golang基础-kafka、zookeeper搭建、go终端发送数据给kafka
- U盘插上,系统有反应,但是却不识别,电脑能识别其他的U盘,U盘在其他电脑也是好用的经验分享(驱动问题)
- HDU 4474
- 十:.pyc是个什么?