Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. # Create vector of random data between 0 and 10, lenght 50
  2. x <- sort(10*runif(50))
  3.  
  4. # We generate data from our fonction (that we need to approximate)
  5. y <- 2*cos(x)+4
  6.  
  7. # We generate vector of 100 valors from 0 to 10 that we will use to draw the basic function and the one approximate with neural network (multilayer perceptron)
  8. x1 <- seq(0, 10, by=0.1)
  9.  
  10. # Basic function (to draw the curve of the basic function that we need to approximate)
  11. f <- 2*cos(x1)+4
  12.  
  13. # Charging library nnet (to apply neural network to our model)
  14. library(nnet)
  15.  
  16. # We apply neural network to our generated data (random points of the function we want to approximate)
  17. # We use one hidden layer, 6 neurons and 40 iteration
  18. nn <- nnet(x, y, size=6, maxit=40, linout=TRUE)
  19.  
  20. # We display the 50 random generated points of our function
  21. plot(x, y)
  22.  
  23. # Original function curve (2*cos+4)
  24. lines(x1, f)
  25.  
  26. # Approximate function curve, generated by prediction on the results of the neural network
  27. lines(x1, predict(nn, data.frame(x=x1)), col="green")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement