# Neural Network Example: Forward Propagation for Simple Network w2 = c(0.171, -0.0772) b2 = c(0.732) w1 = c(-0.265, -0.420) b1 = c(-0.475, -0.140) logsig = function(x) { return(1 / (1 + exp(-x))) } purelin = function(x) { return(x) } a0 = 1 a1 = logsig(w1 * a0 + b1) a2 = purelin(t(w2) %*% a1 + b2) a2 # Neural Network Example: Sonar Classification set.seed(2^17 - 1) library(mxnet) require(mlbench) data(Sonar, package = "mlbench") Sonar[,61] = as.numeric(Sonar[,61]) - 1 n = nrow(Sonar) index = sample(1:n) m = round(0.8 * n) train.x = data.matrix(Sonar[index[1:m], 1:60]) train.y = Sonar[index[1:m], 61] test.x = data.matrix(Sonar[index[(m+1):n], 1:60]) test.y = Sonar[index[(m+1):n], 61] model = mx.mlp(train.x, train.y, hidden_node = 10, out_node = 2, out_activation = "softmax", learning.rate = 0.1, num.round = 1000) predictions = predict(model, test.x) table(test.y, predictions[2,] > 0.5) graph.viz(model$symbol) # Genetic Algorithm for Feature Selection Example set.seed(2^17 - 1) library(mlbench) library(caret) n = 100 sigma = 1 set.seed(1) sim = mlbench.friedman1(n, sd = sigma) colnames(sim$x) <- c(paste("real", 1:5, sep = ""), paste("bogus", 1:5, sep = "")) x = sim$x y = sim$y normalization = preProcess(x) x = predict(normalization, x) x = as.data.frame(x) gafsControl = gafsControl(functions = rfGA, method = "cv", number = 5) set.seed(2^17-1) start = Sys.time() model = gafs(x = x, y = y, iters = 5, gafsControl = gafsControl) stop = Sys.time() model # Naive Bayes Example set.seed(2^17-1) library(e1071) data(HouseVotes84, package = "mlbench") n = nrow(HouseVotes84) m = round(0.8 * nrow(HouseVotes84)) shuffle = sample(1:n) trn = HouseVotes84[shuffle[1:m], c(1,4,5)] tst = HouseVotes84[shuffle[(m+1):n], c(1,4,5)] model = naiveBayes(Class ~ ., data = trn) model names(model) model$apriori model$tables prior = table(trn$Class) / sum(table(trn$Class)) prior table(trn$V3[trn$Class == "democrat"]) / sum(table(trn$V3[trn$Class == "democrat"])) table(trn$V3[trn$Class == "republican"]) / sum(table(trn$V3[trn$Class == "republican"])) table(trn$V4[trn$Class == "democrat"]) / sum(table(trn$V4[trn$Class == "democrat"])) table(trn$V4[trn$Class == "republican"]) / sum(table(trn$V4[trn$Class == "republican"])) predict(model, tst[c(1,2,4,15),]) predict(model, tst[c(1,2,4,15),], type = "raw") observation = tst[1,] ( prior["democrat"] * model$tables$V3["democrat",observation$V3] * model$tables$V4["democrat",observation$V4] ) / ( prior["democrat"] * model$tables$V3["democrat",observation$V3] * model$tables$V4["democrat",observation$V4] + prior["republican"] * model$tables$V3["republican",observation$V3] * model$tables$V4["republican",observation$V4] ) ( prior["republican"] * model$tables$V3["republican",observation$V3] * model$tables$V4["republican",observation$V4] ) / ( prior["democrat"] * model$tables$V3["democrat",observation$V3] * model$tables$V4["democrat",observation$V4] + prior["republican"] * model$tables$V3["republican",observation$V3] * model$tables$V4["republican",observation$V4] )