r - 您如何使用 R 中的神经网络预测值?
问题描述
我试图预测给定时间、区域类型(0,1,2,3)的 AQI 值,是否在工业区(0,1),以及该区域是否有主要道路(0,1) . 我有超过 350,000 对数据,并使用下面的代码训练了神经网络。那么,在给定时间、区域类型、面积和 rd 输入的情况下,如何使用它来预测新的 AQI?还是我不了解神经网络的目的?谢谢!
normalize <- function(x) {
return ((x - min(x)) / (max(x) - min(x)))
}
#(AQI, class, ind, rd, hour)
data = read.csv("neural_data")
data <- subset(data, select=c(2:6))
data = na.omit(data)
## creating training and testing data
train <- data
## Scale the network
train_ <- normalize(train)
## Create the NN
library(neuralnet)
nn <- neuralnet(AQI~hour+rd+ind+class,data=train_,hidden=c(2,1),linear.output=F, threshold = 0.01)
plot(nn)```
解决方案
你有一个多类预测。正如@user2974951 提到的,使用预测。下面我补充说明你必须解释结果。需要注意的是,如果您的预测变量为 0 或 1,则对它们进行归一化不会改变任何内容(请参阅您的函数归一化)。
library(neuralnet)
set.seed(1111)
# training /testing data
trn <- sample(1:nrow(iris),100)
trainData <- iris[trn,]
testData <- iris[-trn,]
# before you fit, check what are the levels of the labels
# in your case should be 0,1,2,3
levels(iris$Species)
1] "setosa" "versicolor" "virginica"
# fit nn
nn <- neuralnet(Species ~ Petal.Length + Petal.Width,trainData , linear.output = FALSE)
# predictions
pred <- predict(nn,testData)
head(pred)
> head(pred)
[,1] [,2] [,3]
3 1 0.10672416 1.855968e-61
5 1 0.10944693 1.214708e-60
8 1 0.11238864 8.835106e-60
9 1 0.10944693 1.214708e-60
从标题 pred 可以看出,这些是概率,第一列是 setosa(物种的第一级)的概率,第二列是 versicolor 等等。每一行都是来自 trainData 的观察
我们可以取回标签,并做一个混淆矩阵
pred_labels <- levels(testData$Species)[apply(pred,1,which.max)]
actual_labels <- testData$Species
table(pred_labels,actual_labels)
actual_labels
pred_labels setosa versicolor virginica
setosa 16 0 0
versicolor 0 18 0
virginica 0 2 14
推荐阅读
- r - 需要安装 Go 才能使用带有 Academic Hugo 主题的 blogdown
- sql - presto - 连接列名称
- reactjs - 创建反应应用程序不工作!events.js:292 抛出错误;// 未处理的“错误”事件
- ckeditor - 如何将 CK Editor 5 与 Sapper/Svelte3 集成?
- apache-spark - org.apache.spark.SparkException:无效的 Spark URL:spark://HeartbeatReceiver@xxxx_LPT-324:51380 PySpark
- elixir - Elixir:减少函数列表的枚举
- java - 如何在 Java 中迭代 List并使用实际对象?
- spring-boot - 将 Spring Boot 连接到多个 Artemis 代理
- javascript - javascript无法对复杂数组进行排序
- c# - XAML - WPF StringFormat dd/MM/yyyy 不能通过相同的 StringFormat 以两种方式工作