Guest User

Untitled

a guest
Dec 14th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. > # read the datafile
  2. > toyota.df <- read.csv("ToyotaCorolla.csv")
  3. >
  4. > # set variable names
  5. > vars = c("Age_08_04", "KM", "Fuel_Type", "Automatic", "Powered_Windows")
  6. >
  7. > # normalize data
  8. > toyota.df.norm <- preProcess(toyota.df[, c(vars, "Price")], method=c("range"))
  9. > toyota.df[, c(vars, "Price")] <- predict(toyota.df.norm, toyota.df[, c(vars, "Price")])
  10. >
  11. > toyota.df.norm <- preProcess(toyota.df[, c(vars, "Age_08_04")], method=c("range"))
  12. > toyota.df.norm <- preProcess(toyota.df[, c(vars, "KM")], method=c("range"))
  13. >
  14. > # partition the data 60% training, 40% validating
  15. > set.seed(2)
  16. > training = sample(row.names(toyota.df), dim(toyota.df)[1]*0.6)
  17. > validation = setdiff(row.names(toyota.df), training)
  18. >
  19. > # when y has multiple classes - need to dummify
  20. > trainData <- cbind(toyota.df[training,c(vars)],
  21. + class.ind(toyota.df[training,]$Fuel_Type),
  22. + class.ind(toyota.df[training,]$Automatic),
  23. + class.ind(toyota.df[training,]$Powered_Windows),
  24. + toyota.df[training,]$Price)
  25. > names(trainData) <- c(vars, paste("Fuel_Type_", c("CNG", "Diesel", "Petrol"), sep = ""),
  26. + paste("Automatic_", c("No", "Yes"), sep = ""),
  27. + paste("PowerWindow_", c("No", "Yes"), sep = ""),
  28. + "Price")
  29. >
  30. > validData <- cbind(toyota.df[validation,c(vars)],
  31. + class.ind(toyota.df[validation,]$Fuel_Type),
  32. + class.ind(toyota.df[validation,]$Automatic),
  33. + class.ind(toyota.df[validation,]$Powered_Windows),
  34. + toyota.df[validation,]$Price)
  35. > names(validData) <- c(vars, paste("Fuel_Type_", c("CNG", "Diesel", "Petrol"), sep = ""),
  36. + paste("Automatic_", c("No", "Yes"), sep = ""),
  37. + paste("PowerWindow_", c("No", "Yes"), sep = ""),
  38. + "Price")
  39. >
  40. > # run nn with 2 hidden nodes
  41. > # use hidden= with a vector of integers specifying number of hidden nodes in each layer
  42. > nn <- neuralnet(Price ~ Age_08_04 + KM + Fuel_Type_CNG + Fuel_Type_Diesel + Fuel_Type_Petrol
  43. + + Automatic_No + Automatic_Yes + PowerWindow_No + PowerWindow_Yes,
  44. + data = trainData, hidden = 2)
  45. >
  46. > # draw nn diagram
  47. > plot(nn, rep="best")
  48.  
  49. > # predict the values
  50. > training.prediction=neuralnet::compute(nn, trainData[,-c(10)])
  51. Error in neurons[[i]] %*% weights[[i]] :
  52. requires numeric/complex matrix/vector arguments
  53. > validation.prediction=neuralnet::compute(nn, validData[,-c(10)])
  54. Error in neurons[[i]] %*% weights[[i]] :
  55. requires numeric/complex matrix/vector arguments
Add Comment
Please, Sign In to add comment