BUG? : np.ravel does not flatten out certain arrays
See original GitHub issueFor arrays in which the number of elements are not the same along the inner axes, np.ravel
and np.flatten
return the array as it is. I’m not sure if this is intended behavior or not.
In [23]: a = np.ravel([[1, 2], [3, 4, 5]])
In [24]: a
Out[24]: array([[1, 2], [3, 4, 5]], dtype=object)
I thought of giving this as a shot, but I’m not sure what part of the source I should modify, the ravel function just says asarray(a).ravel()
and I was not able to figure out (quickly) where the ravel methods for the ndarray is.
Issue Analytics
- State:
- Created 9 years ago
- Reactions:1
- Comments:14 (10 by maintainers)
Top Results From Across the Web
Python numpy ravel function not flattening array - Stack Overflow
Well you created an array of objects, not a 2d array. Hence it is flattening since for numpy the objects are the items...
Read more >Array Manipulation | flatten and ravel | Python Programming
... video tutorial you will learn about array manipulation in detail. Here We will discuss how to flatten array with example. NumPy is......
Read more >numpy.ravel — NumPy v1.24 Manual
Return a contiguous flattened array. A 1-D array, containing the elements of the input, is returned. A copy is made only if needed....
Read more >How to Flatten a NumPy Array - Finxter
To flatten any NumPy array to a one-dimensional array, use the array.flatten() method that returns a new flattened 1D array. Here's a simple...
Read more >Flatten a NumPy array with ravel() and flatten() - nkmk note
You can flatten a NumPy array ndarray with the numpy.label() function, or the ravel() and flatten() methods of numpy.ndarray .
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
NumPy is not designed to handle arrays of lists of arbitrary length. While you can use the numpy hammer for this bowl of pudding you will end up just making a mess and it will not be faster than using a spoon. My advice would be to avoid numpy altogether:
I find
sum(z,[])
to be the fastest as well. It takes about 1/2 of the time forlist(itertools.chain(*z))
and 1/3 of the time for[x for y in z for x in y]
, using %timeit magic command. The data type in my list of lists is strings only.