LIM中对H2o建模的实现

时间:2017-07-12 13:56:51

标签: r dataframe h2o

我想在R中使用h2o(深度学习)创建的模型上实现LIME。为了使用模型中的数据,我创建了h2oFrames并将其转换回数据帧,然后在LIME中使用它(lime函数) ,因为LIME的解释函数无法识别h2oFrame)。在这里,我可以运行函数

下一步是对测试数据使用explain函数来生成解释。这里R抛出了使用数据帧和h2oFrame的错误。

这是使用数据帧时产生的错误:

Error in chk.H2OFrame(x) : must be an H2OFrame

这是使用h2oframe时产生的错误:

Error in UseMethod("permute_cases") : 
  no applicable method for 'permute_cases' applied to an object of class "H2OFrame"
if(!require(pacman))  install.packages("pacman")
pacman::p_load(h2o, lime, data.table, e1071)

data(iris)
h2o.init( nthreads = -1 )
h2o.no_progress()

# Split up the data set
iris <- as.h2o(iris)

split <- h2o.splitFrame( iris, c(0.6, 0.2), seed = 1234 )
iris_train <- h2o.assign( split[[1]], "train" ) # 60%
iris_valid <- h2o.assign( split[[2]], "valid" ) # 20%
iris_test  <- h2o.assign( split[[3]], "test" )  # 20%


output <- 'Species'
input <- setdiff(names(iris),output)


model_dl_1 <- h2o.deeplearning(
  model_id = "dl_1", 
  training_frame = iris_train, 
  validation_frame = iris_valid,
  x = input,
  y = output,
  hidden = c(32, 32, 32),
  epochs = 10, # hopefully converges earlier...
  score_validation_samples = 10000, 
  stopping_rounds = 5,
  stopping_tolerance = 0.01
)

pred1 <- h2o.predict(model_dl_1, iris_test)
list(dimension = dim(pred1), pred1$predict)

#convert to df from h2ofdataframe

train_org<-as.data.frame(iris_train) 
#converting train h2oframe to dataframe
sapply(train_org,class) #checking the class of train_org
test_df <- as.data.frame(iris_test) 
#converting test data h2oFrame to dataframe
test_sample <- test_df[1:1,] 

#works
#lime is used to get explain on the train data
explain <- lime(train_org, model_dl_1, bin_continuous = FALSE, n_bins = 
                  5, n_permutations = 1000)


# Explain new observation
explanation <- explain(test_sample, n_labels = 1, n_features = 1)
h2o.shutdown(prompt=F)

任何人都可以帮我找到解决方案或使用LIME的解释功能与相应的dataFrame

的方法

1 个答案:

答案 0 :(得分:7)

引擎盖下的lime软件包使用两个函数predict_model()model_type(),您需要为当前不支持的任何模型设置这些函数。

对于您的具体示例,这是您需要做的事情。

第1步:为类model_type的模型设置通用H2OMultinomialModel函数。你在这里所做的只是告诉你lime你希望它执行的模型类型,例如“分类”或“回归”。

model_type.H2OMultinomialModel <- function(x, ...) {
    # Function tells lime() what model type we are dealing with
    # 'classification', 'regression', 'survival', 'clustering', 'multilabel', etc
    #
    # x is our h2o model

    return("classification")

}

第2步:为类predict_model的模型设置通用H2OMultinomialModel函数。这里的关键是理解为了使石灰工作,它需要分类概率而不是预测(这需要我花一点时间来弄清楚,它必须处理lime:::output_type(explaination)变量。)

predict_model.H2OMultinomialModel <- function(x, newdata, type, ...) {
    # Function performs prediction and returns dataframe with Response
    #
    # x is h2o model
    # newdata is data frame
    # type is only setup for data frame

    pred <- h2o.predict(x, as.h2o(newdata))

    # return classification probabilities only
    return(as.data.frame(pred[,-1]))

}

正确设置这些功能后,您可以运行lime脚本。

# Lime is used to get explain on the train data
explainer <- lime(train_org, model_dl_1, bin_continuous = FALSE, n_bins = 5, n_permutations = 1000)

# Explain new observation
explanation <- explain(test_sample, explainer, n_labels = 1, n_features = 1)