簡體   English   中英

您如何使用 R 中的神經網絡預測值?

[英]How do you predict values with neural network in 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,則對它們進行標准化不會改變任何事情(請參閱您的 function 標准化)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM