gpt4 book ai didi

python - PySpark ML : OnevsRest strategy for LinearSVC

转载 作者:行者123 更新时间:2023-12-05 06:34:17 26 4
gpt4 key购买 nike

我是 PySpark 的新手。我在 Windows 10 上安装了 Spark 2.3.0 。我想使用线性 SVM 分类器进行交叉验证训练,但用于具有 3 个类的数据集。所以我正在尝试应用 Spark ML 的 One vs Rest 策略。但是我的代码似乎有问题,因为我收到一个错误,显示 LinearSVC 用于二进制分类。

这是我在调试时尝试执行“crossval.fit”行时发生的错误:

 pyspark.sql.utils.IllegalArgumentException: u'requirement failed: LinearSVC only supports binary classification. 1 classes detected in LinearSVC_43a48b0b70d59a8cbdb1__labelCol'

这是我的代码:(我正在尝试仅包含 10 个实例的非常小的数据集)

        from pyspark import SparkContext
sc = SparkContext('local', 'my app')
from pyspark.ml.linalg import Vectors
from pyspark import SQLContext
sqlContext = SQLContext(sc)
import numpy as np

x_train=np.array([[1,2,3],[5,6,7],[9,10,11],[2,4,5],[2,7,9],[3,7,6],[8,3,6],[5,8,2],[44,11,55],[77,33,22]])
y_train=[1,0,2,1,0,2,1,0,2,1]
#converting numpy array to dataframe
df_list = []
i = 0
for element in x_train: # row
tup = (y_train[i], Vectors.dense(element))
i = i + 1
df_list.append(tup)

Train_sparkframe = sqlContext.createDataFrame(df_list, schema=['label', 'features'])

from pyspark.ml.tuning import CrossValidator, ParamGridBuilder
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
from pyspark.ml.classification import OneVsRest
from pyspark.ml.classification import LinearSVC

LSVC = LinearSVC()
ovr = OneVsRest(classifier=LSVC)
paramGrid = ParamGridBuilder().addGrid(LSVC.maxIter, [10, 100]).addGrid(LSVC.regParam,
[0.001, 0.01, 1.0,10.0]).build()

crossval = CrossValidator(estimator=ovr,
estimatorParamMaps=paramGrid,
evaluator=MulticlassClassificationEvaluator(metricName="f1"),
numFolds=2)
cvModel = crossval.fit(Train_sparkframe)
bestModel = cvModel.bestModel

最佳答案

作为documentation说:

Note Only LogisticRegression and NaiveBayes are supported now.

关于python - PySpark ML : OnevsRest strategy for LinearSVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50338747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com