在 Tensorflow(或 Tensorly)中将 2D Dataframe 转换为多维张量

Converting 2D Dataframe to Multidimensional Tensor in Tensorflow (or Tensorly)

我是 python 的新手,正在尝试高维张量分解技术。但首先我需要将我的数据帧从二维数组转换为多维张量,但我有点不知如何去做。

我的数据框如下所示:

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'),对吗?

提前致谢!

使用 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)>