首页 > 解决方案 > 无论如何,我可以记录不规则阵列的形状吗?

问题描述

我有一个包含值的列表:

[array([[-0.44202444, -1.04178747,  1.4362035 , -0.15685013, -0.19853092,
     -0.2987249 ,  0.24365914, -0.10306063, -0.33542501, -0.91320021],
    [-1.56645978,  0.32935671, -0.39181109, -0.83418194,  1.33669395,
      0.52043596, -0.86230729,  1.14529712, -1.32337386, -0.53391686],
    [-2.54651185, -0.53688055, -0.59178218,  0.33979576,  0.10487541,
      0.28045898,  0.00476822, -0.02171904,  1.0531645 , -0.17893486]]),
 array([[-0.72912248],
        [-0.77519337],
        [ 1.26072658],
        [ 1.4535593 ],
        [ 0.51634109],
        [-0.09738144],
        [-1.09359289],
        [-0.81102859],
        [ 0.59387883],
        [-0.1597755 ]])]

我想展平这个数组,改变它,然后把它恢复到原来的形状。对此的澄清是,例如,我从这两个列表中随机选择一个要更改的值,然后将形状重置为上面显示的原始形状。最好的方法是什么?

标签: python

解决方案


import numpy as np

array = np.array

x = [array([[-0.44202444, -1.04178747,  1.4362035 , -0.15685013, -0.19853092,
     -0.2987249 ,  0.24365914, -0.10306063, -0.33542501, -0.91320021],
    [-1.56645978,  0.32935671, -0.39181109, -0.83418194,  1.33669395,
      0.52043596, -0.86230729,  1.14529712, -1.32337386, -0.53391686],
    [-2.54651185, -0.53688055, -0.59178218,  0.33979576,  0.10487541,
      0.28045898,  0.00476822, -0.02171904,  1.0531645 , -0.17893486]]),
 array([[-0.72912248],
        [-0.77519337],
        [ 1.26072658],
        [ 1.4535593 ],
        [ 0.51634109],
        [-0.09738144],
        [-1.09359289],
        [-0.81102859],
        [ 0.59387883],
        [-0.1597755 ]])]

shapes = [a.shape for a in x]

flattened = np.concatenate([a.flatten() for a in x])

new = []
index = 0
for shape in shapes:
    size = np.product(shape)
    new.append(flattened[index : index + size].reshape(shape))
    index += size

print('original:', x)
print('\nflattened:', flattened)
print('\nshapes:', shapes)
print('\nreconstructed:', new)

给出:

original: [array([[-0.44202444, -1.04178747,  1.4362035 , -0.15685013, -0.19853092,
        -0.2987249 ,  0.24365914, -0.10306063, -0.33542501, -0.91320021],
       [-1.56645978,  0.32935671, -0.39181109, -0.83418194,  1.33669395,
         0.52043596, -0.86230729,  1.14529712, -1.32337386, -0.53391686],
       [-2.54651185, -0.53688055, -0.59178218,  0.33979576,  0.10487541,
         0.28045898,  0.00476822, -0.02171904,  1.0531645 , -0.17893486]]), array([[-0.72912248],
       [-0.77519337],
       [ 1.26072658],
       [ 1.4535593 ],
       [ 0.51634109],
       [-0.09738144],
       [-1.09359289],
       [-0.81102859],
       [ 0.59387883],
       [-0.1597755 ]])]

flattened: [-0.44202444 -1.04178747  1.4362035  -0.15685013 -0.19853092 -0.2987249
  0.24365914 -0.10306063 -0.33542501 -0.91320021 -1.56645978  0.32935671
 -0.39181109 -0.83418194  1.33669395  0.52043596 -0.86230729  1.14529712
 -1.32337386 -0.53391686 -2.54651185 -0.53688055 -0.59178218  0.33979576
  0.10487541  0.28045898  0.00476822 -0.02171904  1.0531645  -0.17893486
 -0.72912248 -0.77519337  1.26072658  1.4535593   0.51634109 -0.09738144
 -1.09359289 -0.81102859  0.59387883 -0.1597755 ]

shapes: [(3, 10), (10, 1)]

reconstructed: [array([[-0.44202444, -1.04178747,  1.4362035 , -0.15685013, -0.19853092,
        -0.2987249 ,  0.24365914, -0.10306063, -0.33542501, -0.91320021],
       [-1.56645978,  0.32935671, -0.39181109, -0.83418194,  1.33669395,
         0.52043596, -0.86230729,  1.14529712, -1.32337386, -0.53391686],
       [-2.54651185, -0.53688055, -0.59178218,  0.33979576,  0.10487541,
         0.28045898,  0.00476822, -0.02171904,  1.0531645 , -0.17893486]]), array([[-0.72912248],
       [-0.77519337],
       [ 1.26072658],
       [ 1.4535593 ],
       [ 0.51634109],
       [-0.09738144],
       [-1.09359289],
       [-0.81102859],
       [ 0.59387883],
       [-0.1597755 ]])]

推荐阅读