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

时间:2015-09-27 11:08:59

标签: 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)       #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]

2 个答案:

答案 0 :(得分:4)

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

np.vsplit(data,300)

示例运行 -

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数组,这在性能方面必须更加高效和记忆。在Memorywise它必须是空闲的,因为reshaping只创建numpy数组的视图。实施将是 -

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

示例运行 -

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

答案 1 :(得分:2)

如果您的初始数组已正确排序,并且您想要将数组拆分为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