Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 8 - Deep Learning/Artificial Neural Networks/ann.R
1009 views
1
# Artificial Neural Network
2
3
# Importing the dataset
4
dataset = read.csv('Churn_Modelling.csv')
5
dataset = dataset[4:14]
6
7
# Encoding the categorical variables as factors
8
dataset$Geography = as.numeric(factor(dataset$Geography,
9
levels = c('France', 'Spain', 'Germany'),
10
labels = c(1, 2, 3)))
11
dataset$Gender = as.numeric(factor(dataset$Gender,
12
levels = c('Female', 'Male'),
13
labels = c(1, 2)))
14
15
# Splitting the dataset into the Training set and Test set
16
# install.packages('caTools')
17
library(caTools)
18
set.seed(123)
19
split = sample.split(dataset$Exited, SplitRatio = 0.8)
20
training_set = subset(dataset, split == TRUE)
21
test_set = subset(dataset, split == FALSE)
22
23
# Feature Scaling
24
training_set[-11] = scale(training_set[-11])
25
test_set[-11] = scale(test_set[-11])
26
27
# Fitting ANN to the Training set
28
# install.packages('h2o')
29
library(h2o)
30
h2o.init(nthreads = -1)
31
model = h2o.deeplearning(y = 'Exited',
32
training_frame = as.h2o(training_set),
33
activation = 'Rectifier',
34
hidden = c(5,5),
35
epochs = 100,
36
train_samples_per_iteration = -2)
37
38
# Predicting the Test set results
39
y_pred = h2o.predict(model, newdata = as.h2o(test_set[-11]))
40
y_pred = (y_pred > 0.5)
41
y_pred = as.vector(y_pred)
42
43
# Making the Confusion Matrix
44
cm = table(test_set[, 11], y_pred)
45
46
# h2o.shutdown()
47