【发布时间】:2018-02-06 03:12:33
【问题描述】:
使用内置的 iris 数据集,我可以像这样训练模型:
model <- train(Species~., data=iris, method='xgbTree')
我可以提取特征的名称,但是当我尝试获取它们的类时,它会返回字符,因为它们只是字符串。
model$coefnames
## "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
lapply(model$coefnames, class)
## "character" "character" "character" "character"
但是,当您尝试放入另一种类型的变量进行预测时,似乎插入符号知道预期的类型。
test<- data.frame(Sepal.Length=1,
Sepal.Width=2,
Petal.Length=3,
Petal.Width="x") # character instead of numeric
predict(model, newdata=test)
## Error: variable 'Petal.Width' was fitted with type "numeric" but type "factor" was supplied
有什么方法可以通过仅使用模型对象本身来提取用于训练模型的特征类型?我能得到的最接近的是使用dplyr 函数type.convert 但这需要知道输入将是。我理想的功能应该是这样的:
model_types(model$coefnames)
## "numeric" "numeric" "numeric" "numeric"
【问题讨论】:
标签: r machine-learning r-caret feature-extraction