python - 连接大小不等的 numpy 数组,保持索引位置固定
问题描述
假设我有 3 个变量对 A、B 和 C 的数据(在我的实际应用程序中,变量的数量在 1000-3000 之间,但可能更高)。
我们还假设数组中有一些数据。
例如:
数组 X:
np.array([[ 0., 2., 3.],
[ -2., 0., 4.],
[ -3., -4., 0.]])
在哪里:
X[0,0] = corresponds to data for variables A and A
X[0,1] = corresponds to data for variables A and B
X[0,2] = corresponds to data for variables A and C
X[1,0] = corresponds to data for variables B and A
X[1,1] = corresponds to data for variables B and B
X[1,2] = corresponds to data for variables B and C
X[2,0] = corresponds to data for variables C and A
X[2,1] = corresponds to data for variables C and B
X[2,2] = corresponds to data for variables C and C
数组 Y:
np.array([[2,12],
[-12, 2]])
Y[0,0] = corresponds to data for variables A and C
Y[0,1] = corresponds to data for variables A and B
Y[1,0] = corresponds to data for variables B and A
Y[1,1] = corresponds to data for variables C and A
阵列 Z:
np.array([[ 99, 77],
[-77, -99]])
Z[0,0] = corresponds to data for variables A and C
Z[0,1] = corresponds to data for variables B and C
Z[1,0] = corresponds to data for variables C and B
Z[1,1] = corresponds to data for variables C and A
我想连接上述数组,保持变量位置固定,如下所示:
END_RESULT_ARRAY index 0 corresponds to variable A
END_RESULT_ARRAY index 1 corresponds to variable B
END_RESULT_ARRAY index 2 corresponds to variable C
基本上,宇宙中有 N 个变量,但每个月都会发生变化(可以引入新变量,现有变量可以退出然后返回或永远不会返回)。在宇宙中的 N 个变量中,我计算排列对并且每个变量的位置是固定的,即索引 0 对应于变量 A,索引 = 1 对应于变量 B(如上所述)。
鉴于上述要求,结束 END_RESULT_ARRAY 应如下所示:
array([[[ 0., 2., 3.],
[ -2., 0., 4.],
[ -3., -4., 0.]],
[[ nan, 12., 2.],
[-12., nan, nan],
[ 2., nan, nan]],
[[ nan, nan, 99.],
[ nan, nan, 77.],
[-99., -77., nan]]])
请记住,上面是一个插图。
在我的实际应用中,我有大约 125 个数组,并且每个月都会生成一个新数组。每个月度数组可能有不同的大小,并且可能只有我的 Universe 中定义的部分变量的数据。此外,由于每个月都会创建新数组,因此无法知道其大小或哪些变量将包含数据(或哪些变量将丢失)。
所以直到最近的每月数组,我们可以从可用的历史数据中确定最大大小。每个月我们都必须重新检查所有数组的最大大小,因为有新数组可用。一旦我们有了最大尺寸,我们就可以将所有数组重新缝合/连接在一起,如果这在numpy
. 这将是每月进行的持续操作。
我想要一个能够将这些数组拼接在一起的通用机制,保持我描述的关于变量索引位置的要求是固定的。
我实际上想使用H5PY
数组,因为我的数据集将在不久的将来呈指数增长。但是,我希望将此numpy
作为第一步。
解决方案
基于@user3483203 的评论。下一步是连接数组。
a = np.array([[ 0., 2., 3.],
[ -2., 0., 4.],
[ -3., -4., 0.]])
b = np.array([[0,12], [-12, 0]])
out = np.full_like(a, np.nan); i, j = b.shape; out[:i, :j] = b
res = np.array([a, out])
print (res)
推荐阅读
- function - Excel function for searching/matching/pasting data from different tables
- ibm-doors - 如何在每个大写字母中插入一个空格字符,除了 DXL 脚本中字符串数组的每个元素的第一个字符?
- css - 如何使用@media print 设置粘性页眉和/或页脚
- assembly - 如何在两个 16 位寄存器相乘后从 DX:AX 中提取内容以进行进一步计算?8086 汇编代码
- python - 在 keras 中使用自定义张量流操作
- android - Program type already present: com.google.android.gms.common.api.zzd
- html - HTML页面单独显示很好,但在角度项目中不显示
- java - MYSQL 的问题
- acumatica - Acumatica - 根据不同的字段将复选框设置为 true
- r - 为开源贡献维护不同版本的 R 包