我在Python中使用np.dstack来处理3D中的堆栈数组,但形状看起来像这样
a = np.sin(np.cumsum((np.random.normal(scale=0.1, size=(len(x), 12))), axis=0))
b = np.dstack((nlead1,nlead2,nlead3,nlead4,nlead5,nlead6,nlead7,nlead8,nlead9,nlead10,nlead11,nlead12))
print a
print a.shape
print b
print b.shape
输出看起来像
[[ 0.16172117 -0.06113749 -0.05279262 ..., -0.02493445 0.14569041
-0.20455989]
[ 0.12982533 -0.09941658 0.06052276 ..., 0.08941575 0.23417498
-0.15513552]
[ 0.21850132 -0.16716535 0.048252 ..., 0.00501475 0.16843843
-0.15638653]
...,
[ 0.6436674 0.04253489 -0.58825104 ..., -0.95674562 0.36004254
0.41308711]
[ 0.6193386 -0.00224555 -0.56129994 ..., -0.90884016 0.31269762
0.26443869]
[ 0.63100264 -0.03888419 -0.61395087 ..., -0.92910613 0.42215252
0.1646122 ]]
(5500, 12)
[[[ -39 47 86 ..., -100 -77 -3]
[ -34 50 85 ..., -101 -75 -1]
[ -36 49 86 ..., -103 -77 -4]
...,
[ 0 0 0 ..., 0 0 0]
[ 0 0 0 ..., 0 0 0]
[ 0 0 0 ..., 0 0 0]]]
(1, 5500, 12)
我想让b的形状像a一样,你能给我一些建议非常感谢你。
答案 0 :(得分:1)
您可以squeeze
数组,删除单项维度:
b = np.squeeze(b)
虽然,只是连接并采用转置效率更高:
print(timeit.timeit(lambda: np.concatenate(arrays,axis=0).T, number=100))
print(timeit.timeit(lambda: np.squeeze(np.dstack(arrays)), number=100))
# 0.024745813333333335
# 0.07166506666666667