python中二维数组到三维数组的转换
很抱歉问这个问题,如果已经问过的话,但是在我的例子中,我有一个大小为3000000x50的特殊矩阵,我想将其拆分为300个大小为10000x50的矩阵。我试过了,但不起作用python中二维数组到三维数组的转换,python,arrays,numpy,Python,Arrays,Numpy,很抱歉问这个问题,如果已经问过的话,但是在我的例子中,我有一个大小为3000000x50的特殊矩阵,我想将其拆分为300个大小为10000x50的矩阵。我试过了,但不起作用 >>>import numpy as np >>>data=np.random.randn(3000000,50) >>>D=np.matrix.conjugate(data) >>>ts=50 >>>ts=int(ts)
>>>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]
您正在查找(将数组垂直(按行)拆分为多个子数组)-
样本运行-
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]])]
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]]])
根据您将如何使用输出,您可以将2D输入阵列重塑为沿第一轴长度
300
的3D阵列,这在性能和内存方面必须更高效。Memorywise它必须是自由的,因为重塑
只创建numpy数组的一个视图。执行工作将是:-
data.reshape(300,-1,data.shape[1])
样本运行-
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]])]
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]]])
这里有一些运行时测试来检查性能,比较实际拆分和重塑-
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
如果您的初始数组已正确排序,并且您希望将数组拆分为300个矩阵“框”,那么您只需要对marix进行以下重新定义
import numpy as np
data = np.random.randn(3000000,50)
newData = data.reshape(300,10000,50) # This is as [300,10000,50] array
print newData[0,...] # Show the first matrix, 1 of 300
为什么不使用
np.split()
:new\u array=np.split(D,300)
读者请使用重塑
部分而不是(v)split
@askewchan完全同意这一点!谢谢你的回答,这对我帮助很大。比较测试非常有帮助。vsplit需要一个无余数的均匀分割。所有子体的大小必须相同