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
'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
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:
- We imported NumPy as
- We then created a two-dimensional array,
- 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
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.
Both the ravel() and reshape() methods return a view, if possible. Meanwhile, the flatten() method returns a copy.
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.
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.
To learn more about related topics, check out the tutorials below: