将 3 Dim 转换为 2 Dim 并调整 Class?

Convert 3 Dim to 2 Dim and ajust Class?

数据:

我有一个大小为 (2200, 1000, 12) 的数组。第一个值(2200)是索引,每个索引中有1000条记录。

我有另一个 Class 数组,大小为 (2200)。这里的每个变量代表每个索引中 1000 条记录的标签。

我要:

如何将第一个数组中的所有内容放在一起以从 3 维转换为 2 维?

如何将每个 class 变量放入 1000 条记录中?

想要的结果:

数据帧大小 (2200000,13)

2200000 将是 2200 索引中 1000 条记录的总和。第 13 列将是与 Class 的连接点,其中 class 的每个变量将重复一千次以保持相同的行数。

这对你有帮助吗:

array = array.reshape(2200000,13)

让我们首先导入必要的模块并生成模拟数据:

import numpy as np
import pandas as pd

M = 2200
N = 1000
P = 12

data = np.random.rand(M, N, P)
classes = np.arange(M)

How can I transform from 3 dimensions to 2 dimensions?

data.reshape(M*N, P)

How can I put each class variable in the 1000 records?

np.repeat(classes, N)

Desired result: Dataframe Size (2200000,13)

arr = np.hstack([data.reshape(M*N, P), np.repeat(classes, N)[:, None]])
df = pd.DataFrame(arr)
print(df)

上面的代码输出:

0        0.371495  0.598211  0.038224  ...  0.777405  0.193472     0.0
1        0.356371  0.636690  0.841467  ...  0.403570  0.330145     0.0
2        0.793879  0.008617  0.701122  ...  0.021139  0.514559     0.0
3        0.318618  0.798823  0.844345  ...  0.931606  0.467469     0.0
4        0.307109  0.076505  0.865164  ...  0.809495  0.914563     0.0
...           ...       ...       ...  ...       ...       ...     ...
2199995  0.215133  0.239560  0.477092  ...  0.050997  0.727986  2199.0
2199996  0.249206  0.881694  0.985973  ...  0.897410  0.564516  2199.0
2199997  0.378455  0.697581  0.016306  ...  0.985966  0.638413  2199.0
2199998  0.233829  0.158274  0.478611  ...  0.825343  0.215944  2199.0
2199999  0.351320  0.980258  0.677298  ...  0.791046  0.736788  2199.0