gpt4 book ai didi

python - 在python中将2d数组转换为3d数组

转载 作者:行者123 更新时间:2023-11-28 21:13:26 25 4
gpt4 key购买 nike

很抱歉问这个问题,如果已经有人问过,但在我的例子中,我有一个大小为 3000000x50 的特殊矩阵,我想将它分成 300 个大小为 10000x50 的矩阵。我试过了,但没用

>>>import numpy as np
>>>data=np.random.randn(3000000,50)
>>>D=np.matrix.conjugate(data)
>>>ts=50
>>>ts=int(ts) #number of time series that we have from our data
>>>lw=1e4
>>>lw=int(lw) #length of each window
>>>l=len(data)/lw #l is number of windows
>>>l=np.floor(l)
>>>l=int(l)
#Dc is used to seperate each time series in l windows
>>>Dc=np.zeros((l,lw,ts))
>>>for i in range(l):
Dc[i][0:lw-1][0:ts-1]=D[(lw)*(i):(lw*(i+1))-1][0:ts-1]

最佳答案

您正在寻找np.vsplit (将数组垂直(按行)拆分为多个子数组)-

np.vsplit(data,300)

sample 运行-

In [56]: data
Out[56]:
array([[ 0.46677419, 0.07402051, 0.87270029, 0.12481164],
[ 0.40789713, 0.36018843, 0.41731607, 0.17348898],
[ 0.4701256 , 0.10056201, 0.31289602, 0.18681709],
[ 0.52407036, 0.89913995, 0.59097535, 0.38376443],
[ 0.06734662, 0.24470334, 0.09523911, 0.35680219],
[ 0.91178257, 0.58710922, 0.75099017, 0.24929987]])

In [57]: np.vsplit(data,3)
Out[57]:
[array([[ 0.46677419, 0.07402051, 0.87270029, 0.12481164],
[ 0.40789713, 0.36018843, 0.41731607, 0.17348898]]),
array([[ 0.4701256 , 0.10056201, 0.31289602, 0.18681709],
[ 0.52407036, 0.89913995, 0.59097535, 0.38376443]]),
array([[ 0.06734662, 0.24470334, 0.09523911, 0.35680219],
[ 0.91178257, 0.58710922, 0.75099017, 0.24929987]])]

根据您打算如何使用输出,您可以将 2D 输入数组重新整形为沿第一个轴长度为 300 的 3D 数组,这在性能和内存方面。内存方面,它必须是免费的,因为 reshaping 只创建 numpy 数组的 View 。实现将是 -

data.reshape(300,-1,data.shape[1])

sample 运行-

In [68]: data
Out[68]:
array([[ 0.46677419, 0.07402051, 0.87270029, 0.12481164],
[ 0.40789713, 0.36018843, 0.41731607, 0.17348898],
[ 0.4701256 , 0.10056201, 0.31289602, 0.18681709],
[ 0.52407036, 0.89913995, 0.59097535, 0.38376443],
[ 0.06734662, 0.24470334, 0.09523911, 0.35680219],
[ 0.91178257, 0.58710922, 0.75099017, 0.24929987]])

In [69]: data.reshape(3,-1,data.shape[1])
Out[69]:
array([[[ 0.46677419, 0.07402051, 0.87270029, 0.12481164],
[ 0.40789713, 0.36018843, 0.41731607, 0.17348898]],

[[ 0.4701256 , 0.10056201, 0.31289602, 0.18681709],
[ 0.52407036, 0.89913995, 0.59097535, 0.38376443]],

[[ 0.06734662, 0.24470334, 0.09523911, 0.35680219],
[ 0.91178257, 0.58710922, 0.75099017, 0.24929987]]])

这里有一些运行时测试,用于检查比较实际拆分与 reshape 的性能 -

In [72]: data = np.random.rand(6000,40)

In [73]: %timeit np.vsplit(data,300)
100 loops, best of 3: 7.05 ms per loop

In [74]: %timeit data.reshape(300,-1,data.shape[1])
1000000 loops, best of 3: 1.08 µs per loop

关于python - 在python中将2d数组转换为3d数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32807267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com