Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Flattening | Important Functions
NumPy in a Nutshell
course content

Course Content

NumPy in a Nutshell

NumPy in a Nutshell

1. Getting Started with NumPy
2. Dimensions in Arrays
3. Indexing and Slicing
4. Important Functions

Flattening

Do you know what it means to flatten an array? Flattening is the process of transforming a multidimensional array into a one-dimensional one.

This transformation can be achieved using two different methods:

  • the first one we're already familiar with is the .reshape(-1) method with an argument of -1;
  • the other option is to use the .flatten() method.

Now, let's have a look at both of these methods in practice.

Let's see how to use the .reshape(-1) method:

123456
import numpy as np array = np.array([[12, 45, 78, 34, 0], [13, 5, 78, 3, 1]]) new_array = array.reshape(-1) print(new_array)
copy

Let's see how to use the .flatten() method:

123456
import numpy as np array = np.array([[12, 45, 78, 34, 0], [13, 5, 78, 3, 1]]) new_array = array.flatten() print(new_array)
copy

Let's practice!

Task

Consider the following array:

[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]]

You should transform it into the following array:

[1 2 3 4 5 6 7 8 9 10 11 12].

Task

Consider the following array:

[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]]

You should transform it into the following array:

[1 2 3 4 5 6 7 8 9 10 11 12].

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 4. Chapter 2
toggle bottom row

Flattening

Do you know what it means to flatten an array? Flattening is the process of transforming a multidimensional array into a one-dimensional one.

This transformation can be achieved using two different methods:

  • the first one we're already familiar with is the .reshape(-1) method with an argument of -1;
  • the other option is to use the .flatten() method.

Now, let's have a look at both of these methods in practice.

Let's see how to use the .reshape(-1) method:

123456
import numpy as np array = np.array([[12, 45, 78, 34, 0], [13, 5, 78, 3, 1]]) new_array = array.reshape(-1) print(new_array)
copy

Let's see how to use the .flatten() method:

123456
import numpy as np array = np.array([[12, 45, 78, 34, 0], [13, 5, 78, 3, 1]]) new_array = array.flatten() print(new_array)
copy

Let's practice!

Task

Consider the following array:

[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]]

You should transform it into the following array:

[1 2 3 4 5 6 7 8 9 10 11 12].

Task

Consider the following array:

[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]]

You should transform it into the following array:

[1 2 3 4 5 6 7 8 9 10 11 12].

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 4. Chapter 2
toggle bottom row

Flattening

Do you know what it means to flatten an array? Flattening is the process of transforming a multidimensional array into a one-dimensional one.

This transformation can be achieved using two different methods:

  • the first one we're already familiar with is the .reshape(-1) method with an argument of -1;
  • the other option is to use the .flatten() method.

Now, let's have a look at both of these methods in practice.

Let's see how to use the .reshape(-1) method:

123456
import numpy as np array = np.array([[12, 45, 78, 34, 0], [13, 5, 78, 3, 1]]) new_array = array.reshape(-1) print(new_array)
copy

Let's see how to use the .flatten() method:

123456
import numpy as np array = np.array([[12, 45, 78, 34, 0], [13, 5, 78, 3, 1]]) new_array = array.flatten() print(new_array)
copy

Let's practice!

Task

Consider the following array:

[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]]

You should transform it into the following array:

[1 2 3 4 5 6 7 8 9 10 11 12].

Task

Consider the following array:

[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]]

You should transform it into the following array:

[1 2 3 4 5 6 7 8 9 10 11 12].

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Do you know what it means to flatten an array? Flattening is the process of transforming a multidimensional array into a one-dimensional one.

This transformation can be achieved using two different methods:

  • the first one we're already familiar with is the .reshape(-1) method with an argument of -1;
  • the other option is to use the .flatten() method.

Now, let's have a look at both of these methods in practice.

Let's see how to use the .reshape(-1) method:

123456
import numpy as np array = np.array([[12, 45, 78, 34, 0], [13, 5, 78, 3, 1]]) new_array = array.reshape(-1) print(new_array)
copy

Let's see how to use the .flatten() method:

123456
import numpy as np array = np.array([[12, 45, 78, 34, 0], [13, 5, 78, 3, 1]]) new_array = array.flatten() print(new_array)
copy

Let's practice!

Task

Consider the following array:

[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]]

You should transform it into the following array:

[1 2 3 4 5 6 7 8 9 10 11 12].

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 2
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt