w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Neuralnet package in R big error

It's a scale problem i guess, you can normalize or scale it. There are differences between scaling and normalizing, it will affect your results and worths a separate question on SO:

normalize inputs

norm.fun = function(x){ 
  (x - min(x))/(max(x) - min(x)) 
}

require(ggplot2) # load mpg dataset
require(neuralnet)

data = mpg[, c('cty', 'displ', 'year', 'cyl', 'hwy')]
data.norm = apply(data, 2, norm.fun)

net = neuralnet(cty ~ displ + year + cyl + hwy, data.norm, hidden = 2)

Then you can denormalize the data

# restore data 
y.net = min(data[, 'cty']) + net$net.result[[1]] * range(data[, 'cty'])
plot(data[, 'cty'], col = 'red')
points(y.net)

enter image
description here

scale inputs

data.scaled = scale(data)
net = neuralnet(cty ~ displ + year + cyl + hwy, data.scaled, hidden = 2)

# restore data 
y.sd = sd(data[, 'cty'])
y.mean = mean(data[, 'cty'])

y.net = net$net.result[[1]] * y.sd + y.mean
plot(data[, 'cty'], col = 'red')
points(y.net)

enter image
description here

You can also try the nnet package, it's very fast:

require(nnet)

data2 = mpg
data2$year = scale(data2$year)
fit = nnet(cty ~ displ + year + cyl + hwy, size = 10, data = data2, linout
= TRUE)
plot(mpg$cty)
points(fit$fitted.values, col = 'red')




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.