Post

1 follower Follow
0
Avatar

categorical variable in the train() function using tfestimators package


 

I am a new user of tfestimators, the interface between R and Tensorflow provided by RStudio. I have a problem when I give categorical variables to the train() function.

Here is the code I have tried:

tempdata <- data[,c("HOSP1mois","AGEINDEX","DxP")]
response <- function() "HOSP1mois"
features <- function() setdiff(names(tempdata), response())
set.seed(123)
partitions <- modelr::resample_partition(tempdata, c(test = 0.1, train = 0.9))
Train <- as.data.frame(partitions$train)
Test  <- as.data.frame(partitions$test)
feature_columns <- feature_columns(column_numeric("AGEINDEX"),column_indicator("DxP"))
classifier <- dnn_classifier(feature_columns = feature_columns,hidden_units = c(10, 20, 10),n_classes = 2)
hsp_input_fn <- function(data) { input_fn(data, features = features(), response = response())}
train(classifier, input_fn = hsp_input_fn(Train))

and I got the error message

Error in py_call_impl(callable, dots$args, dots$keywords) : AttributeError: 'str' object has no attribute 'name'

I think the problem comes from column_indicator("DxP") because if I only have numerical feature columns there is no problem. So I also tried column_categorical_with_vocabulary_list("DxP",vocabulary_list = list(...)) but it did not work either. Finally I tried to specify that the DxP variable was a string "Train$DxP <- toString(Train$DxP)" before calling the train() function, and it seems the train() function was running during several minutes, but I still got the error message in the end. 

Any help will be sincerely appreciated!

Qing Li