【问题标题】:Reshaping pyspark dataframe to 4-dimensional numpy array for Keras/Theano将 pyspark 数据帧重塑为 Keras/Theano 的 4 维 numpy 数组
【发布时间】:2017-01-03 13:36:29
【问题描述】:

我正在尝试将 spark 数据帧 traindf 转换为 4-d numpy 数组。我试过这个:

traindf = sqlContext.createDataFrame([
    (1, 1, 2, 3),
    (1, 2, 2, 3),
    (1, 3, 2, 3),
    (1, 4, 2, 3),
    (2, 4, 5, 6),
    (2, 4, 5, 6),
    (3, 7, 8, 9),
    (2, 4, 5, 6),
    (3, 7, 8, 9),
    (3, 7, 8, 9)
], ("id", "image", "s", "t"))

values = (traindf.rdd.map(lambda l: [map(lambda r: float(r), l)]).collect())
x = np.array(values)
x = np.array_split(x, x.shape[0]/2)
x = np.asarray(x)
x.shape

这会产生 (5, 2, 1, 4),但似乎 keras 需要 (5, 1, 2, 4)。我尝试了几种方法,但没有找到获得正确格式的好方法。

有什么建议吗?

【问题讨论】:

    标签: python numpy apache-spark pyspark keras


    【解决方案1】:

    想通了,把它钉在最后

    x = np.reshape(x, (5, 1, 2, 4))
    

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 2019-11-16
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 2021-09-25
      相关资源
      最近更新 更多