BP神经网络  百度百科:传送门

  BP(back propagation)神经网络:一种按照误差逆向传播算法训练的多层前馈神经网络,是目前应用最广泛的神经网络

  

#设置文件工作区间
setwd('D:\\dat')
#读入数据
Gary=read.csv("sales_data.csv")[,2:5]
#数据命名
library(nnet)
colnames(Gary)<-c("x1","x2","x3","y") ###最终模型
model1=nnet(y~.,data=Gary,size=6,decay=5e-4,maxit=1000) pred=predict(model1,Gary[,1:3],type="class")
(P=sum(as.numeric(pred==Gary$y))/nrow(Gary))
table(Gary$y,pred)
prop.table(table(Gary$y,pred),1)

Gary.Script

实现过程

  目的:通过BP神经网络预测销量的高低

  数据预处理,对数据进行重命名并去除无关项

> #设置文件工作区间
> setwd('D:\\dat')
> #读入数据
> Gary=read.csv("sales_data.csv")[,2:5]
> #数据命名
> library(nnet)
> colnames(Gary)<-c("x1","x2","x3","y")
> Gary
x1 x2 x3 y
1 bad yes yes high
2 bad yes yes high
3 bad yes yes high
4 bad no yes high
5 bad yes yes high
6 bad no yes high
7 bad yes no high
8 good yes yes high
9 good yes no high
10 good yes yes high
11 good yes yes high
12 good yes yes high
13 good yes yes high
14 bad yes yes low
15 good no yes high
16 good no yes high
17 good no yes high
18 good no yes high
19 good no no high
20 bad no no low
21 bad no yes low
22 bad no yes low
23 bad no yes low
24 bad no no low
25 bad yes no low
26 good no yes low
27 good no yes low
28 bad no no low
29 bad no no low
30 good no no low
31 bad yes no low
32 good no yes low
33 good no no low
34 good no no low

  nnet:包实现了前馈神经网络和多项对数线性模型。前馈神经网络是一种常用的神经网络结构,如下图所示

  

  前馈网络中各个神经元按接受信息的先后分为不同的组。每一组可以看作一个神经层。每一层中的神经元接受前一层神经元的输出,并输出到下一层神经元。整个网络中的信息是朝一个方向传播,没有反向的信息传播。前馈网络可以用一个有向无环路图表示。前馈网络可以看作一个函数,通过简单非线性函数的多次复合,实现输入空间到输出空间的复杂映射。这种网络结构简单,易于实现。前馈网络包括全连接前馈网络和卷积神经网络等

使用neet()方法创建模型

  neet()方法:

  decay: 权重衰减parameter for weight decay. Default 0.

  maxit: 最大迭代次数

x: 训练样本数据集的输入集合
y: x对应的训练样本数据集的标签(类)集合
weights:
size: 隐层节点数, Can be zero if there are skip-layer units.
data:训练数据集.
subset: An index vector specifying the cases to be used in the training sample. (NOTE: If given, this argument must be named.)
na.action: A function to specify the action to be taken if NAs are found. The default action is for the procedure to fail. An alternative is na.omit, which leads to rejection of cases with missing values on any required variable. (NOTE: If given, this argument must be named.)
contrasts:a list of contrasts to be used for some or all of the factors appearing as variables in the model formula.
Wts: 边的权重. If missing chosen at random.
mask: logical vector indicating which parameters should be optimized (default all).
linout: 是否为逻辑输出单元,若为FALSE,则为线性输出单元
entropy: switch for entropy (= maximum conditional likelihood) fitting. Default by least-squares.
softmax: switch for softmax (log-linear model) and maximum conditional likelihood fitting. linout, entropy, softmax and censored are mutually exclusive.
censored: A variant on softmax, in which non-zero targets mean possible classes. Thus for softmax a row of (0, 1, 1) means one example each of classes 2 and 3, but for censored it means one example whose class is only known to be 2 or 3.
skip: switch to add skip-layer connections from input to output.
rang: Initial random weights on [-rang, rang]. Value about 0.5 unless the inputs are large, in which case it should be chosen so that rang * max(|x|) is about 1.
decay: 权重衰减parameter for weight decay. Default 0.
maxit: 最大迭代次数
Hess: If true, the Hessian of the measure of fit at the best set of weights found is returned as component Hessian.
trace: switch for tracing optimization. Default TRUE.
MaxNWts: The maximum allowable number of weights. There is no intrinsic limit in the code, but increasing MaxNWts will probably allow fits that are very slow and time-consuming.
abstol: Stop if the fit criterion falls below abstol, indicating an essentially perfect fit.
reltol: Stop if the optimizer is unable to reduce the fit criterion by a factor of at least 1 - reltol.

neet()方法

model1=nnet(y~.,data=Gary,size=6,decay=5e-4,maxit=1000)
# weights: 31
initial value 27.073547
iter 10 value 16.080731
iter 20 value 15.038060
iter 30 value 14.937127
iter 40 value 14.917485
iter 50 value 14.911531
iter 60 value 14.908678
iter 70 value 14.907836
iter 80 value 14.905234
iter 90 value 14.904499
iter 100 value 14.904028
iter 110 value 14.903688
iter 120 value 14.903480
iter 130 value 14.903450
iter 130 value 14.903450
iter 130 value 14.903450
final value 14.903450
converged

评估模型

> pred=predict(model1,Gary[,1:3],type="class")
> (P=sum(as.numeric(pred==Gary$y))/nrow(Gary))
[1] 0.7647059
> table(Gary$y,pred)
pred
high low
high 14 4
low 4 12
> prop.table(table(Gary$y,pred),1)
pred
high low
high 0.7777778 0.2222222
low 0.2500000 0.7500000

  得到混淆矩阵图后可以看出

  检测样本为34个,预测正确的个数为26个,预测准确率为76.5%,预测准确率较低

  原因:由于神经网络训练时需要较多的样本,而这里训练集比较少 

R_Studio(神经网络)BP神经网络算法预测销量的高低的更多相关文章

  1. BP神经网络算法预测销量高低

    理论以前写过:https://www.cnblogs.com/fangxiaoqi/p/11306545.html,这里根据天气.是否周末.有无促销的情况,来预测销量情况. function [ ma ...

  2. 秒懂神经网络---BP神经网络具体应用不能说的秘密.

    秒懂神经网络---BP神经网络具体应用不能说的秘密 一.总结 一句话总结: 还是要上课和自己找书找博客学习相结合,这样学习效果才好,不能单视频,也不能单书 BP神经网络就是反向传播神经网络 1.BP神 ...

  3. BP神经网络与Python实现

    人工神经网络是一种经典的机器学习模型,随着深度学习的发展神经网络模型日益完善. 联想大家熟悉的回归问题, 神经网络模型实际上是根据训练样本创造出一个多维输入多维输出的函数, 并使用该函数进行预测, 网 ...

  4. BP神经网络学习

    人工神经元模型     S型函数(Sigmoid) 双极S型函数 神经网络可以分为哪些? 按照连接方式,可以分为:前向神经网络 vs. 反馈(递归)神经网络 按照学习方式,可以分为:有导师学习神经网络 ...

  5. Python语言编写BP神经网络

    Python语言编写BP神经网络 2016年10月31日 16:42:44 ldy944758217 阅读数 3135   人工神经网络是一种经典的机器学习模型,随着深度学习的发展神经网络模型日益完善 ...

  6. 详细BP神经网络预测算法及实现过程实例

    1.具体应用实例.根据表2,预测序号15的跳高成绩. 表2 国内男子跳高运动员各项素质指标 序号 跳高成绩() 30行进跑(s) 立定三级跳远() 助跑摸高() 助跑4—6步跳高() 负重深蹲杠铃() ...

  7. 基于steam的游戏销量预测 — PART 3 — 基于BP神经网络的机器学习与预测

    语言:c++ 环境:windows 训练内容:根据从steam中爬取的数据经过文本分析制作的向量以及标签 使用相关:无 解释: 就是一个BP神经网络,借鉴参考了一些博客的解释和代码,具体哪些忘了,给出 ...

  8. 数据挖掘系列(9)——BP神经网络算法与实践

    神经网络曾经很火,有过一段低迷期,现在因为深度学习的原因继续火起来了.神经网络有很多种:前向传输网络.反向传输网络.递归神经网络.卷积神经网络等.本文介绍基本的反向传输神经网络(Backpropaga ...

  9. bp神经网络算法

    对于BP神经网络算法,由于之前一直没有应用到项目中,今日偶然之时 进行了学习, 这个算法的基本思路是这样的:不断地迭代优化网络权值,使得输入与输出之间的映射关系与所期望的映射关系一致,利用梯度下降的方 ...

随机推荐

  1. mysql以及mysql bench安装教程

    首先,我们需要去官网下载mysql(这里以下载) 1 2 3 4 5 下载好了自己好了之后,点击安装好的东西出现如下界面: 1.接受使用条款并点击next 2.点击custom,可以根据个人习惯进行安 ...

  2. [BJOI2014]大融合(Link Cut Tree)

    [BJOI2014]大融合(Link Cut Tree) 题面 给出一棵树,动态加边,动态查询通过每条边的简单路径数量. 分析 通过每条边的简单路径数量显然等于边两侧节点x,y子树大小的乘积. 我们知 ...

  3. Linux 测试IP和端口是否能访问

    一. 使用wget判断 wget是linux下的下载工具,需要先安装. 用法: wget ip:port 连接存在的端口 转自:https://blog.csdn.net/weixin_3768923 ...

  4. 树莓派USB存储设备自动挂载并通过脚本实现自动拷贝,自动播放视频,脚本自动升级等功能

    需求:首先需要树莓派自动挂载USB设备,然后扫描USB指定目录下文件,将相关文件拷贝至树莓派指定目录,然后通过omxplayer循环播放新拷贝文件视频 1. 树莓派实现USB存储设备自动挂载 树莓派U ...

  5. 链接校验——是否是协议http://或https://开头的

    if(str.substr(0,7)!="http://" && str.substr(0,7)!="https://"){ return 'y ...

  6. LintCode 29---交叉字符串

    public class Solution { /** * @param s1: A string * @param s2: A string * @param s3: A string * @ret ...

  7. 帝国cms 反馈

    <form name='feedback' method='post' enctype='multipart/form-data' action='/e/enews/index.php' ons ...

  8. 帝国cms 常用标签汇总

    1.列表内容标签 [!--empirenews.listtemp--]<!--list.var1-->[!--empirenews.listtemp--] 2.分页标签 [!--show. ...

  9. CentOS7 ab压力测试安装

    ①.ab(apache benchmark)安装 命令: yum -y install httpd-tools ②.ab测试的命令参数 命令: ab 或 ab -help 显示命令参数如下 ③.ab的 ...

  10. python-嵌套函数

    python-嵌套函数 定义:在函数体内用def定义一个函数,它的作用域只在该函数体内有效. def outside(): print("int the outside") def ...