【问题标题】:create a PySpark Schema for a tuple composed of a list and an array为由列表和数组组成的元组创建 PySpark Schema
【发布时间】:2019-07-10 12:14:22
【问题描述】:

有人可以帮忙告诉我以下元组的正确 PySpark Schema 应该是什么:

([['__label__positif', '__label__négatif', '__label__neutre']], array([[0.60312474, 0.24436191, 0.15254335]]))

提前谢谢你

【问题讨论】:

  • 你想得到什么数据帧结构?
  • 六列:三列每列包含一个标签,三列每列包含一个双数@cronoik
  • type(tuple[1]) 的输出是什么?
  • 确实是fasttext模型的预测结果,所以要预测每一行的类我必须有上面的表达式作为每一行的结果,所以我必须将预测函数传递给udf ,为此我必须在 udf 函数中指定以下表达式的架构
  • 模型的输出是:" ([['__label__positif', '__label__négatif', '__label__neutre']], array([[0.60312474, 0.24436191, 0.15254335]])) "

标签: pyspark sql-types


【解决方案1】:

看看下面的叙述代码:

import numpy as np

#this is the object you got from the fasttext model
pred = ([['__label__positif', '__label__négatif', '__label__neutre']], np.array([[0.60312474, 0.24436191, 0.15254335]]))
print(pred)

#At first we flatten this object to create a list with 6 elements
pred = [item for sublist in pred for subsubiter in sublist for item in subsubiter]
print(pred)

#pyspark doesn't work that well with numpy and therefore we cast the numpy floats to python floats
pred = [x.item() if type(x) == np.float64 else x for x in pred]
print(pred)

l = [tuple(pred)]

columns = ['one', 'two', 'three', 'four', 'five', 'six']

df=spark.createDataFrame(l, columns)
df.show()

输出:

([['__label__positif', '__label__négatif', '__label__neutre']], array([[0.60312474, 0.24436191, 0.15254335]])) 
['__label__positif', '__label__négatif', '__label__neutre', 0.60312474, 0.24436191, 0.15254335] 
['__label__positif', '__label__négatif', '__label__neutre', 0.60312474, 0.24436191, 0.15254335] 
+----------------+----------------+---------------+----------+----------+----------+ 
|             one|             two|          three|      four|      five|       six| 
+----------------+----------------+---------------+----------+----------+----------+ 
|__label__positif|__label__négatif|__label__neutre|0.60312474|0.24436191|0.15254335| 
+----------------+----------------+---------------+----------+----------+----------+

【讨论】:

    猜你喜欢
    • 2020-05-08
    • 2019-10-13
    • 1970-01-01
    • 2016-05-02
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多