在下面的代码中,我正在使用嵌套列表构建数据。在for循环之后,我想要将其转换为多维Numpy数组,使其尽可能整洁。但是,当我对它进行数组转换时,似乎只有外部列表被转换为数组。更糟糕的是,当我继续向下走时,我会得到形状为
(100L,)的数据点...每个列表都是我的数据(显然,我想要一个(100,3))。我也尝试了numpy.asanyarray()
,但是我似乎无法解决它。如果可能的话,我真的希望从一开始就从我的三维列表中获得3D数组。如果不行,如何在不必迭代并全部转换它们的情况下将列表数组转换为2D数组?
编辑:如果可以使处理更容易,我也可以从一开始就更好地构造数据结构。但是,它通过串行端口传输,大小事先未知。
import numpy as np
import time
data = []
for _i in range(100): #build some list of lists
d = [np.random.rand(), np.random.rand(), np.random.rand()]
data.append([d,time.clock()])
dataArray = np.array(data) #now I have an array of lists of a list(of data) and a time
dataPoints = dataArray[:,0] #this is the data in an array of lists