[ Web Proxy ]
URL:
Viewing: https://www.pythontutorial.net/python-numpy/numpy-flatten/ [Back]  [Original]

NumPy flatten()
Skip to content

NumPy flatten()

Summary: in this tutorial, you’ll learn how to use the numpy flatten() method to return a copy of an array collapsed into one dimension.

Introduction to the NumPy flatten() method #

The flatten() is a method of the ndarray class. The flatten() method returns a copy of an array collapsed into one dimension.

The following shows the syntax of the flatten() method:

ndarray.flatten(order='C')Code language: Python (python)

The order parameter specifies the order of elements of an array in the returned array. It accepts one of the following values:

By default, the order is ‘C’ which flattens the array elements into row-major.

NumPy flatten() method examples #

Let’s take some examples of using the NumPy flatten() method.

1) Using flatten() method example with a multidimensional array #

The following example uses the flatten() method to return a 1-D array from a 2-D array:

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = a.flatten()
print(b)Code language: Python (python)

Output:

[1 2 3 4]Code language: Python (python)

How it works.

NumPy flatten [NumPy flatten]

First, create a 2-D array that has two rows and two columns:

a = np.array([[1, 2], [3, 4]])Code language: Python (python)

Second, return a copy of the array with dimensions collapsed into one using the flatten() method:

b = a.flatten()Code language: Python (python)

Third, display the result array:

print(b)Code language: Python (python)

Note that b is a copy, not a view of the array a. If you change elements in array b, the elements in array a are not changed. For example:

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = a.flatten()

# change element at index 0
b[0] = 0
print(b)

# display the array a
print(a)Code language: Python (python)

Output:

[0 2 3 4]
[[1 2]
 [3 4]]Code language: Python (python)

In this example:

First, flatten the array a and assign the result array to b variable:

b = a.flatten()Code language: Python (python)

Second, change the element at index 0 of b to zero and print out b:

b[0] = 0
print(b)Code language: Python (python)

Third, display the array a:

print(a)Code language: Python (python)

The output shows that the element at index 0 of b changes but the element at index 0 of a doesn’t change.

2) Using numpy flatten() method to flatten an array using column-major order #

The following example uses the numpy flatten() method to flatten an array using column-major order:

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = a.flatten(order='F')

print(b)Code language: Python (python)
numpy flatten column-major order F [numpy flatten column-major order F]

Output:

[1 3 2 4]Code language: Python (python)

Summary #

Was this tutorial helpful ?
Yes No
Search …:

Getting Started

Creating Arrays

Array Indexing & Slicing

Aggregate Functions

Array Operations

Arithmetic Operations

Joining & Splitting Arrays

Copyright © 2021 - Present, By Python Tutorial. All Rights Reserved.


Web Proxy Viewer  |  New URL  |  Original Page