Path: blob/master/Part 8 - Deep Learning/Artificial Neural Networks/ann.R
1009 views
# Artificial Neural Network12# Importing the dataset3dataset = read.csv('Churn_Modelling.csv')4dataset = dataset[4:14]56# Encoding the categorical variables as factors7dataset$Geography = as.numeric(factor(dataset$Geography,8levels = c('France', 'Spain', 'Germany'),9labels = c(1, 2, 3)))10dataset$Gender = as.numeric(factor(dataset$Gender,11levels = c('Female', 'Male'),12labels = c(1, 2)))1314# Splitting the dataset into the Training set and Test set15# install.packages('caTools')16library(caTools)17set.seed(123)18split = sample.split(dataset$Exited, SplitRatio = 0.8)19training_set = subset(dataset, split == TRUE)20test_set = subset(dataset, split == FALSE)2122# Feature Scaling23training_set[-11] = scale(training_set[-11])24test_set[-11] = scale(test_set[-11])2526# Fitting ANN to the Training set27# install.packages('h2o')28library(h2o)29h2o.init(nthreads = -1)30model = h2o.deeplearning(y = 'Exited',31training_frame = as.h2o(training_set),32activation = 'Rectifier',33hidden = c(5,5),34epochs = 100,35train_samples_per_iteration = -2)3637# Predicting the Test set results38y_pred = h2o.predict(model, newdata = as.h2o(test_set[-11]))39y_pred = (y_pred > 0.5)40y_pred = as.vector(y_pred)4142# Making the Confusion Matrix43cm = table(test_set[, 11], y_pred)4445# h2o.shutdown()4647