Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy list of 1D Arrays to 2D Array

I have a large list files that contain 2D numpy arrays pickled through numpy.save. I am trying to read the first column of each file and create a new 2D array.

I currently read each column using numpy.load with a mmap. The 1D arrays are now in a list.

col_list = [] for f in file_list:     Temp = np.load(f,mmap_mode='r')     col_list.append(Temp[:,0]) 

How can I convert this into a 2D array?

like image 271
David Folkner Avatar asked Jan 24 '14 01:01

David Folkner


People also ask

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

Use reshape() Function to Transform 1d Array to 2d Array The number of components within every dimension defines the form of the array. We may add or delete parameters or adjust the number of items within every dimension by using reshaping. To modify the layout of a NumPy ndarray, we will be using the reshape() method.

How will you change the following 1D list into a 2D NumPy array as shown below?

reshape which is used to convert a 1-D array into a 2-D array of required dimensions (n x m). This function gives a new required shape without changing the data of the 1-D array. Parameters: array: is the given 1-D array that will be given a new shape or converted into 2-D array.

How do you convert a 1D array of tuples to a 2D NumPy array?

How to convert a 1d array of tuples to a 2d numpy array? Yes it is possible to convert a 1 dimensional numpy array to a 2 dimensional numpy array, by using "np. reshape()" this function we can achiev this.


2 Answers

You can use

numpy.stack(arrays, axis=0) 

if you have an array of arrays. You can specify the axis in case you want to stack columns and not rows.

like image 157
C. Yduqoli Avatar answered Oct 14 '22 19:10

C. Yduqoli


The array may be recreated:

a = np.array(a.tolist()) 
like image 29
splendor Avatar answered Oct 14 '22 21:10

splendor