如何解决如何转换列类型以匹配 pyspark 中的连接数据帧?
我在 pyspark 中有一个空数据框,我想用它来附加来自 pyspark 中 model.transform(test_data)
的机器学习结果 - 但随后我尝试了一个联合函数来加入数据框,我得到一个列类型必须匹配错误。
这是我的代码:
sc = SparkContext.getOrCreate()
spark = SparkSession(sc)
schema = StructType([
StructField("row_num",IntegerType(),True),StructField("label",StructField("probability",DoubleType(),])
empty = spark.createDataFrame(sc.emptyRDD(),schema)
model = LogisticRegression().fit(train_data)
preds = model.transform(test_data)
all_preds = empty.unionAll(preds)
AnalysisException: Union can only be performed on tables with the compatible column types.
struct<type:tinyint,size:int,indices:array<int>,values:array<double>> <> double at the third column of the second table;
我尝试将我的空数据框的类型转换为匹配,但无法获得相同的类型 - 有什么办法可以解决这个问题吗?我的目标是让机器学习在 for 循环中迭代运行,并将每个预测输出附加到 pyspark 数据帧。
作为参考,preds
看起来像:
preds.printSchema()
root
|-- row_num: integer (nullable = true)
|-- label: integer (nullable = true)
|-- probability: vector (nullable = true)
解决方法
您可以根据 preds
数据框的架构创建一个空数据框:
model = LogisticRegression().fit(train_data)
preds = model.transform(test_data)
empty = spark.createDataFrame(sc.emptyRDD(),preds.schema)
all_preds = empty.unionAll(preds)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。