NumPy Shape Manipulation

NumPyNumPyBeginner
Practice Now

Introduction

In this lab, you will learn the NumPy shape manipulation functions that allow you to manipulate the shape of NumPy arrays.

Achievements

  • Reshaping arrays
  • Concatenating and splitting arrays
  • Transposing arrays

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL numpy(("`NumPy`")) -.-> numpy/ArrayManipulationGroup(["`Array Manipulation`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) numpy/ArrayManipulationGroup -.-> numpy/reshape("`Reshape`") numpy/ArrayManipulationGroup -.-> numpy/split("`Split`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/multi_array("`Multi-dimensional Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/ArrayBasicsGroup -.-> numpy/shape_dim("`Shapes and Dimensions`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") numpy/ArrayManipulationGroup -.-> numpy/merge("`Merge`") subgraph Lab Skills numpy/reshape -.-> lab-214{{"`NumPy Shape Manipulation`"}} numpy/split -.-> lab-214{{"`NumPy Shape Manipulation`"}} python/lists -.-> lab-214{{"`NumPy Shape Manipulation`"}} python/tuples -.-> lab-214{{"`NumPy Shape Manipulation`"}} python/importing_modules -.-> lab-214{{"`NumPy Shape Manipulation`"}} python/numerical_computing -.-> lab-214{{"`NumPy Shape Manipulation`"}} python/build_in_functions -.-> lab-214{{"`NumPy Shape Manipulation`"}} numpy/1d_array -.-> lab-214{{"`NumPy Shape Manipulation`"}} numpy/multi_array -.-> lab-214{{"`NumPy Shape Manipulation`"}} numpy/data_array -.-> lab-214{{"`NumPy Shape Manipulation`"}} numpy/shape_dim -.-> lab-214{{"`NumPy Shape Manipulation`"}} numpy/bool_idx -.-> lab-214{{"`NumPy Shape Manipulation`"}} numpy/fancy_idx -.-> lab-214{{"`NumPy Shape Manipulation`"}} numpy/merge -.-> lab-214{{"`NumPy Shape Manipulation`"}} end

Reshaping Arrays

The reshape function allows you to change the shape of a NumPy array. The syntax of the reshape function is as follows:

np.reshape(a, new_shape)
  • where a is the input array and new_shape is the desired new shape of the array.

Open the Python Shell

Open the Python shell by typing the following command in the terminal.

python3

Import NumPy

NumPy is already installed, you can import it in your Python code:

import numpy as np

Create an Array

Create an array a of shape (2, 3) as an example:

a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape)

Output:

(2, 3)

Using Reshape

You can reshape this array to a shape of (3, 2) using the reshape function:

b = np.reshape(a, (3, 2))
print(b.shape)
print(b)

Output:

(3, 2)
[[1 2]
 [3 4]
 [5 6]]

Concatenating and Splitting Arrays

NumPy provides two functions for concatenating arrays:

  1. np.concatenate: for concatenating arrays along a given axis
  2. np.stack: for concatenating arrays along a new axis

You can split arrays using the np.split function.

Concatenating Arrays

Create two arrays a and b as an example:

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

Using Concatenate

You can concatenate these arrays along the first axis(0) using the np.concatenate function:

c = np.concatenate((a, b))
print(c)

Output:

[1 2 3 4 5 6]

Using Stack

You can also concatenate these arrays along a new axis using the np.stack function:

d = np.stack((a, b))
print(d)
print(d.shape)

Output:

[[1 2 3]
 [4 5 6]]
(2, 3)

Splitting Arrays

Create an array a of shape (6,) as an example:

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

Using Split

You can split this array into two arrays of length 3 using the np.split function:

b, c = np.split(a, 2)
print(b)
print(c)

Output:

[1 2 3]
[4 5 6]

Transposing Arrays

The transpose function allows you to transpose the axes of a NumPy array. The syntax of the transpose function is as follows:

a.transpose([axis1, axis2, ...])
  • where axis1, axis2, etc. are the axes to be transposed.

Create an Array

create an array a of shape (2, 3) as an example:

a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print(a.shape)

Output:

[[1 2 3]
 [4 5 6]]
(2, 3)

Using Transpose

You can transpose this array using the transpose function:

b = a.transpose()
print(b)
print(b.shape)

Output:

[[1 4]
 [2 5]
 [3 6]]
(3, 2)

You can also transpose specific axes of the array. For example, you can transpose the axes of the array a to have a shape of (3, 2) using the following code:

c = a.transpose(1, 0)
print(c)
print(c.shape)

Output:

[[1 4]
 [2 5]
 [3 6]]
(3, 2)

Summary

Congratulations! You have completed the NumPy Shape Manipulation Lab.

In this lab, you learned the NumPy shape manipulation functions reshape, concatenate, stack, split, and transpose. These functions allow you to manipulate the shape of NumPy arrays and are essential for many data manipulation tasks.

Other NumPy Tutorials you may like