From bf6fba1150d3d9b65373fda069653a3342321b0f Mon Sep 17 00:00:00 2001 From: SpencerA Date: Tue, 16 Dec 2014 19:29:45 -0800 Subject: [PATCH] getModel fixes for kmeans --- R/h2o-package/R/Algorithms.R | 10 +++++----- R/h2o-package/R/Classes.R | 5 ++--- R/h2o-package/R/Internal.R | 7 ++++++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/R/h2o-package/R/Algorithms.R b/R/h2o-package/R/Algorithms.R index 69def51322..70c79c9567 100644 --- a/R/h2o-package/R/Algorithms.R +++ b/R/h2o-package/R/Algorithms.R @@ -535,11 +535,11 @@ h2o.kmeans <- function(data, centers, cols = '', key = "", iter.max = 10, normal params$centers = res$parameters$k params$iter.max = res$parameters$max_iter result$params = params - - result$cluster = .h2o.exec2(clusters_key, h2o = data@h2o, clusters_key) - feat = res$'_names' - result$centers = t(matrix(unlist(res$centers), ncol = res$parameters$k)) - dimnames(result$centers) = list(seq(1,res$parameters$k), feat) + + result$cluster <- if( .check.exists(data@h2o, clusters_key) ) NULL else .h2o.exec2(clusters_key, h2o = data@h2o, clusters_key) + feat <- res$'_names' + result$centers <- t(matrix(unlist(res$centers), ncol = res$parameters$k)) + dimnames(result$centers) <- list(seq(1,res$parameters$k), feat) #result$totss <- res$total_SS result$withinss <- res$within_cluster_variances ## FIXME: sum of squares != variances (bad name of the latter) result$tot.withinss <- res$total_within_SS diff --git a/R/h2o-package/R/Classes.R b/R/h2o-package/R/Classes.R index 706e040e20..2a76ad884e 100644 --- a/R/h2o-package/R/Classes.R +++ b/R/h2o-package/R/Classes.R @@ -49,7 +49,7 @@ setClass("H2OGLMModelList", representation(models="list", best_model="numeric", # setMethod("initialize", "H2OParsedData", function(.Object, h2o = new("H2OClient"), key = "") { # .Object@h2o = h2o # .Object@key = key -# .Object@env = new.env() +# .Object@env = new.env()ASTSS # # assign("h2o", .Object@h2o, envir = .Object@env) # assign("key", .Object@key, envir = .Object@env) @@ -270,11 +270,10 @@ setMethod("show", "H2OKMeansModel", function(object) { print(object@data@h2o) cat("Parsed Data Key:", object@data@key, "\n\n") cat("K-Means Model Key:", object@key) - model = object@model cat("\n\nK-means clustering with", length(model$size), "clusters of sizes "); cat(model$size, sep=", ") cat("\n\nCluster means:\n"); print(model$centers) - cat("\nClustering vector:\n"); print(summary(model$cluster)) + if (!is.null(model$cluster)) { cat("\nClustering vector:\n"); print(summary(model$cluster)) } cat("\nWithin cluster sum of squares by cluster:\n"); print(model$withinss) # cat("(between_SS / total_SS = ", round(100*sum(model$betweenss)/model$totss, 1), "%)\n") cat("\nAvailable components:\n\n"); print(names(model)) diff --git a/R/h2o-package/R/Internal.R b/R/h2o-package/R/Internal.R index 51de68fc52..f72797d13a 100644 --- a/R/h2o-package/R/Internal.R +++ b/R/h2o-package/R/Internal.R @@ -889,6 +889,11 @@ function(h2o, key) { .fetchJSON(h2o, key) } +.check.exists <- function(h2o, key) { + keys <- as.data.frame(h2o.ls(h2o))[,1] + key %in% keys +} + #' #' Fetch the model from the key h2o.getModel <- function(h2o, key) { @@ -926,7 +931,7 @@ h2o.getModel <- function(h2o, key) { dest_key <- key #params$destination_key train_fr <- new("H2OParsedData", key = "NA") - if(!is.null(response$"_dataKey")) train_fr <- h2o.getFrame(h2o, response$"_dataKey") + if(!is.null(response$"_dataKey") && .check.exists(h2o, response$"_dataKey")) train_fr <- h2o.getFrame(h2o, response$"_dataKey") params$importance <- !is.null(params$varimp) if(!is.null(params$family) && model.type == "gbm_model") { if(params$classification == "false") {params$distribution <- "gaussian"