pyspark.mllib.classification.
SVMModel
Model for Support Vector Machines (SVMs).
New in version 0.9.0.
pyspark.mllib.linalg.Vector
Weights computed for every feature.
Intercept computed for this model.
Examples
>>> from pyspark.mllib.linalg import SparseVector >>> data = [ ... LabeledPoint(0.0, [0.0]), ... LabeledPoint(1.0, [1.0]), ... LabeledPoint(1.0, [2.0]), ... LabeledPoint(1.0, [3.0]) ... ] >>> svm = SVMWithSGD.train(sc.parallelize(data), iterations=10) >>> svm.predict([1.0]) 1 >>> svm.predict(sc.parallelize([[1.0]])).collect() [1] >>> svm.clearThreshold() >>> svm.predict(numpy.array([1.0])) 1.44...
>>> sparse_data = [ ... LabeledPoint(0.0, SparseVector(2, {0: -1.0})), ... LabeledPoint(1.0, SparseVector(2, {1: 1.0})), ... LabeledPoint(0.0, SparseVector(2, {0: 0.0})), ... LabeledPoint(1.0, SparseVector(2, {1: 2.0})) ... ] >>> svm = SVMWithSGD.train(sc.parallelize(sparse_data), iterations=10) >>> svm.predict(SparseVector(2, {1: 1.0})) 1 >>> svm.predict(SparseVector(2, {0: -1.0})) 0 >>> import os, tempfile >>> path = tempfile.mkdtemp() >>> svm.save(sc, path) >>> sameModel = SVMModel.load(sc, path) >>> sameModel.predict(SparseVector(2, {1: 1.0})) 1 >>> sameModel.predict(SparseVector(2, {0: -1.0})) 0 >>> from shutil import rmtree >>> try: ... rmtree(path) ... except BaseException: ... pass
Methods
clearThreshold()
clearThreshold
Clears the threshold so that predict will output raw prediction scores.
load(sc, path)
load
Load a model from the given path.
predict(x)
predict
Predict values for a single data point or an RDD of points using the model trained.
save(sc, path)
save
Save this model to the given path.
setThreshold(value)
setThreshold
Sets the threshold that separates positive predictions from negative predictions.
Attributes
intercept
threshold
Returns the threshold (if any) used for converting raw prediction scores into 0/1 predictions.
weights
Methods Documentation
Clears the threshold so that predict will output raw prediction scores. It is used for binary classification only.
New in version 1.4.0.
Sets the threshold that separates positive predictions from negative predictions. An example with prediction score greater than or equal to this threshold is identified as a positive, and negative otherwise. It is used for binary classification only.
Attributes Documentation
New in version 1.0.0.
Returns the threshold (if any) used for converting raw prediction scores into 0/1 predictions. It is used for binary classification only.