【R语言】神经网络

library(MASS)
library(caret)
install.packages("neuralnet")
library(neuralnet)
library(vcd)

data(shuttle)
head(shuttle)
#   stability error sign wind   magn vis  use
# 1     xstab    LX   pp head  Light  no auto
# 2     xstab    LX   pp head Medium  no auto
# 3     xstab    LX   pp head Strong  no auto
# 4     xstab    LX   pp tail  Light  no auto
# 5     xstab    LX   pp tail Medium  no auto
# 6     xstab    LX   pp tail Strong  no auto
table(shuttle$use)
# auto noauto 
# 145    111 


#分类变量需要处理成哑变量
dummies <- dummyVars(use~.,data = shuttle)
dummies
shuttle.2 <- as.data.frame(predict(dummies,newdata = shuttle))
head(shuttle.2,3)
#   stability.stab stability.xstab error.LX error.MM error.SS error.XL sign.nn sign.pp wind.head wind.tail magn.Light magn.Medium magn.Out magn.Strong vis.no vis.yes
# 1              0               1        1        0        0        0       0       1         1         0          1           0        0           0      1       0
# 2              0               1        1        0        0        0       0       1         1         0          0           1        0           0      1       0
# 3              0               1        1        0        0        0       0       1         1         0          0           0        0           1      1       0
shuttle.2$use <- ifelse(shuttle$use=="auto",1,0)
table(shuttle.2$use)
#   0   1 
# 111 145


#划分数据集
set.seed(123)
trainIndex <- createDataPartition(shuttle.2$use,p=0.7,list = FALSE,times = 1)
shuttleTrain <- shuttle.2[trainIndex,]
table(shuttleTrain$use)
#  0   1 
# 73 107 
shuttleTest <- shuttle.2[-trainIndex,]
table(shuttleTest$use)
#  0  1 
# 38 38

构建神经网络模型

#构建神经网络模型
n <- names(shuttleTrain)
form <- as.formula(paste("use~",paste(n[!n %in% "use"],collapse = "+")))
form
# use ~ stability.stab + stability.xstab + error.LX + error.MM + 
#   error.SS + error.XL + sign.nn + sign.pp + wind.head + wind.tail + 
#   magn.Light + magn.Medium + magn.Out + magn.Strong + vis.no + 
#   vis.yes
fit <- neuralnet(form,data = shuttleTrain,err.fct = "ce",linear.output = FALSE)
fit$result.matrix
#                                      [,1]
# error                         0.034172183
# reached.threshold             0.009881012
# steps                       196.000000000
# Intercept.to.1layhid1         0.927004568
# stability.stab.to.1layhid1    2.843133961
# stability.xstab.to.1layhid1  -5.640394634
# error.LX.to.1layhid1         -2.914143760
# error.MM.to.1layhid1         -0.757744212
# error.SS.to.1layhid1          4.217875578
# error.XL.to.1layhid1         -4.067079927
# sign.nn.to.1layhid1          -1.048810408
# sign.pp.to.1layhid1           1.772158507
# wind.head.to.1layhid1        -0.638870226
# wind.tail.to.1layhid1         1.722314124
# magn.Light.to.1layhid1        0.787091479
# magn.Medium.to.1layhid1       1.015102440
# magn.Out.to.1layhid1         -7.201957019
# magn.Strong.to.1layhid1       0.380298367
# vis.no.to.1layhid1           19.168715515
# vis.yes.to.1layhid1          -5.836542535
# Intercept.to.use             -9.659001340
# 1layhid1.to.use              20.013238671
plot(fit)

在这里插入图片描述

评估模型效果

shuttleTrain$prob <- fit$net.result[[1]]
plot(density(shuttleTrain$prob),main = "Train Predict")

在这里插入图片描述

shuttleTrain$prob <- fit$net.result[[1]]
plot(density(shuttleTrain$prob),main = "Train Predict")
#ROC曲线
library(scorecard)
perf_eva(pred = fit$net.result[[1]],label = shuttleTrain$use)

在这里插入图片描述

res2 <- compute(fit,shuttleTest)
predTest <- res2$net.result

perf_eva(pred = predTest,label = shuttleTest$use)

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值