Skip to content

Flatten an Array with NumPy flatten

Flatten an Array with NumPy flatten cover image

In this tutorial, you’ll learn how to flatten an array with NumPy flatten function, meaning that an array is collapsed to a single dimension. The NumPy flatten function allows you to turn a multi-dimensional array into a single-dimensional array. The function allows you to easily flatten arrays in different ways, including column-wise and row-wise.

By the end of this tutorial, you’ll have learned:

  • How to flatten a NumPy array row-wise
  • How to flatten a NumPy array column-wise

Understanding the NumPy flatten Function

Before diving into how to use the NumPy flatten function, let’s take a look at how the function works. Compared to many other NumPy functions, this function is relatively straightforward.

The method takes a single, optional parameter, order=, which determines the way in which the array is flattened. Let’s take a look at the definition of the method:

# Understanding the NumPy Flatten Method
import numpy as np
ndarray.flatten(order='C')

In the code above, we can see that the method is applied to an array. The method returns a copy of the input array, flattened to a single dimension.

Let’s quickly see what options are available for the order= parameter:

  • 'C', the default argument, flattens the array row-major (C-style)
  • 'F', flattens the array column-wise (Fortran-style)
  • 'A', flattens in column-major order if the array is contiguous in memory
  • 'K', flattens the array in the order the elements occur in memory

In the following section, you’ll learn how to flatten NumPy arrays row-wise using the NumPy flatten function.

Flatten a NumPy Array Row-Wise

In this section, you’ll learn how to use the NumPy flatten method to flatten an array row-wise. This means that items are appended based on the arrays in which they appear. In order to flatten arrays row-wise, you can use the default argument of order='C'.

Let’s see how you can flatten a NumPy array row-wise:

# Flatten an Array Row-Wise
import numpy as np

arr = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

flattened = arr.flatten()
print(flattened)

# Returns: [1 2 3 4 5 6]

Let’s break down what we did in the code block above:

  1. We imported NumPy as np
  2. We then created a two-dimensional array, arr
  3. We then created a new array flattened, which is the result of applying the .flatten() method to the array

It’s important to note that the method does not modify the original array, but returns a flattened copy of the array.

Flatten a NumPy Array Column-Wise

In this section, you’ll learn how to flatten a NumPy array column-wise, meaning that items are flattened in the order in which they appear. This means that the first item of each array is flattened, then the second, and so on.

In order to flatten a NumPy array column-wise, we can pass in the argument of order='F' to the flatten method:

# Flatten an Array Column-Wise
import numpy as np

arr = np.array([
     [1, 2, 3],
     [4, 5, 6]
])

flattened = arr.flatten('F')
print(flattened)

# Returns: [1 4 2 5 3 6]

In the following section, you’ll learn how to use NumPy to flatten a Python list of lists using the NumPy flatten method.

Flatten a Python List of Lists with NumPy Flatten

The NumPy flatten method can also be used to flatten a Python list of lists. The biggest limitation here is that because NumPy arrays can only contain numeric values, this cannot be used for all types of lists of lists.

Let’s see how we can use the flatten method to flatten a Python list of lists:

# Flatten a List of Lists with NumPy flatten()
import numpy as np

list_of_lists = [
    [1, 2, 3],
    [4, 5, 6]
]

flattened = np.array(list_of_lists).flatten()
print(list(flattened))

# Returns: [1, 2, 3, 4, 5, 6]

Similarly, you can use different arguments to modify the way in which these lists of lists are flattened using NumPy.

Frequently Asked Questions

Why doesn’t NumPy flatten work?

The NumPy flatten method doesn’t modify an array, but rather returns a copy of the array. This means that you need to assign the copy to a new variable.

How is NumPy flatten different from NumPy ravel and reshape?

Both the ravel() and reshape() methods return a view, if possible. Meanwhile, the flatten() method returns a copy.

Is NumPy flatten faster than NumPy ravel?

The NumPy ravel() method is almost always faster than NumPy flatten(). This is because by creating a copy, Python will need to first allocate new memory.

Conclusion

In this tutorial, you learned how to use the NumPy flatten() method. The method allows you to flatten a multi-dimensional array into a single-dimensional array. You first learned how the method is defined and what the available parameter is. Then, you learned how to use the flatten method to flatten an array column-wise and row-wise. Finally, you learned how to use the method to flatten a list of lists.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

Your email address will not be published. Required fields are marked *