[英]How do I extract feature_importances from my model in SparklyR?
I would like to extract feature_importances
from my model in SparklyR.我想从 SparklyR 中的 model 中提取
feature_importances
。 So far I have the following reproducible code that is working:到目前为止,我有以下正在运行的可重现代码:
library(sparklyr)
library(dplyr)
sc <- spark_connect(method = "databricks")
dtrain <- data_frame(text = c("Chinese Beijing Chinese",
"Chinese Chinese Shanghai",
"Chinese Macao",
"Tokyo Japan Chinese"),
doc_id = 1:4,
class = c(1, 1, 1, 0))
dtrain_spark <- copy_to(sc, dtrain, overwrite = TRUE)
pipeline <- ml_pipeline(
ft_tokenizer(sc, input_col = "text", output_col = "tokens"),
ft_count_vectorizer(sc, input_col = 'tokens', output_col = 'myvocab'),
ml_decision_tree_classifier(sc, label_col = "class",
features_col = "myvocab",
prediction_col = "pcol",
probability_col = "prcol",
raw_prediction_col = "rpcol")
)
model <- ml_fit(pipeline, dtrain_spark)
When I try to run the ml_stage
step below, I find that I cannot extract a vector of feature_importances
, but rather it is a function. A prior post ( how to extract the feature importances in Sparklyr? ) displays it as a vector which I would like to obtain.当我尝试运行下面的
ml_stage
步骤时,我发现我无法提取feature_importances
的向量,而是一个 function。之前的帖子(如何在 Sparklyr 中提取特征重要性? )将其显示为一个向量,我会喜欢得到。 What could be my error here?我的错误可能是什么? Is there another step I need to take to unwrap the function and get a vector of values here?
我需要采取其他步骤来打开 function 并在此处获取值向量吗?
ml_stage(model, 3)$feature_importances
Here is what my output to the ml_stage
looks like (instead of a vector of values):这是我的 output 到
ml_stage
的样子(而不是值向量):
function (...)
{
tryCatch(.f(...), error = function(e) {
if (!quiet)
message("Error: ", e$message)
otherwise
}, interrupt = function(e) {
stop("Terminated by user", call. = FALSE)
})
}
<bytecode: 0x559a0d438278>
<environment: 0x559a0ce8e840>
I am not sure if this is what you want, but could combine the vectorizer model and vocaculary to extract the feature_importances
of your model which will results in a table with the importances of your text.我不确定这是否是您想要的,但可以结合向量化器 model 和词汇来提取 model 的
feature_importances
,这将生成一个包含文本重要性的表格。 You could use the following code:您可以使用以下代码:
library(sparklyr)
library(dplyr)
sc <- spark_connect(method = "databricks")
dtrain <- data_frame(text = c("Chinese Beijing Chinese",
"Chinese Chinese Shanghai",
"Chinese Macao",
"Tokyo Japan Chinese"),
doc_id = 1:4,
class = c(1, 1, 1, 0))
dtrain_spark <- copy_to(sc, dtrain, overwrite = TRUE)
pipeline <- ml_pipeline(
ft_tokenizer(sc, input_col = "text", output_col = "tokens"),
ft_count_vectorizer(sc, input_col = 'tokens', output_col = 'myvocab'),
ml_decision_tree_classifier(sc, label_col = "class",
features_col = "myvocab",
prediction_col = "pcol",
probability_col = "prcol",
raw_prediction_col = "rpcol")
)
model <- ml_fit(pipeline, dtrain_spark)
tibble(
token = unlist(ml_stage(model, 'count_vectorizer')$vocabulary),
importance = ml_stage(model, 'decision_tree_classifier')$feature_importances
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.