Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Create vector of random data between 0 and 10, lenght 50
- x <- sort(10*runif(50))
- # We generate data from our fonction (that we need to approximate)
- y <- 2*cos(x)+4
- # 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)
- x1 <- seq(0, 10, by=0.1)
- # Basic function (to draw the curve of the basic function that we need to approximate)
- f <- 2*cos(x1)+4
- # Charging library nnet (to apply neural network to our model)
- library(nnet)
- # We apply neural network to our generated data (random points of the function we want to approximate)
- # We use one hidden layer, 6 neurons and 40 iteration
- nn <- nnet(x, y, size=6, maxit=40, linout=TRUE)
- # We display the 50 random generated points of our function
- plot(x, y)
- # Original function curve (2*cos+4)
- lines(x1, f)
- # Approximate function curve, generated by prediction on the results of the neural network
- lines(x1, predict(nn, data.frame(x=x1)), col="green")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement