Ce document montre comment entraîner un réseau de neurones simple avec le package neuralnet sur un petit jeu de données synthétique.
library(neuralnet)
set.seed(123)
data <- data.frame(
age = c(25, 45, 39, 50, 23, 31, 40, 60),
education_num = c(10, 12, 13, 9, 7, 15, 14, 11),
hours_per_week = c(40, 50, 60, 20, 25, 55, 45, 30),
income = c(0, 1, 1, 0, 0, 1, 1, 0) # 0 = ???50K, 1 = >50K
)
head(data)
## age education_num hours_per_week income
## 1 25 10 40 0
## 2 45 12 50 1
## 3 39 13 60 1
## 4 50 9 20 0
## 5 23 7 25 0
## 6 31 15 55 1
normalize <- function(x) {
(x - min(x)) / (max(x) - min(x))
}
data_norm <- as.data.frame(lapply(data[ , 1:3], normalize))
data_norm$income <- data$income
head(data_norm)
## age education_num hours_per_week income
## 1 0.05405405 0.375 0.500 0
## 2 0.59459459 0.625 0.750 1
## 3 0.43243243 0.750 1.000 1
## 4 0.72972973 0.250 0.000 0
## 5 0.00000000 0.000 0.125 0
## 6 0.21621622 1.000 0.875 1
nn <- neuralnet(income ~ age + education_num + hours_per_week,
data = data_norm,
hidden = c(3),
linear.output = FALSE)
plot(nn)