【发布时间】:2019-10-09 06:04:21
【问题描述】:
对于numpy stacks 和linear algebra,您希望使用第一轴上的堆栈来格式化数据。
例如,要使用determinant,参数需要是一个数组,其中最后两个轴是对称的,例如(x,M,M)
如何将四个单独的扁平数组(对应于 2x2 矩阵的系数)重新格式化为这样的格式?
我一直在修补 https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html#numpy.concatenate、https://docs.scipy.org/doc/numpy/reference/generated/numpy.block.html#numpy.block 和 https://docs.scipy.org/doc/numpy/reference/generated/numpy.dstack.html,
但到目前为止还没有把它变成我认为我需要的格式。
例如,我非常接近
a = np.array([1, 2, 3, 4, 5])
b = np.array([10, 20, 30, 40, 50])
c = np.array([15, 16, 17, 18, 19])
d = np.array([100, 200, 300, 400, 500])
result = np.dstack((a, b, c, d))
result = np.reshape(result, (1, 5, 2, 2))
print("RESULT SHAPE", result.shape)
print("RESULT VALUE", result[:, :, 0, 0])
print("RESULT I REALLY WANT", result[:, 0, 0])
不确定如何删除最后一个轴。
【问题讨论】:
-
等等,我现在感觉很笨。我想我可以这样做:
new_result = np.reshape(result, (-1, 2, 2))有没有比顺序执行此操作更简单的方法?
标签: python numpy multidimensional-array