PySpark MLlib:AssertionError:分类器不从HasRawPredictionCol扩展

时间:2018-04-29 22:27:02

标签: apache-spark pyspark svm apache-spark-mllib apache-spark-ml

我是Spark的新手。我想在PySpark MLlib中使用SVM的多类分类。我在Windows上安装了Spark 2.3.0。

但我搜索并发现SVM仅在Spark中实现二进制分类,因此我们必须使用one-all-all策略。当我尝试使用SVM的one-vs-all时,它给了我一个错误。我搜索了错误,但没有找到解决方案。

我使用了此链接中的one-vs-all代码 https://spark.apache.org/docs/2.1.0/ml-classification-regression.html#one-vs-rest-classifier-aka-one-vs-all

这是我的代码:

        from pyspark.mllib.classification import SVMWithSGD , SVMModel
        from pyspark.ml.classification import OneVsRest
        # instantiate the One Vs Rest Classifier.
        svm_model = SVMWithSGD()
        ovr = OneVsRest(classifier=svm_model)
        # train the multiclass model.
        ovrModel = ovr.fit(rdd_train)
        # score the model on test data.
        predictions = ovrModel.transform(rdd_test)

错误在“ovr.fit(rdd_train)”行中。这是错误

  File "D:/Mycode-newtrials - Copy/stance_detection -norelieff-lgbm - randomizedsearch - modified - spark.py", line 1460, in computescores
ovrModel = ovr.fit(rdd_train)
  File "D:\python27\lib\site-packages\pyspark\ml\base.py", line 132, in fit
return self._fit(dataset)
  File "D:\python27\lib\site-packages\pyspark\ml\classification.py", line 1758, in _fit
"Classifier %s doesn't extend from HasRawPredictionCol." % type(classifier)
 AssertionError: Classifier <class 'pyspark.mllib.classification.SVMWithSGD'> doesn't extend from HasRawPredictionCol.

1 个答案:

答案 0 :(得分:1)

您收到错误是因为您尝试使用Spark ML(OneVsRest)中的模型和Spark MLlib(SVMWithSGD)中的基本二进制分类器。

Spark MLlib(旧的,基于RDD的API)和Spark ML(基于数据框的新API)不仅是不同的库,而且不兼容 :你不能在它们之间混合模型(仔细观察这些例子,你会看到它们从pyspark.ml导入基础分类器,而不是从pyspark.mllib导入,就像你在这里尝试的那样。)< / p>

不幸的是,在撰写本文时(Spark 2.3)Spark ML不包含SVM,您目前无法将该算法用作OneVsRest的基本分类器...