简单易学的机器学习算法——在线顺序极限学习机OS-ELM

news/2024/10/30 22:21:51/

   这篇文章主要是前面整理的,就直接上图了。








实验:

# coding:UTF-8
#################
# OS_ELM
# author : zhiyong_will
# date : 2015.3.22
#################
from __future__ import division
from datetime import datetime
from csv import DictReader
from math import exp
import random
from numpy import *
import string#####设置相关参数########
trainData = "C:\\Users\\dell\\Desktop\\OS-ELM\\segment_train.csv"
testData = "C:\Users\dell\Desktop\OS-ELM\\segment_test.csv"
#隐含神经元的个数
nHiddenNeurons = 180
#输入层的神经元个数
nInputNeurons = 19
#初始训练集的大小
NO = 280#函数
def sig(tData, Iw, bias, num):'''tData:样本矩阵:样本数*特征数Iw:输入层到第一个隐含层的权重:隐含层神经元数*特整数bias:偏置1*隐含神经元个数'''v = tData * Iw.T   #样本数*隐含神经元个数bias_1 = ones((num, 1)) * biasv = v + bias_1H = 1./(1+exp(-v))return H##导入数据集
firstTrainData = []
firstTrainLable = []# 处理训练样本
for t, row in enumerate(DictReader(open(trainData))):Id = row['Id']del row['Id']del row['I0']data = []if int(Id) < NO:# 处理是否被点击if row['Label'] == '1.00000000':y = 1elif row['Label'] == '2.00000000':y = 2elif row['Label'] == '3.00000000':y = 3elif row['Label'] == '4.00000000':y = 4elif row['Label'] == '5.00000000':y = 5elif row['Label'] == '6.00000000':y = 6else:y = 7        del row['Label']firstTrainLable.append(y)# 处理特征for key in row:value = string.atof(row[key])#index = int(value + key[1:], 16) % Ddata.append(value)firstTrainData.append(data)continueelif int(Id) == NO:#开始训练p0 = mat(firstTrainData)T0 = zeros((NO, 7))#处理样本标签for i in xrange(0, NO): a = firstTrainLable[i]T0[i][a-1] = 1T0 = T0 * 2 - 1Iw = mat(random.rand(nHiddenNeurons, nInputNeurons) * 2 - 1)#随机生成区间-1,1之间的随机矩阵bias = mat(random.rand(1, nHiddenNeurons))H0 = sig(p0, Iw, bias, NO)#样本数*隐含神经元个数M = (H0.T * H0).Ibeta = M * H0.T * T0else:#训练剩余的样本,每次训练一条样本# 处理labelif row['Label'] == '1.00000000':y = 1elif row['Label'] == '2.00000000':y = 2elif row['Label'] == '3.00000000':y = 3elif row['Label'] == '4.00000000':y = 4elif row['Label'] == '5.00000000':y = 5elif row['Label'] == '6.00000000':y = 6else:y = 7        del row['Label']Tn = zeros((1, 7))#处理样本标签b = yTn[0][b-1] = 1Tn = Tn * 2 - 1# 处理特征data = []for key in row:value = string.atof(row[key])data.append(value)pn = mat(data)H = sig(pn, Iw, bias, 1)M = M - M * H.T * (eye(1,1) + H * M * H.T).I * H * Mbeta = beta + M * H.T * (Tn - H * beta)# 计算训练误差
correct = 0
sum = 0
for t, row in enumerate(DictReader(open(trainData))):del row['Id']del row['I0']# 处理是否被点击if row['Label'] == '1.00000000':y = 1elif row['Label'] == '2.00000000':y = 2elif row['Label'] == '3.00000000':y = 3elif row['Label'] == '4.00000000':y = 4elif row['Label'] == '5.00000000':y = 5elif row['Label'] == '6.00000000':y = 6else:y = 7        del row['Label']# 处理特征data = []for key in row:value = string.atof(row[key])data.append(value)p = mat(data)HTrain = sig(p, Iw, bias, 1)Y = HTrain * beta# 判断if argmax(Y) + 1 == y:correct += 1sum += 1
print("训练准确性为:%f" % (correct/sum))# 计算测试误差
correctTest = 0
sumTest = 0
for t, row in enumerate(DictReader(open(testData))):del row['Id']del row['I0']# 处理是否被点击if row['Label'] == '1.00000000':y = 1elif row['Label'] == '2.00000000':y = 2elif row['Label'] == '3.00000000':y = 3elif row['Label'] == '4.00000000':y = 4elif row['Label'] == '5.00000000':y = 5elif row['Label'] == '6.00000000':y = 6else:y = 7        del row['Label']# 处理特征data = []for key in row:value = string.atof(row[key])data.append(value)p = mat(data)HTrain = sig(p, Iw, bias, 1)Y = HTrain * beta# 判断if argmax(Y) + 1 == y:correctTest += 1sumTest += 1
print("测试准确性为:%f" % (correctTest/sumTest))

实验结果:



参考文章:

A Fast and Accurate Online Sequential Learning Algorithm for Feedforward Networks



http://www.ppmy.cn/news/143547.html

相关文章

极限学习机的一篇小综述

极限学习机综述 简介 神经网络是由具有适应性的简单单元组成的广泛并行互联的网络&#xff0c;它的组织能过够模拟生物神经系统对真实世界物体所作出的交互反应[1]。它由大量简单的处理单元&#xff08;神经元&#xff09;相互连接&#xff0c;具有极为强大的学习功能。 但在…

超限学习机(ELM)、在线顺序超限学习机(OS-ELM)

概念 ELM是一种应用于训练SLFN的算法&#xff0c;传统的单隐层前馈神经网络&#xff08;Single hidden Layer Feedforward Network, SLFN&#xff09;由于其结构简单、训练速度快且具有较高的泛化能力等特点&#xff0c;已经在模式识别、信号处理、短期预测等领域有了很多应用…

疫情肆虐 三大学习机对比评测 助你在家预习

我们挑选了三款相近价位的不同型号来进行横向对比&#xff0c;分别是读书郎学生平板G550A、步步高家教机S3 Pro以及优学派学生平板U27&#xff0c;配置表如下&#xff1a; 从硬件对比上看&#xff0c;在同价位里三款产品差异不大&#xff0c;步步高S3 Pro屏幕尺寸稍小&#xff…

K8s相关常用命令

一、查看相关信息 查看pod相关信息 kubectl get pod --all-namespaces|grep 关键字 kubectl get po --all-namespaces -owide |grep 关键字 kubectl describe pod -n namespace 查看service相关信息 kubectl get svc -n namespace |grep 关键字 pod调度 kubectl get no…

iphone usb开热点无法共享上网

iphone开热点给电脑&#xff0c;插入数据线&#xff0c;理论上是可以上网的&#xff0c;但实际在我机器上是不行的&#xff0c;反复对比了一下 用5.10的deepin社区版是可以的,但用4.19内核的UOS就死活不行&#xff0c;网络&#xff0c;网关啥都是好的&#xff0c;就是不发包。 …

6s如何无线连电脑连接服务器,iPhone6s用itunes wifi同步到电脑图文教程

iPhone6s用itunes wifi同步到电脑教程&#xff0c;iPhone6s/6s Plus已经开售几天了&#xff0c;越来越多的果粉拿到了心仪的新机&#xff0c;不过对于部分新手果粉来说&#xff0c;iPhone6s怎么用iTunes同步呢&#xff1f;要知道&#xff0c;用iTunes同步/备份是保证手机中重要…

怎样使用计算机进行上网,台式电脑如何利用ipnone手机进行上网

随着移动互联网的发展,移动智能终端市场的竞争日益加剧。在中高端智能手机市场中,以iOS为操作系统的iPhone广受用户欢迎&#xff0c;而最近有很多小伙伴问小编台式电脑如何利用ipnone手机进行上网&#xff0c;下面就由学习啦小编跟大家分享具体的操作方法吧&#xff0c;希望对大…

关于MacBook Pro和iPhone和iPad连接无线WIFI不能上网的问题

关于MacBook Pro和iPhone和iPad连接无线WIFI不能上网的问题 三台设备连接的是同一台路由器&#xff08;我的是腾达路由器&#xff09;&#xff0c;经常显示无线WIFI正常连接的图标&#xff0c;但就是上不去网&#xff0c;(只连接MacBook Pro的时候也是显示有IP&#xff0c;但上…