【问题标题】:Extract feature classes/types from caret train object从插入符号训练对象中提取要素类/类型
【发布时间】: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


    【解决方案1】:

    此信息作为模型条款的属性存储

    attr(terms(model), "dataClasses")
    #      Species Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
    #     "factor"    "numeric"    "numeric"    "numeric"    "numeric" 
    

    【讨论】:

    • 非常感谢,知道它一定在某个地方。
    猜你喜欢
    • 2021-08-11
    • 2021-01-22
    • 2015-12-30
    • 2018-06-16
    • 2015-11-01
    • 1970-01-01
    • 2018-10-28
    • 2018-06-24
    • 2018-05-28
    相关资源
    最近更新 更多