Linear Support Vector Machines multiclass classification with PySpark API
<p>Support Vector Machines currently does not yet support multi class classification within Spark, but will in the future as it is described on the Spark <a href="http://spark.apache.org/docs/latest/mllib-classification-regression.html" rel="nofollow">page</a>.</p>

<p>Is there any release date or any chance to run it with PySpark API that implements multi class with Support Vector Machines? Thank you for any insights.</p>

<p>In practice you can perform multiclass classification using an arbitrary binary classifier and one-vs-rest strategy. <code>mllib</code> doesn't provide one (there is one in <code>ml</code>) but you can easily built your own. Assuming data looks like this</p>

<pre><code>import numpy as np
np.random.seed(323)

classes = [0, 1, 2, 3, 4, 5]

def make_point(classes):
    label = np.random.choice(classes)
    features = np.random.random(len(classes))
    features[label] += 10
    return LabeledPoint(label, features)

data  = sc.parallelize([make_point(classes) for _ in range(1000)])

xs = data.take(5)
</code></pre>

<p>we can train separate model for each class:</p>

<pre><code>def model_for_class(c, rdd):
    def adjust_label(lp):
        return LabeledPoint(1 if lp.label == c else 0, lp.features)

    model = SVMWithSGD.train(rdd.map(adjust_label))
    model.clearThreshold()
    return model

models = [model_for_class(c, data) for c in classes]
</code></pre>

<p>and use it for prediction:</p>

<pre><code>[(x.label, np.argmax([model.predict(x.features) for model in models]))
for x in xs]
## [(0.0, 0), (1.0, 1), (0.0, 0), (5.0, 5), (2.0, 2)]
</code></pre>

<p>On the side note you cannot expect any further developments in <code>pyspark.mllib</code> because it is getting deprecated in favor of <code>ml</code>. </p>

python apache-spark pyspark svm text-classification