-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-20906][SparkR]:Constrained Logistic Regression for SparkR #18128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
602c27f
add constraint logit
wangmiao1981 2dd0b7d
add unit test and doc
wangmiao1981 5b272fd
fix <-
wangmiao1981 eb65a1d
fix style
wangmiao1981 62a436b
change test order
wangmiao1981 43640f4
add check and test for matrix type
wangmiao1981 9ab89c7
extra spaces
wangmiao1981 b8d683c
address review comments
wangmiao1981 98709ac
add unit test and fix R style issue
wangmiao1981 8c88a4f
address review comments
wangmiao1981 8bebb98
revert change of as.integer
wangmiao1981 45b62cc
address review comments
wangmiao1981 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,6 +202,20 @@ function(object, path, overwrite = FALSE) { | |
| #' @param aggregationDepth The depth for treeAggregate (greater than or equal to 2). If the dimensions of features | ||
| #' or the number of partitions are large, this param could be adjusted to a larger size. | ||
| #' This is an expert parameter. Default value should be good for most cases. | ||
| #' @param lowerBoundsOnCoefficients The lower bounds on coefficients if fitting under bound constrained optimization. | ||
| #' The bound matrix must be compatible with the shape (1, number of features) for binomial | ||
| #' regression, or (number of classes, number of features) for multinomial regression. | ||
| #' It is a R matrix. | ||
| #' @param upperBoundsOnCoefficients The upper bounds on coefficients if fitting under bound constrained optimization. | ||
| #' The bound matrix must be compatible with the shape (1, number of features) for binomial | ||
| #' regression, or (number of classes, number of features) for multinomial regression. | ||
| #' It is a R matrix. | ||
| #' @param lowerBoundsOnIntercepts The lower bounds on intercepts if fitting under bound constrained optimization. | ||
| #' The bounds vector size must be equal to 1 for binomial regression, or the number | ||
| #' of classes for multinomial regression. | ||
| #' @param upperBoundsOnIntercepts The upper bounds on intercepts if fitting under bound constrained optimization. | ||
| #' The bound vector size must be equal to 1 for binomial regression, or the number | ||
| #' of classes for multinomial regression. | ||
| #' @param ... additional arguments passed to the method. | ||
| #' @return \code{spark.logit} returns a fitted logistic regression model. | ||
| #' @rdname spark.logit | ||
|
|
@@ -239,21 +253,64 @@ function(object, path, overwrite = FALSE) { | |
| setMethod("spark.logit", signature(data = "SparkDataFrame", formula = "formula"), | ||
| function(data, formula, regParam = 0.0, elasticNetParam = 0.0, maxIter = 100, | ||
| tol = 1E-6, family = "auto", standardization = TRUE, | ||
| thresholds = 0.5, weightCol = NULL, aggregationDepth = 2) { | ||
| thresholds = 0.5, weightCol = NULL, aggregationDepth = 2, | ||
| lowerBoundsOnCoefficients = NULL, upperBoundsOnCoefficients = NULL, | ||
| lowerBoundsOnIntercepts = NULL, upperBoundsOnIntercepts = NULL) { | ||
| formula <- paste(deparse(formula), collapse = "") | ||
| row <- 0 | ||
| col <- 0 | ||
|
|
||
| if (!is.null(weightCol) && weightCol == "") { | ||
| weightCol <- NULL | ||
| } else if (!is.null(weightCol)) { | ||
| weightCol <- as.character(weightCol) | ||
| } | ||
|
|
||
| if (!is.null(lowerBoundsOnIntercepts)) { | ||
| lowerBoundsOnIntercepts <- as.array(lowerBoundsOnIntercepts) | ||
| } | ||
|
|
||
| if (!is.null(upperBoundsOnIntercepts)) { | ||
| upperBoundsOnIntercepts <- as.array(upperBoundsOnIntercepts) | ||
| } | ||
|
|
||
| if (!is.null(lowerBoundsOnCoefficients)) { | ||
| if (class(lowerBoundsOnCoefficients) != "matrix") { | ||
| stop("lowerBoundsOnCoefficients must be a matrix.") | ||
| } | ||
| row <- nrow(lowerBoundsOnCoefficients) | ||
| col <- ncol(lowerBoundsOnCoefficients) | ||
| lowerBoundsOnCoefficients <- as.array(as.vector(lowerBoundsOnCoefficients)) | ||
| } | ||
|
|
||
| if (!is.null(upperBoundsOnCoefficients)) { | ||
| if (class(upperBoundsOnCoefficients) != "matrix") { | ||
| stop("upperBoundsOnCoefficients must be a matrix.") | ||
| } | ||
|
|
||
| if (!is.null(lowerBoundsOnCoefficients) && (row != nrow(upperBoundsOnCoefficients) | ||
| || col != ncol(upperBoundsOnCoefficients))) { | ||
| stop(paste0("dimension of upperBoundsOnCoefficients ", | ||
| "is not the same as lowerBoundsOnCoefficients", sep = "")) | ||
| } | ||
|
|
||
| if (is.null(lowerBoundsOnCoefficients)) { | ||
| row <- nrow(upperBoundsOnCoefficients) | ||
| col <- ncol(upperBoundsOnCoefficients) | ||
| } | ||
|
|
||
| upperBoundsOnCoefficients <- as.array(as.vector(upperBoundsOnCoefficients)) | ||
| } | ||
|
|
||
| jobj <- callJStatic("org.apache.spark.ml.r.LogisticRegressionWrapper", "fit", | ||
| data@sdf, formula, as.numeric(regParam), | ||
| as.numeric(elasticNetParam), as.integer(maxIter), | ||
| as.numeric(tol), as.character(family), | ||
| as.logical(standardization), as.array(thresholds), | ||
| weightCol, as.integer(aggregationDepth)) | ||
| weightCol, as.integer(aggregationDepth), | ||
| as.integer(row), as.integer(col), | ||
|
||
| lowerBoundsOnCoefficients, upperBoundsOnCoefficients, | ||
| lowerBoundsOnIntercepts, upperBoundsOnIntercepts) | ||
| new("LogisticRegressionModel", jobj = jobj) | ||
| }) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given how this is used later in scala code, should there be a check that nrow(upper) == nrow(lower) and ditto for ncol(upper) == ncol(lower)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the case where we only set the upperbound. We can set both or either one of them.
For the case that both are set. We enforce upperbound and lowerbound are the same dimension, as checked above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok thanks, L290-291