Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting nested lists of data into multidimensional Numpy arrays

In the code below I am building data up in a nested list. After the for loop what I would like is to cast it into a multidimensional Numpy array as neatly as possible. However, when I do the array conversion on it, it only seems to convert the outer list into an array. Even worse when I continue downward I wind up with dataPoints as shape (100L,)...so an array of lists where each list is my data (obviously I wanted a (100,3)). I have tried fooling with numpy.asanyarray() also but I can't seem to work it out. I would really like a 3d array from my 3d list from the outset if that is possible. If not, how can I get the array of lists into a 2d array without having to iterate and convert them all?

Edit: I am also open to better way of structuring the data from the outset if it makes processing easier. However, it is coming over a serial port and the size is not known beforehand.

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
like image 284
Matt Avatar asked Dec 06 '12 15:12

Matt


People also ask

How do you convert a 2D list to a 2D array?

Use numpy. array() to convert a 2D list into a NumPy array. Call numpy. array(object) with object as a 2D list to convert object into a NumPy array .


1 Answers

dataPoints is not a 2d list. Convert it first into a 2d list and then it will work:

d=np.array(dataPoints.tolist())

Now d is (100,3) as you wanted.

like image 54
Bitwise Avatar answered Oct 23 '22 15:10

Bitwise