问题
I have dataset which consists of credit details of customers. I have to build Neural Network model on this data set using the neuralnet library. This data set contains categorical variables. I need to transform these variables before running it. Can someone help me with this.
Variable in data set :
checking_balance : < 0 DM, 1 - 200 DM, unknown, < 0 DM
purpose: furniture, education, cars
employment_duration: > 7 years, 1 - 4 years, 4 - 7 years
credit_history: very good, critical, good
months_loan_duration: 6, 48, 12, 42
Thank you..
回答1:
To use a categorical variable as input you can encode it as a set of boolean inputs, each representing one category with 0 or 1. For instance, your 'purpose' variable can be transformed into three boolean variables (furniture, education, cars).
You can generate columns with category flags automatically like this:
flags = data.frame(Reduce(cbind,
lapply(levels(d$purpose), function(x){(d$purpose == x)*1})
))
names(flags) = levels(d$purpose)
d = cbind(d, flags)
# Include the new columns as input variables
levelnames = paste(names(flags), collapse = " + ")
neuralnet(paste("output ~ ", levelnames), d)
来源:https://stackoverflow.com/questions/27183827/converting-categorical-variables-in-r-for-ann-neuralnet