Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flattening a list of NumPy arrays?

It appears that I have data in the format of a list of NumPy arrays (type() = np.ndarray):

[array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]),  array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]),  array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]),  array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]])] 

I am trying to put this into a polyfit function:

m1 = np.polyfit(x, y, deg=2) 

However, it returns the error: TypeError: expected 1D vector for x

I assume I need to flatten my data into something like:

[0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654 ...] 

I have tried a list comprehension which usually works on lists of lists, but this as expected has not worked:

[val for sublist in risks for val in sublist] 

What would be the best way to do this?

like image 520
Jerry Zhang Avatar asked Nov 14 '15 18:11

Jerry Zhang


People also ask

Can I flatten a list in Python?

You can flatten a Python list using a list comprehension, a nested for loop, and the itertools. chain() method. The list comprehension is the most “Pythonic” method and is therefore favoured in most cases. While nested for loops are effective, they consume more lines of code than a list comprehension.

How do you flatten a list inside a list Python?

Flatten List of Lists Using itertools (chain()) This approach is ideal for transforming a 2-D list into a single flat list as it treats consecutive sequences as a single sequence by iterating through the iterable passed as the argument in a sequential manner.

How do you flatten an array in Python?

By using ndarray. flatten() function we can flatten a matrix to one dimension in python. order:'C' means to flatten in row-major. 'F' means to flatten in column-major.


1 Answers

You could use numpy.concatenate, which as the name suggests, basically concatenates all the elements of such an input list into a single NumPy array, like so -

import numpy as np out = np.concatenate(input_list).ravel() 

If you wish the final output to be a list, you can extend the solution, like so -

out = np.concatenate(input_list).ravel().tolist() 

Sample run -

In [24]: input_list Out[24]:  [array([[ 0.00353654]]),  array([[ 0.00353654]]),  array([[ 0.00353654]]),  array([[ 0.00353654]]),  array([[ 0.00353654]]),  array([[ 0.00353654]]),  array([[ 0.00353654]]),  array([[ 0.00353654]]),  array([[ 0.00353654]]),  array([[ 0.00353654]]),  array([[ 0.00353654]]),  array([[ 0.00353654]]),  array([[ 0.00353654]])]  In [25]: np.concatenate(input_list).ravel() Out[25]:  array([ 0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,         0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,         0.00353654,  0.00353654,  0.00353654]) 

Convert to list -

In [26]: np.concatenate(input_list).ravel().tolist() Out[26]:  [0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654,  0.00353654] 
like image 58
Divakar Avatar answered Oct 03 '22 04:10

Divakar