统计机器学习笔记(一)---感知机模型
来源:互联网 发布:电视阿里云与安卓区别 编辑:程序博客网 时间:2024/05/14 18:35
代码
一、公共绘图部分
import numpy as npfrom matplotlib import pyplot as pltfrom matplotlib import animationfrom functools import partial# first set up the figure, the axis, and the plot element we want to animate__FIG = plt.figure()__AX = plt.axes(xlim=(0, 2), ylim=(-2, 2))__LINE, = __AX.plot([], [], 'g', lw = 2)__LABEL = __AX.text([], [], '')# initialization function: plot the background of each framedef __init(trains): __LINE.set_data([], []) x, y, x_, y_ = [], [], [], [] for p in trains: if p[1] > 0: x.append(p[0][0]) y.append(p[0][1]) else: x_.append(p[0][0]) y_.append(p[0][1]) plt.plot(x, y, 'bo', x_, y_, 'rx') plt.axis([-6, 6, -6, 6]) plt.grid(True) plt.xlabel('x') plt.ylabel('y') plt.title('Prceptron Algorithm(wendale.cn)') return __LINE, __LABEL# animation function. this is called sequentiallydef __animate(history, i): global __AX, __LINE, __LABEL w = history[i][0] b = history[i][1] if w[1] == 0: return __LINE, __LABEL x1 = -7 y1 = -(b + w[0]*x1)/w[1] x2 = 7 y2 = -(b + w[0]*x2)/w[1] __LINE.set_data([x1, x2], [y1, y2]) x1 = 0 y1 = -(b + w[0] * x1) / w[1] __LABEL.set_text(history[i]) __LABEL.set_position([x1, y1]) return __LINE, __LABELdef show(trains, history, save=False, name=''): # call the animator. blit=true means only re-draw the parts that have changed. anim = animation.FuncAnimation(__FIG, partial(__animate, history), init_func=partial(__init, trains), frames=len(history), interval=1000, repeat=True, blit=True) plt.show() if save: anim.save(name, fps=2, writer='imagemagick')if __name__ == '__main__': trains = np.array([[(3, 3), 1], [(4, 3), 1], [(1, 1), -1]]) history = [[[1.0, 0.0, -0.0], 1], [[1.0, 0.0, -1.0], 0], [[1.0, 0.0, -2.0], -1], [[1.0, 0.0, -3.0], -2], [[2.0, 0.0, -3.0], -1], [[2.0, 0.0, -4.0], -2], [[2.0, 0.0, -5.0], -3]] show(trains, history)
二、原始形式
import copyimport drawHISTORY = []TRAINS = [[(3, 3), 1], [(4, 3), 1], [(1, 1), -1]]def update(w, b, item): for i in range(len(item[0])): w[i] += item[1] * item[0][i] b += item[1] return w, bdef cal(item): res = 0 for i in range(len(item[0])): res += item[0][i] * w[i] res += b res *= item[1] return resdef iterComp(w, b, trains): global HISTORY flag = False for item in trains: if cal(item) <= 0: flag = True (w, b) = update(w, b, item) HISTORY.append([copy.copy(w), b]) if not flag: print "RESULT:w:" + str(w) + "b:" + str(b) return flag, w, bif __name__ == "__main__": w = [0, 0] b = 0 while True: (flag, w, b) = iterComp(w, b, TRAINS) if not flag: break draw.show(TRAINS, HISTORY, True, 'perceptron.gif')
效果
三、对偶形式
__author__ = 'wendale'import numpy as npimport drawdef __cal_gram(trains): gram = np.empty((len(trains), len(trains)), np.int) for i in range(len(trains)): for j in range(len(trains)): gram[i][j] = np.dot(trains[i][0], trains[j][0]) return gramdef __check(trains, gram, alpha, b): y = np.array(trains[:,1]) for i in range(len(trains)): if (np.dot(alpha * y, gram[i]) + b) * y[i] <= 0: return False, i return True, -1def find(trains): gram = __cal_gram(trains) alpha = np.zeros(len(trains)) b = 0 x = np.array(trains[:,0].tolist()).reshape(3,2) y = np.array(trains[:,1]) history = [] while True: (flag, i) = __check(trains, gram, alpha, b) if flag: break #update alpha[i] += 1 b += trains[i][1] history.append([np.dot(alpha * y, x).tolist(), b]) return alpha, b, historyif __name__ == '__main__': trains = np.array([[[3, 3], 1], [[4, 3], 1], [[1, 1], -1]]) (a, b, history) = find(trains) draw.show(trains, history, True, 'duality_perceptron.gif')
效果
参考
参考1
0 0
- 统计机器学习笔记(一)---感知机模型
- 统计学习笔记--感知机模型
- 《统计学习方法》-感知机模型学习笔记
- 机器学习笔记一:感知机
- 【机器学习】感知机学习---《统计学习方法》学习笔记
- 机器学习:感知机模型
- 机器学习模型--感知机
- 统计机器学习笔记——感知机(1)
- 机器学习笔记(0)-统计学习方法与感知机
- 统计学习笔记(2)——感知机模型
- 统计学习笔记(2)——感知机模型
- 统计学习笔记(2)——感知机模型
- 统计学习笔记(2)——感知机模型
- 统计学习笔记(2)——感知机模型
- 《统计学习方法》学习笔记—感知机模型python实现
- 统计学习笔记(2)——感知机模型
- 机器学习-感知机笔记
- 机器学习学习笔记3---感知机
- Spring笔记——Bean后处理器
- crsctl stat res -t 和crsctl stat res -t -init 中STATE_DETAILS的解释
- 自定义Label的行间距
- Core Data
- ubuntu 设置共享文件夹
- 统计机器学习笔记(一)---感知机模型
- unity 关于模型的边界框bounds
- Iterator的数据访问方式
- codeforces 498D
- UVA 11806-Cheerleaders-容斥原理+组合数打表
- HDU 2647 Reward (拓扑排序 + 链式前向星 )
- PL/SQL解决汉字显示乱码问题
- ARM-Linux应用程序自动升级以及自动回滚的实现
- android插件化开发,1个主app,N个插件app