[英]Spark ML Error: Incorrect no. of classes detected while using Linear SVC
我正在研究二进制分类问题并使用 SparkML,我使用随机森林和逻辑回归模型训练和评估了我的数据,现在我想检查 SVM 对我的数据的分类效果如何。
我的训练数据片段:-
+----------+------+
| spam | count|
+----------+------+
| No|197378|
| Yes| 7652|
+----------+------+
Note:- My dependent variable: 'spam': string (nullable = true)
+-----+------+
|label| count|
+-----+------+
| 0.0|197488|
| 1.0| 7650|
+-----+------+
Note:- label: double (nullable = false)
更新我的问题:-
trainingData.select('label').distinct().show()
+-----+
|label|
+-----+
| 0.0|
| 1.0|
+-----+
所以,我使用下面的代码来使用线性 SVC来拟合我的训练数据:-
pyspark.ml.classification import LinearSVC
lsvc = LinearSVC()
# Fit the model
lsvcModel = lsvc.fit(trainingData)
在我的数据框中,label 和因变量只有 2 个类,但我收到一条错误消息,提示检测到更多类。 不太确定是什么导致了这个异常。
任何帮助深表感谢!
错误:-
IllegalArgumentException: u'requirement failed: LinearSVC only supports
binary classification. 3 classes detected in
LinearSVC_4240bb949b9fad486ec0__labelCol'
您可以尝试使用OnehotEncoder
将您的标签值转换为分类数据,其handleInvalid
参数为“保持”
我有同样的问题。
scala> TEST_DF_37849c70_7cd3_4fd6_a9a0_df4de727df25.select("si_37849c70_7cd3_4fd6_a9a0_df4de727df25_logicProp1_lable_left").distinct.show
+-------------------------------------------------------------+
|si_37849c70_7cd3_4fd6_a9a0_df4de727df25_logicProp1_lable_left|
+-------------------------------------------------------------+
| 0.0|
| 1.0|
+-------------------------------------------------------------+
错误:要求失败:LinearSVC 仅支持二进制分类。 在 linearsvc_d18a38204551__labelCol 中检测到 3 个类
但就我而言,将 StringIndexer 与 setHandleInvalid(" skip ") 选项一起使用,它有效。 也许 LeanerSVM 在 StringIndexer "keep" 选项的情况下有一些错误。
LogisticRegression
和DecisionTreeClassification
模型也存在此问题。 在的情况下DecisionTreeClassifier
,有关打印numClasses
的值decisionTreeModel
,它在打印时3中唯一值的标签栏的数目仅为2。 但是模型拟合过程不会失败。 但是LogisticRegression
也注意到了类似的行为,唯一的区别是模型构建过程失败。
现在还不清楚,当我们不使用StringIndexer
的setHandleInvalid()
keep
选项时,为什么不会出现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.