Python 多个阵列的numpy布局
我们想布局一个由n个子数组组成的新数组 例如:将三个numpy数组(x、y、z)放置到此结果数组中:Python 多个阵列的numpy布局,python,arrays,layout,numpy,Python,Arrays,Layout,Numpy,我们想布局一个由n个子数组组成的新数组 例如:将三个numpy数组(x、y、z)放置到此结果数组中: +---+---+ | | | | | Y | | X | | | +---+ | | Z | +---+---+ 我们知道连接,但我们希望使用更通用的方法。缺少的部分应使用可指定的默认值进行填充。我不知道numpy.tile、numpy.expand\u dims或类似的选项是否更容易实现,但下面的方法可以实现: import numpy as np # examp
+---+---+
| | |
| | Y |
| X | |
| +---+
| | Z |
+---+---+
我们知道连接,但我们希望使用更通用的方法。缺少的部分应使用可指定的默认值进行填充。我不知道
numpy.tile
、numpy.expand\u dims
或类似的选项是否更容易实现,但下面的方法可以实现:
import numpy as np
# example arrays
x = np.ones((5,3))
# [[ 1. 1. 1.]
# [ 1. 1. 1.]
# [ 1. 1. 1.]
# [ 1. 1. 1.]
# [ 1. 1. 1.]]
y = 2*np.ones((3,3))
# [[ 2. 2. 2.]
# [ 2. 2. 2.]
# [ 2. 2. 2.]]
z = 3*np.ones((2,2))
# [[ 3. 3.]
# [ 3. 3.]]
# calculate shape of resulting array
res_shape = (x.shape[0], max(x.shape[1] + y.shape[1], x.shape[1] + z.shape[1]))
# create result array and set "specifiable default value", here zero, or using np.ones as above
res = np.zeros(res_shape)
# insert subarrays into result array
res[:x.shape[0],:x.shape[1]] = x
res[:y.shape[0],x.shape[1]:x.shape[1]+y.shape[1]] = y
res[y.shape[0]:y.shape[0]+z.shape[0],x.shape[1]:x.shape[1]+z.shape[1]] = z
# array([[ 1., 1., 1., 2., 2., 2.],
# [ 1., 1., 1., 2., 2., 2.],
# [ 1., 1., 1., 2., 2., 2.],
# [ 1., 1., 1., 3., 3., 0.],
# [ 1., 1., 1., 3., 3., 0.]])
除非你限制你的要求太多太多,否则在我看来,你是在寻求解决问题的办法。祝你好运如果解决方案适合您,请让其他用户查看是否有解决方案。非常感谢。