Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. # split data in train and test
  2. train <- data3 %>% sample_frac(.85)
  3. test <- data3 %>% anti_join(train, by='id')
  4.  
  5. # count amount of churn and not churn
  6. n_0 <- train %>% filter(churn==0) %>% count() %>% pull(n)
  7. n_1 <- train %>% filter(churn==1) %>% count() %>% pull(n)
  8.  
  9. # remove the id and the target variable
  10. x_train <- train %>% select(-id,-churn) %>% as.matrix()
  11.  
  12. # do the scaling on training data
  13. x_train <- predict(preProcess(x_train,method=c('center','scale')), x_train)
  14.  
  15. # remove the id and the target variable
  16. x_test <- test %>% select(-id,-churn) %>% as.matrix()
  17.  
  18. # do the scaling on test data
  19. x_test <- predict(preProcess(x_test,method=c('center','scale')), x_test)
  20.  
  21. # separate the target variable
  22. y_train <- train$churn
  23. y_test <- test$churn
  24.  
  25. # create the neural network
  26. model <- keras_model_sequential()
  27. model %>%
  28. layer_dense(units = 512, activation = 'relu',
  29. kernel_initializer = keras::initializer_glorot_uniform(7),
  30. input_shape = ncol(x_train)) %>%
  31. layer_activity_regularization(l1=0.01, l2 = 0.01) %>%
  32. layer_dropout(rate = 0.6, seed=7) %>%
  33. layer_dense(units = 128, activation = 'relu',
  34. kernel_initializer = keras::initializer_glorot_uniform(7)) %>%
  35. layer_activity_regularization(l1=0.01, l2 = 0.01) %>%
  36. layer_dropout(rate = 0.5, seed=7) %>%
  37. layer_dense(units = 1, activation = 'sigmoid')
  38.  
  39. # define the loss function and other parameters
  40. model %>%
  41. compile(loss = 'binary_crossentropy',
  42. optimizer = optimizer_adam(lr = 0.0001, beta_1 = 0.9,
  43. beta_2 = 0.999, epsilon = 1e-08, decay = 0.0001),
  44. metrics = list('accuracy'))
  45.  
  46. # define the execution parameters
  47. model %>%
  48. fit(x_train, y_train,
  49. class_weight = list('0'= 1,'1'= n_0/n_1),
  50. epochs = 50, batch_size = 32,
  51. validation_data=list(x_test, y_test))
  52.  
  53. # save the model
  54. model %>% save_model_hdf5("[path]/[model_name].h5")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement