question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG? : np.ravel does not flatten out certain arrays

See original GitHub issue

For 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:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
mattipcommented, Jun 28, 2018

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:

lst = [[1, 2], [3, 4, 5]]
flat = [val for sublist in lst for val in sublist]
0reactions
cody-wancommented, Sep 15, 2020

I find sum(z,[]) to be the fastest as well. It takes about 1/2 of the time for list(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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found