R_Studio(神经网络)BP神经网络算法预测销量的高低
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神经网络算法预测销量的高低的更多相关文章
- BP神经网络算法预测销量高低
理论以前写过:https://www.cnblogs.com/fangxiaoqi/p/11306545.html,这里根据天气.是否周末.有无促销的情况,来预测销量情况. function [ ma ...
- 秒懂神经网络---BP神经网络具体应用不能说的秘密.
秒懂神经网络---BP神经网络具体应用不能说的秘密 一.总结 一句话总结: 还是要上课和自己找书找博客学习相结合,这样学习效果才好,不能单视频,也不能单书 BP神经网络就是反向传播神经网络 1.BP神 ...
- BP神经网络与Python实现
人工神经网络是一种经典的机器学习模型,随着深度学习的发展神经网络模型日益完善. 联想大家熟悉的回归问题, 神经网络模型实际上是根据训练样本创造出一个多维输入多维输出的函数, 并使用该函数进行预测, 网 ...
- BP神经网络学习
人工神经元模型 S型函数(Sigmoid) 双极S型函数 神经网络可以分为哪些? 按照连接方式,可以分为:前向神经网络 vs. 反馈(递归)神经网络 按照学习方式,可以分为:有导师学习神经网络 ...
- Python语言编写BP神经网络
Python语言编写BP神经网络 2016年10月31日 16:42:44 ldy944758217 阅读数 3135 人工神经网络是一种经典的机器学习模型,随着深度学习的发展神经网络模型日益完善 ...
- 详细BP神经网络预测算法及实现过程实例
1.具体应用实例.根据表2,预测序号15的跳高成绩. 表2 国内男子跳高运动员各项素质指标 序号 跳高成绩() 30行进跑(s) 立定三级跳远() 助跑摸高() 助跑4—6步跳高() 负重深蹲杠铃() ...
- 基于steam的游戏销量预测 — PART 3 — 基于BP神经网络的机器学习与预测
语言:c++ 环境:windows 训练内容:根据从steam中爬取的数据经过文本分析制作的向量以及标签 使用相关:无 解释: 就是一个BP神经网络,借鉴参考了一些博客的解释和代码,具体哪些忘了,给出 ...
- 数据挖掘系列(9)——BP神经网络算法与实践
神经网络曾经很火,有过一段低迷期,现在因为深度学习的原因继续火起来了.神经网络有很多种:前向传输网络.反向传输网络.递归神经网络.卷积神经网络等.本文介绍基本的反向传输神经网络(Backpropaga ...
- bp神经网络算法
对于BP神经网络算法,由于之前一直没有应用到项目中,今日偶然之时 进行了学习, 这个算法的基本思路是这样的:不断地迭代优化网络权值,使得输入与输出之间的映射关系与所期望的映射关系一致,利用梯度下降的方 ...
随机推荐
- vscode 插件 配置
第一页 第二页 第三页 settings.json配置 { "editor.fontSize": 20, "files.autoSave": "off ...
- Bicolored RBS CodeForces - 1167D (括号)
建树, 然后高度最大值的最小值显然为$\lceil \frac{dep}{2}\rceil$, 将$>\frac{dep}{2}$的全部分出去即可. #include <sstream&g ...
- idea自定义注释
类配置位置: 方法配置位置 配置内容 * * @Author *** * @Date $date$ $time$ $param$ * @return $return$ * @Description * ...
- 关于tomcat部署项目的问题
问题是这样的 之前用tomcat8.5部署的项目,结果启动项目一直三个端口被占用,浏览器也打不开目标网页 卸了8,装了9.先配置的一大堆,结果可以打开Tomcat的主页locahost:8080,到此 ...
- O010、动手实践虚拟网络
参考https://www.cnblogs.com/CloudMan6/p/5296573.html 本节将演示如何在实验环境中实现下图所示的虚拟网络
- python 练习合集一
一.运算符与流程控制 1.输入两个整数,打印较大的那个值2.输入三个整数,按照从小到大的顺序打印3.输入一个三位数,打印其个位.十位.百位上的数4.输入一个年份,判断是否为闰年,是打印一句话,不是打印 ...
- VMware虚拟化集群的配置(一)
一.VMware介绍 VMware vSphere 是业界领先且最可靠的虚拟化平台.vSphere将应用程序和操作系统从底层硬件分离出来,从而简化了 IT操作. VMware集群最主要的两个部分ESX ...
- 2019.10.28sql注入工具
SQLMAP工具的使用 sql注入工具:明小子 啊D 萝卜头 穿山甲 sqlmap等等 开源自动化注入利用工具,支持12中数据库,在/plugins中可以看到支持的数据库种类 支持的注入类型:bool ...
- tornada-模板
tornado模板 1.配置模板路径 (project/config.py) # coding=utf-8 import os BASE_DIRS = os.path.dirname(__file__ ...
- 团队作业-Beta冲刺(周五)
一. 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/ 这个作业要求在哪里 https://edu.c ...