【问题标题】:Converting the float column in Spark Dataframe to VectorUDT将 Spark Dataframe 中的浮点列转换为 VectorUDT
【发布时间】:2016-11-03 18:34:08
【问题描述】:

我试图使用如下所示的 pyspark.ml.evaluation 二进制分类指标

evaluator = BinaryClassificationEvaluator(rawPredictionCol="prediction")
print evaluator.evaluate(predictions)

我的预测数据框如下所示:

predictions.select('rating','prediction')
predictions.show()
+------+------------+
|rating|  prediction|
+------+------------+
|     1|  0.14829934|
|     1|-0.017862909|
|     1|   0.4951505|
|     1|0.0074382657|
|     1|-0.002562912|
|     1|   0.0208337|
|     1| 0.049362548|
|     1|  0.09693333|
|     1|  0.17998546|
|     1| 0.019649783|
|     1| 0.031353004|
|     1|  0.03657037|
|     1|  0.23280995|
|     1| 0.033190556|
|     1|  0.35569906|
|     1| 0.030974165|
|     1|   0.1422375|
|     1|  0.19786166|
|     1|  0.07740938|
|     1|  0.33970386|
+------+------------+
only showing top 20 rows

每一列的数据类型如下:

predictions.printSchema()
root
 |-- rating: integer (nullable = true)
 |-- prediction: float (nullable = true)

现在我得到一个错误,上面的 Ml 代码说预测列是 Float 并且需要一个 VectorUDT。

/Users/i854319/spark/python/lib/py4j-0.9-src.zip/py4j/java_gateway.py in __call__(self, *args)
    811         answer = self.gateway_client.send_command(command)
    812         return_value = get_return_value(
--> 813             answer, self.gateway_client, self.target_id, self.name)
    814 
    815         for temp_arg in temp_args:

/Users/i854319/spark/python/pyspark/sql/utils.pyc in deco(*a, **kw)
     51                 raise AnalysisException(s.split(': ', 1)[1], stackTrace)
     52             if s.startswith('java.lang.IllegalArgumentException: '):
---> 53                 raise IllegalArgumentException(s.split(': ', 1)[1], stackTrace)
     54             raise
     55     return deco

IllegalArgumentException: u'requirement failed: Column prediction must be of type org.apache.spark.mllib.linalg.VectorUDT@f71b0bce but was actually FloatType.'

所以我想到了将预测列从 float 转换为 VectorUDT,如下所示:

将架构应用于数据框以将浮点列类型转换为 VectorUDT

from pyspark.sql.types import IntegerType, StructType,StructField

schema = StructType([
    StructField("rating", IntegerType, True),
    StructField("prediction", VectorUDT(), True)
])


predictions_dtype=sqlContext.createDataFrame(prediction,schema)

但是现在我得到了这个错误。

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-30-8fce6c4bbeb4> in <module>()
      4 
      5 schema = StructType([
----> 6     StructField("rating", IntegerType, True),
      7     StructField("prediction", VectorUDT(), True)
      8 ])

/Users/i854319/spark/python/pyspark/sql/types.pyc in __init__(self, name, dataType, nullable, metadata)
    401         False
    402         """
--> 403         assert isinstance(dataType, DataType), "dataType should be DataType"
    404         if not isinstance(name, str):
    405             name = name.encode('utf-8')

AssertionError: dataType should be DataType

在 spark 库中运行 ml 算法需要花费大量时间,但会出现很多奇怪的错误。甚至我用 RDD 数据尝试了 Mllib。这给出了 ValueError: Null 指针异常。

请指教。

【问题讨论】:

  • 介意为什么它被否决了吗?

标签: python apache-spark pyspark


【解决方案1】:

试试:

as_prob = udf(lambda x: DenseVector([1 - x, x]), VectorUDT())

df.withColumn("prediction", as_prob(df["prediction"]))

来源:Tuning parameters for implicit pyspark.ml ALS matrix factorization model through pyspark.ml CrossValidator

【讨论】:

  • 酷。这行得通。快速的问题。 pyspark.ml.BinaryClassificationEvaluator 应将原始标签和预测值作为输入。原始标签应该是整数,因为它是 1 或 0。我的原始评级是整数,但是它给出了一个错误,它需要标签 col 是双倍的?我不确定为什么需要这样做。它也将标签转换为浮点值,然后进行比较?
猜你喜欢
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 2017-01-26
  • 2016-12-01
  • 2018-04-17
  • 2021-09-25
  • 2017-03-17
  • 1970-01-01
相关资源
最近更新 更多