Repository: spark Updated Branches: refs/heads/master 15b144d2b -> 8b57ea4a1
[SPARK-19639][SPARKR][EXAMPLE] Add spark.svmLinear example and update vignettes ## What changes were proposed in this pull request? We recently add the spark.svmLinear API for SparkR. We need to add an example and update the vignettes. ## How was this patch tested? Manually run example. Author: [email protected] <[email protected]> Closes #16969 from wangmiao1981/example. Project: http://git-wip-us.apache.org/repos/asf/spark/repo Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/8b57ea4a Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/8b57ea4a Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/8b57ea4a Branch: refs/heads/master Commit: 8b57ea4a1e582d24baf37df3eb148804d83cf767 Parents: 15b144d Author: [email protected] <[email protected]> Authored: Fri Feb 17 21:21:10 2017 -0800 Committer: Felix Cheung <[email protected]> Committed: Fri Feb 17 21:21:10 2017 -0800 ---------------------------------------------------------------------- R/pkg/vignettes/sparkr-vignettes.Rmd | 22 ++++++++++++++++ examples/src/main/r/ml/survreg.R | 1 + examples/src/main/r/ml/svmLinear.R | 42 +++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/spark/blob/8b57ea4a/R/pkg/vignettes/sparkr-vignettes.Rmd ---------------------------------------------------------------------- diff --git a/R/pkg/vignettes/sparkr-vignettes.Rmd b/R/pkg/vignettes/sparkr-vignettes.Rmd index a742484..bc8bc3c 100644 --- a/R/pkg/vignettes/sparkr-vignettes.Rmd +++ b/R/pkg/vignettes/sparkr-vignettes.Rmd @@ -469,6 +469,8 @@ SparkR supports the following machine learning models and algorithms. #### Classification +* Linear Support Vector Machine (SVM) Classifier + * Logistic Regression * Multilayer Perceptron (MLP) @@ -532,6 +534,26 @@ head(carsDF_test) ### Models and Algorithms +#### Linear Support Vector Machine (SVM) Classifier + +[Linear Support Vector Machine (SVM)](https://en.wikipedia.org/wiki/Support_vector_machine#Linear_SVM) classifier is an SVM classifier with linear kernels. +This is a binary classifier. We use a simple example to show how to use `spark.svmLinear` +for binary classification. + +```{r} +# load training data and create a DataFrame +t <- as.data.frame(Titanic) +training <- createDataFrame(t) +# fit a Linear SVM classifier model +model <- spark.svmLinear(training, Survived ~ ., regParam = 0.01, maxIter = 10) +summary(model) +``` + +Predict values on training data +```{r} +prediction <- predict(model, training) +``` + #### Logistic Regression [Logistic regression](https://en.wikipedia.org/wiki/Logistic_regression) is a widely-used model when the response is categorical. It can be seen as a special case of the [Generalized Linear Predictive Model](https://en.wikipedia.org/wiki/Generalized_linear_model). http://git-wip-us.apache.org/repos/asf/spark/blob/8b57ea4a/examples/src/main/r/ml/survreg.R ---------------------------------------------------------------------- diff --git a/examples/src/main/r/ml/survreg.R b/examples/src/main/r/ml/survreg.R index bf6c0a1..e4eadfc 100644 --- a/examples/src/main/r/ml/survreg.R +++ b/examples/src/main/r/ml/survreg.R @@ -43,3 +43,4 @@ head(aftPredictions) # $example off$ sparkR.session.stop() + http://git-wip-us.apache.org/repos/asf/spark/blob/8b57ea4a/examples/src/main/r/ml/svmLinear.R ---------------------------------------------------------------------- diff --git a/examples/src/main/r/ml/svmLinear.R b/examples/src/main/r/ml/svmLinear.R new file mode 100644 index 0000000..c632f12 --- /dev/null +++ b/examples/src/main/r/ml/svmLinear.R @@ -0,0 +1,42 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# To run this example use +# ./bin/spark-submit examples/src/main/r/ml/svmLinear.R + +# Load SparkR library into your R session +library(SparkR) + +# Initialize SparkSession +sparkR.session(appName = "SparkR-ML-svmLinear-example") + +# $example on$ +# load training data +t <- as.data.frame(Titanic) +training <- createDataFrame(t) + +# fit Linear SVM model +model <- spark.svmLinear(training, Survived ~ ., regParam = 0.01, maxIter = 10) + +# Model summary +summary(model) + +# Prediction +prediction <- predict(model, training) +showDF(prediction) +# $example off$ +sparkR.session.stop() --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
