spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shivaram <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-13734][SPARKR] Added histogram function
Date Tue, 19 Apr 2016 18:09:23 GMT
Github user shivaram commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11569#discussion_r60281327
  
    --- Diff: R/pkg/R/functions.R ---
    @@ -2638,3 +2638,100 @@ setMethod("sort_array",
                 jc <- callJStatic("org.apache.spark.sql.functions", "sort_array", x@jc,
asc)
                 column(jc)
               })
    +
    +#' This function computes a histogram for a given SparkR Column.
    +#' 
    +#' @name histogram
    +#' @title Histogram
    +#' @param nbins the number of bins (optional). Default value is 10.
    +#' @param df the DataFrame containing the Column to build the histogram from.
    +#' @param colname the name of the column to build the histogram from.
    +#' @return a data.frame with the histogram statistics, i.e., counts and centroids.
    +#' @rdname histogram
    +#' @family agg_funcs
    +#' @export
    +#' @examples 
    +#' \dontrun{
    +#' # Create a DataFrame from the Iris dataset
    +#' irisDF <- createDataFrame(sqlContext, iris)
    +#' 
    +#' # Compute histogram statistics
    +#' histData <- histogram(df, "colname"Sepal_Length", nbins = 12)
    +#'
    +#' # Once SparkR has computed the histogram statistics, the histogram can be
    +#' # rendered using the ggplot2 library:
    +#'
    +#' require(ggplot2)
    +#' plot <- ggplot(histStats, aes(x = centroids, y = counts))
    +#' plot <- plot + geom_histogram(data = histStats, stat = "identity", binwidth = 100)
    +#' plot <- plot + xlab("Sepal_Length") + ylab("Frequency")   
    +#' } 
    +setMethod("histogram",
    +          signature(df = "DataFrame", col = "characterOrColumn"),
    +          function(df, col, nbins = 10) {
    +            # Validate nbins
    +            if (nbins < 2) {
    +              stop("The number of bins must be a positive integer number greater than
1.")
    +            }
    +
    +            # Round nbins to the smallest integer
    +            nbins <- floor(nbins)
    +
    +            # Validate col
    +            if (is.null(col)) {
    +              stop("col must be specified.")
    +            }
    +
    +            colname <- col
    +            x <- if (class(col) == "character") {
    +              if (!colname %in% names(df)) {
    +                stop("Specified colname does not belong to the given DataFrame.")
    +              }
    +
    +              # Filter NA values in the target column
    +              df <- na.omit(df[, colname])
    +
    +              # TODO: This will be when improved SPARK-9325 or SPARK-13436 are fixed
    +              eval(parse(text = paste0("df$", colname)))
    +            } else if (class(col) == "Column") {
    +              # Append the given column to the dataset
    +              df$x <- col
    +              colname <- "x"
    +              col
    +            }
    +
    +            stats <- collect(describe(df[, colname]))
    +            min <- as.numeric(stats[4, 2])
    +            max <- as.numeric(stats[5, 2])
    +
    +            # Normalize the data
    +            xnorm <- (x - min) / (max - min)
    +
    +            # Round the data to 4 significant digits. This is to avoid rounding issues.
    +            xnorm <- cast(xnorm * 10000, "integer") / 10000.0
    +
    +            # Since min = 0, max = 1 (data is already normalized)
    +            normBinSize <- 1 / nbins
    +            binsize <- (max - min) / nbins
    +            approxBins <- xnorm / normBinSize
    +
    +            # Adjust values that are equal to the upper bound of each bin
    +            bins <- cast(approxBins -
    +                         ifelse(approxBins == cast(approxBins, "integer") & x !=
min, 1, 0),
    +                         "integer")
    +
    +            df$bins <- bins
    --- End diff --
    
    Similar question as above. I'm wondering if there is a better way than adding `bins` as
a column to the input DF. Ideally, as a user I would assume that `histogram` is a safe function
in that it doesn't mutate the input data given to it. I am not sure whats an easy solution
here though. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message