### Pyspark错误:使用交叉验证时出现“Field rawPrediction does not exist”

我在训练数据上尝试使用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重命名为features,但显然这没有帮助。


回答:

您在回归问题中错误地使用了BinaryClassificationEvaluator,因为rawPrediction仅由分类模型使用,而不用于回归模型,因此您的评估器寻找rawPrediction列,但找不到它,并返回一个错误。

请按如下方式更改您的交叉验证器:

from pyspark.ml.evaluation import RegressionEvaluatorcrossval = CrossValidator(estimator=pipeline,                          estimatorParamMaps=paramGrid,                          evaluator=RegressionEvaluator(),                          numFolds=2)

这样应该就可以了。

Related Posts

为什么我们在K-means聚类方法中使用kmeans.fit函数?

我在一个视频中使用K-means聚类技术,但我不明白为…

如何获取Keras中ImageDataGenerator的.flow_from_directory函数扫描的类名?

我想制作一个用户友好的GUI图像分类器,用户只需指向数…

如何查看每个词的tf-idf得分

我试图了解文档中每个词的tf-idf得分。然而,它只返…

如何修复 ‘ValueError: Found input variables with inconsistent numbers of samples: [32979, 21602]’?

我在制作一个用于情感分析的逻辑回归模型时遇到了这个问题…

如何向神经网络输入两个不同大小的输入?

我想向神经网络输入两个数据集。第一个数据集(元素)具有…

逻辑回归与机器学习有何关联

我们正在开会讨论聘请一位我们信任的顾问来做机器学习。一…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注