-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNet2.R
More file actions
66 lines (48 loc) · 1.61 KB
/
Copy pathNeuralNet2.R
File metadata and controls
66 lines (48 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
library(tidymodels)
library(tidyverse)
library(vroom)
library(themis)
library(recipes)
library(discrim)
library(klaR)
test<-vroom("test.csv")
train<-vroom("train.csv")
sample<-vroom("sample_submission.csv")
glimpse(train)
# Create recipe, change nominal predictors to be dummy variables
recipe <- recipe(type ~ ., data = train) %>%
update_role(id, new_role="id") %>%
step_dummy(all_nominal(), -all_outcomes()) %>%
step_dummy(all_nominal_predictors()) %>%
step_normalize(all_predictors())
# check to make sure it looks reasonable
recipe
# prep and bake
prepped_recipe <- prep(recipe, training = train)
baked_test <- bake(prepped_recipe, new_data = test)
#Neural Network model
nn_model <- mlp(hidden_units = tune(),
penalty = tune(),
epochs = 50) %>%
set_engine("nnet", ) %>% #verbose = 0 prints off less
set_mode("classification")
nn_workflow <- workflow() %>%
add_model(nn_model) %>%
add_recipe(recipe)
s
CV_results %>% collect_metrics() %>%
filter(.metric=="accuracy") %>%
ggplot(aes(x=hidden_units, y=mean)) + geom_line()
final_wf <-nn_workflow %>%
finalize_workflow(best_parameters) %>%
fit(data = train)
## Look at the fitted LM model this way
extract_fit_engine(final_wf) %>%
summary()
## Get Predictions for test set AND format for Kaggle
test_preds <- predict(final_wf, new_data = test) %>%
bind_cols(., test) %>%
rename(type=.pred_class) %>% #rename pred to count (for submission to Kaggle) as well as undo the log
dplyr::select(id, type)
glimpse(test_preds)
vroom_write(x=test_preds, file="TestPredsNN.csv", delim=",")