# NOT RUN {
library("magrittr")
# use the iris dataset as an example
set.seed(8818)
# balanced sample 80% for training
train_idxs <- unlist(by(seq_len(nrow(iris)), iris$Species, function(x) {
sample(x, length(x) * .8)
}))
train_data <- iris[train_idxs, ]
test_data <- iris[-train_idxs, ]
colnames(iris)
# Sepal.Length will be the interest column to predict
train_file <- paste0(tempdir(), "/iris_train.csv")
write.csv(train_data, train_file, row.names = FALSE)
# file to predict, cant have the response "Sepal.Length" column
test_file_to_predict <- paste0(tempdir(), "/iris_test_2_pred.csv")
write.csv(test_data[, -1], test_file_to_predict, row.names = FALSE)
test_file_to_eval <- paste0(tempdir(), "/iris_test_2_eval.csv")
write.csv(test_data, test_file_to_eval, row.names = FALSE)
library("autokeras")
# Initialize the structured data regressor
reg <- model_structured_data_regressor(max_trials = 10) %>% # It tries 10 different models
fit(train_file, "Sepal.Length") # Feed the structured data regressor with training data
# If you want to use own valitadion data do:
reg <- model_structured_data_regressor(max_trials = 10) %>%
fit(
train_file,
"Sepal.Length",
validation_data = list(test_file_to_eval, "Sepal.Length")
)
# Predict with the best model
(predicted_y <- reg %>% predict(test_file_to_predict))
# Evaluate the best model with testing data
reg %>% evaluate(test_file_to_eval, "Sepal.Length")
# Get the best trained Keras model, to work with the keras R library
export_model(reg)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab