python - 无论如何,我可以记录不规则阵列的形状吗?
问题描述
我有一个包含值的列表:
[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 ]])]
我想展平这个数组,改变它,然后把它恢复到原来的形状。对此的澄清是,例如,我从这两个列表中随机选择一个要更改的值,然后将形状重置为上面显示的原始形状。最好的方法是什么?
解决方案
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 ]])]
推荐阅读
- r - 如何使用 tidycensus 和 tidyverse 准确汇总子组误差值?
- python - @reify 每次调用都会执行数据库查询?
- docker - 如何将 web 应用程序(在我的云上)安装到客户端的本地?
- android - 真正的横幅广告不会出现在我的应用程序的列表视图底部
- oop - 如何在班级工作人员中修复我的方法 toString?
- questdb - 如何计算运行 QuestDB 的硬件容量?
- javascript - 如何在反应中获取嵌套数据
- lua - 如何在斐波那契球体的点之间绘制三角形?
- flutter - 我应该关闭 BehaviorSubject
- javascript - 将键盘事件应用于输入元素