Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > # read the datafile
- > toyota.df <- read.csv("ToyotaCorolla.csv")
- >
- > # set variable names
- > vars = c("Age_08_04", "KM", "Fuel_Type", "Automatic", "Powered_Windows")
- >
- > # normalize data
- > toyota.df.norm <- preProcess(toyota.df[, c(vars, "Price")], method=c("range"))
- > toyota.df[, c(vars, "Price")] <- predict(toyota.df.norm, toyota.df[, c(vars, "Price")])
- >
- > toyota.df.norm <- preProcess(toyota.df[, c(vars, "Age_08_04")], method=c("range"))
- > toyota.df.norm <- preProcess(toyota.df[, c(vars, "KM")], method=c("range"))
- >
- > # partition the data 60% training, 40% validating
- > set.seed(2)
- > training = sample(row.names(toyota.df), dim(toyota.df)[1]*0.6)
- > validation = setdiff(row.names(toyota.df), training)
- >
- > # when y has multiple classes - need to dummify
- > trainData <- cbind(toyota.df[training,c(vars)],
- + class.ind(toyota.df[training,]$Fuel_Type),
- + class.ind(toyota.df[training,]$Automatic),
- + class.ind(toyota.df[training,]$Powered_Windows),
- + toyota.df[training,]$Price)
- > names(trainData) <- c(vars, paste("Fuel_Type_", c("CNG", "Diesel", "Petrol"), sep = ""),
- + paste("Automatic_", c("No", "Yes"), sep = ""),
- + paste("PowerWindow_", c("No", "Yes"), sep = ""),
- + "Price")
- >
- > validData <- cbind(toyota.df[validation,c(vars)],
- + class.ind(toyota.df[validation,]$Fuel_Type),
- + class.ind(toyota.df[validation,]$Automatic),
- + class.ind(toyota.df[validation,]$Powered_Windows),
- + toyota.df[validation,]$Price)
- > names(validData) <- c(vars, paste("Fuel_Type_", c("CNG", "Diesel", "Petrol"), sep = ""),
- + paste("Automatic_", c("No", "Yes"), sep = ""),
- + paste("PowerWindow_", c("No", "Yes"), sep = ""),
- + "Price")
- >
- > # run nn with 2 hidden nodes
- > # use hidden= with a vector of integers specifying number of hidden nodes in each layer
- > nn <- neuralnet(Price ~ Age_08_04 + KM + Fuel_Type_CNG + Fuel_Type_Diesel + Fuel_Type_Petrol
- + + Automatic_No + Automatic_Yes + PowerWindow_No + PowerWindow_Yes,
- + data = trainData, hidden = 2)
- >
- > # draw nn diagram
- > plot(nn, rep="best")
- > # predict the values
- > training.prediction=neuralnet::compute(nn, trainData[,-c(10)])
- Error in neurons[[i]] %*% weights[[i]] :
- requires numeric/complex matrix/vector arguments
- > validation.prediction=neuralnet::compute(nn, validData[,-c(10)])
- Error in neurons[[i]] %*% weights[[i]] :
- requires numeric/complex matrix/vector arguments
Add Comment
Please, Sign In to add comment