MATLAB神经网络(1)之R练习

将在MATLAB神经网络中学到的知识用R进行适当地重构,再写一遍,一方面可以加深理解和记忆,另一方面练习R,比较R和MATLAB的不同。
如要在R中使用之前的数据,应首先在MATLAB中用writetable函数将原本的由mat文件读入的数据写到csv文件中,以备R读入。

writetable(T,filename) writes to a file with the name and extension specified by filename.

writetable determines the file format based on the specified extension. The extension must be one of the following:

  1. .txt, .dat, or .csv for delimited text files
  2. .xls, .xlsm, or .xlsx for Excel® spreadsheet files
  3. .xlsb for Excel spreadsheet files supported on systems with Excel for Windows® See doc writetable.
writetable(table(c1),"data1.csv");
writetable(table(c2),"data2.csv");
writetable(table(c3),"data3.csv");
writetable(table(c4),"data4.csv");

这里我们使用R中十分经典的鸢尾花数据集iris(在dplyr包中)。

library(dplyr)

## Warning: package 'dplyr' was built under R version 3.5.3

##
## Attaching package: 'dplyr'

## The following objects are masked from 'package:stats':
##
## filter, lag

## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union

dim(iris)

## [1] 150 5

str(iris)

## 'data.frame': 150 obs. of 5 variables:
## Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

可以看到该数据集共有150组数据,4个自变量,1个因变量(factor),鸢尾花有3类。

sort: sort a vector or factor (partially) into ascending or descending order.

order: returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments.

k<-rnorm()
n<-order(k)
input<-iris[,1:]
output1<-iris[,]
output1<-as.integer(output1)
output<-matrix(,,)
output<-as.data.frame(output)
#把输出从1维变成3维
for(i in
1:)
{
if(output1[i]==)
output[i,]=
else
if(output1[i]==)
output[i,]=
else
output[i,]=
}
input_train=input[n[1:],]
output_train=output[n[1:],]
input_test=input[n[121:],]
input_test=input[n[121:],]
me<-apply(input_train,,mean)
va<-apply(input_train,,var)
me

## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 5.875000 3.030000 3.849167 1.239167

va

## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 0.6707983 0.1876639 3.0223522 0.5622346

#输入数据归一化
inputn<-scale(input_train)

下面进行神经网络初始化。

结构4-5-3

innum<-
midnum<-
outnum<-
#权值初始化
w1<-matrix(rnorm(innum*midnum),midnum,innum)
b1<-rnorm(midnum)
w2<-matrix(rnorm(outnum*midnum),midnum,outnum)
b2<-rnorm(outnum)
#学习率
xite<-0.1
loopNumber<-
I<-rep(,midnum)
Iout<-rep(,midnum)
FI<-rep(,midnum)
dw1<-matrix(,innum,midnum)
db1<-rep(,midnum)

神经网络训练

E<-rep(,loopNumber)
for(ii in
1:loopNumber)
{
for(i in
1:)
{
x=inputn[i,]
for(j in
1:midnum)
{
I[j]<-sum(inputn[i,]*w1[j,])+b1[j]
Iout[j]<-1/(1+exp(-I[j]))
}
yn<-t(w2)%*%Iout+b2

e<-output_train[i,]-yn
E[ii]<-E[ii]+sum(abs(e))

dw2<-t(e)%*%Iout
db2<-e

for(j in
1:midnum)
{
S<-1/(1+exp(-I[j]));
FI[j]<-S*(1-S);
}

for(k in
1:innum)
{
for(j in
1:midnum)
{
dw1[k,j]<-FI[j]*x[k]*sum(e*w2[j,])
db1[j]<-FI[j]*sum(e*w2[j,])
}
}

w1<-w1+xite*t(dw1)
b1<-b1+xite*t(db1)
w2<-w2+xite*t(dw2)
b2<-b2+xite*t(db2)
}
}

分类预测

inputn_test<-input_test
for(i in
1:)
{
inputn_test[i,]<-(input_test[i,]-me)/va^0.5
}
fore=matrix(,,);
for(i in
1:)
{
for(j in
1:midnum)
{
I[j]=sum(inputn_test[i,]*w1[j,])+b1[j]
I<-unlist(I)
Iout[j]=1/(1+exp(-I[j]))
}
fore[,i]=t(w2)%*%Iout+b2
}

结果分析

output_fore=rep(,)
for(i in
1:)
{
output_fore[i]<-which.max(fore[,i])
}
error=output_fore-output1[n[121:]]
t<-table(output_fore,output1[n[121:]])
t

##
## output_fore 1 2 3
## 1 13 0 0
## 2 0 9 0
## 3 0 0 8

#正确率
options(digits=)
rightridio<-(t[,]+t[,]+t[,])/
result<-paste("正确率是 ",round(rightridio*,digits=),"%")
result

## [1] "正确率是 100 %"

MATLAB神经网络(1)之R练习的更多相关文章

  1. 12.Matlab神经网络工具箱

    概述: 1 人工神经网络介绍 2 人工神经元 3 MATLAB神经网络工具箱 4 感知器神经网络 5 感知器神经网络 5.1 设计实例分析 clear all; close all; P=[ ; ]; ...

  2. MATLAB神经网络原理与实例精解视频教程

    教程内容:<MATLAB神经网络原理与实例精解>随书附带源程序.rar9.随机神经网络.rar8.反馈神经网络.rar7.自组织竞争神经网络.rar6.径向基函数网络.rar5.BP神经网 ...

  3. 《精通Matlab神经网络》例10-16的新写法

    <精通Matlab神经网络>书中示例10-16,在创建BP网络时,原来的写法是: net = newff(minmax(alphabet),[S1 S2],{'logsig' 'logsi ...

  4. Matlab神经网络

    1. <MATLAB神经网络原理与实例精解> 2. B站:https://search.bilibili.com/all?keyword=matlab&from_source=na ...

  5. 使用opencv-python实现MATLAB的fspecial('Gaussian', [r, c], sigma)

    reference_opencv实现高斯核 reference_MATLAB_fspecial函数说明 # MATLAB H = fspecial('Gaussian', [r, c], sigma) ...

  6. MATLAB神经网络(2)之R练习

    1. AMORE 1.1 newff newff(n.neurons, learning.rate.global, momentum.global, error.criterium, Stao, hi ...

  7. Matlab神经网络工具箱学习之一

    1.神经网络设计的流程 2.神经网络设计四个层次 3.神经网络模型 4.神经网络结构 5.创建神经网络对象 6.配置神经网络的输入输出 7.理解神经网络工具箱的数据结构 8.神经网络训练 1.神经网络 ...

  8. matlab神经网络工具箱创建神经网络

    为了看懂师兄的文章中使用的方法,研究了一下神经网络 昨天花了一天的时间查怎么写程序,但是费了半天劲,不能运行,百度知道里倒是有一个,可以运行的,先贴着做标本 % 生成训练样本集 clear all; ...

  9. Matlab神经网络验证码识别

    本文,将会简述如何利用Matlab的强大功能,调用神经网络处理验证码的识别问题.  预备知识,Matlab基础编程,神经网络基础.  可以先看下: Matlab基础视频教程 Matlab经典教程--从 ...

随机推荐

  1. ML modeling process

    一.数据读取Load Data 二.数据分析EDA 三.数据预处理 四.特征工程Feature engineering 五.modeling & Tuning 六.Result 七.other ...

  2. RabbitMQ传输原理、五种模式

    本文代码基于SpringBoot,文末有代码连接 .首先是一些在Spring Boot的一些配置和概念,然后跟随代码看下五种模式 MQ两种消息传输方式,点对点(代码中的简单传递模式),发布/订阅(代码 ...

  3. python之循删list

    先来看下循环遍历删除list元素的一段代码: L=[1,3,1,4,3,6,5] # 0 1 2 3 4 5 6(下标) for i in L: if i%2!=0:#%表示除商取余数,除以2余数为0 ...

  4. javascript常用知识汇总

    javascript这个语言庞大而复杂,我用了三年多了,还是皮毛都不会.从刚开始的jquery,到后来的es6,每天都在学习,每天都在忘记. 1.禁止手机虚拟键盘弹出 在开发适配手机的页面时,出现了这 ...

  5. freeRadius日志关闭

    vim /etc/raddb/radiusd.conf #file = ${logdir}/radius.log file = /dev/null vim /etc/raddb/modules/det ...

  6. MyBatis学习笔记一:MyBatis最简单的环境搭建

    MyBatis的最简单环境的搭建,使用xml配置,用来理解后面的复杂配置做基础 1.环境目录树(导入mybatis-3.4.1.jar包即可,这里是为后面的环境最准备使用了web项目,如果只是做 my ...

  7. 用R的dplyr进行数据转换(一)

    在网上找了很久关于数据转换的,都没有找到比较好的.现在为大家整理一下.按照我自己的思路.当然也是为了自己做笔记. 为了方便,大家可以统一安装一个系列的包,这个只需要安装tidyverse这个包就可以, ...

  8. Python 搭建webdriver环境遇到的问题总结

    安装过程是参考<selenium2Python自动化测试实战>中Pythonwebdriver环境搭建章节 在安装过程中,遇到了一些问题,总结一下,为日后自己再遇到相同问题做个笔记以便查看 ...

  9. Python---11模块

    在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很 ...

  10. canvas基本

    基本 支持ie 9+,firefox,opera,chrome,safari html: <canvas id="fir_canvas" width="400&qu ...