首页 > 解决方案 > Pyspark 错误:使用交叉验证时“字段 rawPrediction 不存在”

问题描述

我一直在尝试使用CrossValidator我的训练数据,但我总是收到错误消息:

"An error occurred while calling o80267.evaluate.
: java.lang.IllegalArgumentException: Field "rawPrediction" does not exist.
Available fields: label, features, CrossValidator_6a7bb791f63f_rand, features_scaled, prediction"

这是代码:

df = spark.createDataFrame(input_data, ["label", "features"])

train_data, test_data = df.randomSplit([.8,.2],seed=1234)
train_data.show()

standardScaler = StandardScaler(inputCol="features", outputCol="features_scaled")
lr = LinearRegression(maxIter=10)

pipeline = Pipeline(stages=[standardScaler, lr])

paramGrid = ParamGridBuilder()\
    .addGrid(lr.regParam, [0.3, 0.1, 0.01])\
    .addGrid(lr.fitIntercept, [False, True])\
    .addGrid(lr.elasticNetParam, [0.0, 0.5, 0.8, 1.0])\
    .build()


crossval = CrossValidator(estimator=pipeline,
                          estimatorParamMaps=paramGrid,
                          evaluator=BinaryClassificationEvaluator(),
                          numFolds=2)


cvModel = crossval.fit(train_data)

使用时train_data.show()(在第三行),输出如下:

    +-----+--------------------+
    |label|            features|
    +-----+--------------------+
    |4.526|[129.0,322.0,126....|
    |3.585|[1106.0,2401.0,11...|
    |3.521|[190.0,496.0,177....|
    |3.413|[235.0,558.0,219....|
    |3.422|[280.0,565.0,259....|
    |2.697|[213.0,413.0,193....|
    |2.992|[489.0,1094.0,514...|
    |2.414|[687.0,1157.0,647...|
    |2.267|[665.0,1206.0,595...|
    |2.611|[707.0,1551.0,714...|
    |2.815|[434.0,910.0,402....|
    |2.418|[752.0,1504.0,734...|
    |2.135|[474.0,1098.0,468...|
    |1.913|[191.0,345.0,174....|
    |1.592|[626.0,1212.0,620...|
    |  1.4|[283.0,697.0,264....|
    |1.525|[347.0,793.0,331....|
    |1.555|[293.0,648.0,303....|
    |1.587|[455.0,990.0,419....|
    |1.629|[298.0,690.0,275....|
    +-----+--------------------+

我搜rawPrediction了,但至少我是怎么理解的,这一列只是在转换测试数据DF之后才添加的。那么我在这里做错了什么,为什么会出现这个错误?我是否将某些列命名错误?我也重命名scaled_features为 justfeatures但这显然没有帮助。

标签: pythonapache-sparkmachine-learningpysparkapache-spark-ml

解决方案


您错误地BinaryClassificationEvaluator在回归问题中使用,并且由于rawPrediction仅用于分类模型而不用于回归模型,因此您的评估者查找 column rawPrediction,但没有找到它并返回错误。

更改您的交叉验证器,如下所示:

from pyspark.ml.evaluation import RegressionEvaluator

crossval = CrossValidator(estimator=pipeline,
                          estimatorParamMaps=paramGrid,
                          evaluator=RegressionEvaluator(),
                          numFolds=2)

你应该没事。


推荐阅读