Skip to content

Commit

Permalink
getModel fixes for kmeans
Browse files Browse the repository at this point in the history
  • Loading branch information
spennihana committed Dec 17, 2014
1 parent 46efdc5 commit bf6fba1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
10 changes: 5 additions & 5 deletions R/h2o-package/R/Algorithms.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions R/h2o-package/R/Classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion R/h2o-package/R/Internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit bf6fba1

Please sign in to comment.