【问题标题】:Converting 2D Dataframe to Multidimensional Tensor in Tensorflow (or Tensorly)在 Tensorflow(或 Tensorly)中将 2D 数据帧转换为多维张量
【发布时间】:2022-01-05 23:09:45
【问题描述】:

我对 python 非常陌生,并且正在尝试使用更高维张量分解技术。但首先我需要将我的数据帧从 2D 数组转换为多维张量,但我有点不知道该怎么做。

我的数据框如下所示:

Subject Cz  F7  F8...Pz Diagnosis Test  Time 
1       #   #   #    #    A        x     100 
1       #   #   #    #    A        x     200   
1       #   #   #    #    A        y     100
1       #   #   #    #    A        y     200
2       #   #   #    #    B        x     100
2       #   #   #    #    B        x     200
2       #   #   #    #    B        y     100
2       #   #   #    #    B        y     200

我想将其转换为 3 级张量:

Dimension 1: Channel (Cz F7 F8...Pz)
Dimension 2: Test    (x y)
Dimension 3: Time    (100 200)

and also turn Diagnosis into a predictor label

由于数据框的设置方式,我认为我不能只做label = df.pop('Diagnosis'),对吗?

提前致谢!

【问题讨论】:

    标签: python pandas tensorflow tensorly


    【解决方案1】:

    使用 tf.reshape 将二维数组转换为 n 维数组的简单方法。

    示例

    import tensorflow as tf
    t = [[1, 2],
         [4, 5],
         [3,4],
         [6,7]]
    print(tf.shape(t).numpy())
    
    tf.reshape(t, [2, 2, 2])
    

    输出

    [4 2]
    <tf.Tensor: shape=(2, 2, 2), dtype=int32, numpy=
    array([[[1, 2],
            [4, 5]],
    
           [[3, 4],
            [6, 7]]], dtype=int32)>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-17
      • 2017-07-06
      • 1970-01-01
      • 2018-10-22
      • 2021-05-28
      • 2022-06-29
      相关资源
      最近更新 更多