SVM
- Model API type
The LinearSVC algorithm provides ML classification APIs.
Model API Type
Function API
ML Classification API
def fit(dataset: Dataset[_]): LinearSVCModel
def fit(dataset: Dataset[_], paramMap:ParamMap) : LinearSVCModel
def fit(dataset: Dataset[_], paramMaps: Array[ParamMap]) : LinearSVCModel
def fit(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]* ) : LinearSVCModel
- Input: the model parameters of the fit API paramMap, paramMaps, firstParamPair, otherParamPairs, which are described as follows:
Parameter
Value Type
Example
Description
paramMap
ParamMap
ParamMap(A.c -> b)
Assigns the value of b to the parameter c of model A.
paramMaps
Array[ParamMap]
Array[ParamMap](n)
Generates n parameter lists for the ParamMap model.
firstParamPair
ParamPair
ParamPair(A.c, b)
Assigns the value of b to the parameter c of model A.
otherParamPairs
ParamPair
ParamPair(A.e, f)
Assigns the value of f to the parameter e of model A.
ML Classification API
- Function description
Output the LinearSVC classification model after you input sample data in dataset format and call the fit API.
- Input and output
- Package name: package org.apache.spark.ml.classification
- Class name: LinearSVC
- Method name: fit
- Input: training sample data (Dataset[_]). The following are mandatory fields.
Parameter
Value Type
Default Value
Description
labelCol
Double
label
Predicted label
featuresCol
Vector
features
Feature label
- Parameters optimized based on native algorithms
1 2 3 4 5 6 7 8
def setRegParam(value: Double): this.type def setMaxIter(value: Int): this.type def setFitIntercept(value: Boolean): this.type def setTol(value: Double): this.type def setStandardization(value: Boolean): this.type def setWeightCol(value: String): this.type def setThreshold(value: Double): this.type def setAggregationDepth(value: Int): this.type
- Newly added parameters
Parameter
Description
Value Type
inertiaCoefficient
Weight of historical direction information in momentum calculation
Double type. The value is a positive real number.
An example is provided as follows:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import org.apache.spark.ml.param.{ParamMap, ParamPair} val svm = new LinearSVC() // Define the def fit(dataset: Dataset[_], paramMap: ParamMap) API parameter. val paramMap = ParamMap(svm.regParam -> regParam) .put(svm.maxIter, numIterations) // Define the def fit(dataset: Dataset[_], paramMaps: Array[ParamMap]): API parameter. val paramMaps: Array[ParamMap] = new Array[ParamMap](2) for (i <- 0 to 2) { paramMaps(i) = ParamMap(svm.regParam -> regParam(i)) .put(svm.maxIter, numIterations) }//Assign a value to paramMaps. // Define the def fit(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*) API parameter. val defaultParamPair = ParamPair(svm.regParam, regParam) val regParamPair = ParamPair(svm.regParam, regParam) val maxIterParamPair = ParamPair(svm.maxIter, numIterations) // Call the fit APIs. model = svm.fit(trainingData) // Return GBTRegressionModel. model = svm.fit(trainingData, paramMap) // Return GBTRegressionModel. models = svm.fit(trainingData, paramMaps) // Return Seq[GBTRegressionModel]. model = svm.fit(trainingData, defaultParamPair, regParamPair, maxIterParamPair) // Return SVMRegressionModel.
- Output: LinearSVC classification model (LinearSVCModel). The following table lists the fields output in model prediction.
Parameter
Value Type
Default Value
Description
Remarks
prediction
Double
prediction
Predicted label
-
label
Double
label
label
Classification only
features
Vector
features
features
Classification only
- Example
fit(dataset: Dataset[_]): LinearSVCModel example:
1 2 3 4 5 6 7 8
test("train"){ val svc = new LinearSVC().setMaxIter(100).setRegParam(0.1) svc.setIc(0.1) val msvc = svc.fit(sdf) val res = msvc.transform(sdf) val accuracy = res.filter($"label"===$"prediction").count().toDouble/res.count println("svmx Accuracy = "+accuracy) }
- Result
1svmx Accuracy = 0.999406629786391