
NumPy is a powerful library for working with arrays of data in Python. One of the key features of NumPy is its array manipulation functions, which enable you to perform a wide variety of operations on arrays to reshape, slice, and manipulate the data in them.
- NumPy Array Concatenation
- NumPy Array Splitting
- NumPy Array Resizing
- NumPy Array Broadcasting
- NumPy Array Input/Output
In this tutorial, we will explore some of the most useful NumPy array manipulation techniques, including:
- Reshaping arrays: You will learn how to use the
reshape
function to change the shape of an array, and theresize
function to change the size of an array. - Slicing arrays: You will learn how to use array slicing to access specific elements or subarrays of an array.
- Splitting and stacking arrays: You will learn how to use the
split
,hsplit
,vsplit
,concatenate
,hstack
, andvstack
functions to split and stack arrays in different ways. - Flattening arrays: You will learn how to use the
flatten
andravel
functions to convert multi-dimensional arrays into 1D arrays.
By the end of this tutorial, you will have a solid understanding of how to manipulate NumPy arrays to suit your needs. Let’s get started!
NumPy Array Concatenation
NumPy provides several functions for concatenating arrays, including concatenate
, hstack
, and vstack
.
There are many reasons why you might want to concatenate arrays. Some common use cases include:
- Merging data from multiple sources: You might have data stored in multiple arrays that you want to combine into a single array for further analysis or processing.
- Increasing the size of an array: You might want to add new elements to an existing array to increase its size.
- Changing the shape of an array: You might want to concatenate arrays along a specific axis to change the shape of an array. For example, you might want to concatenate two 1D arrays into a 2D array by adding a new column.
- Creating a new array from existing arrays: You might want to create a new array by combining elements from multiple existing arrays.
There are many other reasons why you might want to concatenate arrays, and the specific use case will depend on your needs and the data you are working with.
Here is an example of using the concatenate
function to concatenate two 1D arrays along the first axis (row-wise):
import numpy as np
# Create two 1D arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Concatenate the arrays row-wise
c = np.concatenate((a, b))
print(c)
# Output: [1 2 3 4 5 6]
You can also use the concatenate
function to concatenate two 2D arrays along the second axis (column-wise) by specifying the axis
parameter:
import numpy as np
# Create two 2D arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# Concatenate the arrays column-wise
c = np.concatenate((a, b), axis=1)
print(c)
# Output: [[1 2 5 6]
# [3 4 7 8]]
The hstack
function is a convenient function for concatenating arrays horizontally (column-wise):
import numpy as np
# Create two 2D arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# Concatenate the arrays column-wise using hstack
c = np.hstack((a, b))
print(c)
# Output: [[1 2 5 6]
# [3 4 7 8]]
Similarly, the vstack
function is a convenient function for concatenating arrays vertically (row-wise):
import numpy as np
# Create two 2D arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# Concatenate the arrays row-wise using vstack
c = np.vstack((a, b))
print(c)
# Output: [[1 2]
# [3 4]
# [5 6]
# [7 8]]
NumPy Array Splitting
Here are some reasons you may want to split an array.
- Separate data for further analysis: You might want to split an array into smaller arrays so that you can analyze each subset of data separately.
- Divide data into groups: You might want to split an array into smaller arrays based on certain criteria, such as the values of the elements in the array.
- Process data in smaller chunks: You might want to split an array into smaller arrays in order to process the data in smaller chunks, which can be more efficient for certain types of operations.
- Modify only a part of the data: You might want to split an array into smaller arrays so that you can modify only a part of the data, while leaving the rest of the data unchanged.
NumPy provides several functions for splitting arrays, including split
, hsplit
, and vsplit
.
Here is an example of using the split
function to split a 1D array into three equal-sized subarrays:
import numpy as np
# Create a 1D array
a = np.array([1, 2, 3, 4, 5, 6])
# Split the array into three equal-sized subarrays
b = np.split(a, 3)
print(b)
# Output: [array([1, 2]), array([3, 4]), array([5, 6])]
You can also use the split
function to split a 2D array along a specific axis:
import numpy as np
# Create a 2D array
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Split the array along the rows (axis=0)
b = np.split(a, 3, axis=0)
print(b)
# Output: [array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]
# Split the array along the columns (axis=1)
c = np.split(a, 3, axis=1)
print(c)
# Output: [array([[1], [4], [7]]), array([[2], [5], [8]]), array([[3], [6], [9]])]
The hsplit
function is a convenient function for splitting an array horizontally (i.e., along the columns):
import numpy as np
# Create a 2D array
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Split the array into three equal-sized subarrays column-wise using hsplit
b = np.hsplit(a, 3)
print(b)
# Output: [array([[1], [4], [7]]), array([[2], [5], [8]]), array([[3], [6], [9]])]
Finally, the vsplit
function is a convenient function for splitting an array vertically (i.e., along the rows):
import numpy as np
# Create a 2D array
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Split the array into three equal-sized subarrays row-wise using vsplit
b = np.vsplit(a, 3)
print(b)
# Output: [array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]
NumPy Array Resizing
You might want to resize an array for a variety of reasons. For example, you might want to change the size of an array to a new size that is larger or smaller than the original size. You might also want to repeat the elements of an array a certain number of times to create a new array or modify the shape of an array by adding or removing elements. Resizing an array can be useful for a variety of purposes, including data analysis, processing, and visualization. It can also be an efficient way to create a new array from an existing array or to modify the shape of an array to fit a particular requirement or use case.
NumPy provides the resize
function to change the size of an array. Here is an example of using the resize
function to change the size of a 1D array:
import numpy as np
# Create a 1D array
a = np.array([1, 2, 3, 4, 5])
# Resize the array to a new size
b = np.resize(a, (10,))
print(b)
# Output: [1 2 3 4 5 1 2 3 4 5]
In this example, the resize
function changes the size of the a
array from (5,) to (10,). If the new size is larger than the original size, the resize
function will repeat the elements of the array until the new size is reached.
You can also use the resize
function to change the size of a 2D array:
import numpy as np
# Create a 2D array
a = np.array([[1, 2, 3], [4, 5, 6]])
# Resize the array to a new size
b = np.resize(a, (4, 3))
print(b)
# Output: [[1 2 3]
# [4 5 6]
# [1 2 3]
# [4 5 6]]
In this example, the resize
function changes the size of the a
array from (2, 3) to (4, 3). If the new size is larger than the original size, the resize
function will repeat the elements of the array until the new size is reached.
NumPy Array Broadcasting
Broadcasting is a powerful mechanism in NumPy that allows you to perform arithmetic operations on arrays of different shapes. It works by “stretching” or “duplicating” the smaller array so that its shape is compatible with the larger array, and then performing the operation element-wise.
Here is an example of using broadcasting to add a 1D array to a 2D array:
import numpy as np
# Create a 2D array
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
# Output: [[1 2 3]
# [4 5 6]]
# Create a 1D array
b = np.array([10, 20, 30])
print(b)
# Output: [10 20 30]
# Add the 1D array to the 2D array using broadcasting
c = a + b
print(c)
# Output: [[11 22 33]
# [14 25 36]]
In this example, the b
array is “stretched” or “duplicated” so that its shape is compatible with the a
array, and the operation is performed element-wise.
Broadcasting is a very useful technique for performing arithmetic operations on arrays of different shapes, and it can greatly simplify your code and make it more efficient.
NumPy Array Input/Output
NumPy provides several functions for reading and writing arrays from and to files. These functions allow you to save and load arrays to and from various file formats, such as text files and binary files.
Here is an example of using NumPy’s save
and load
functions to save and load a 1D array to and from a binary file:
import numpy as np
# Create a 1D array
a = np.array([1, 2, 3, 4, 5])
# Save the array to a binary file
np.save('my_array', a)
# Load the array from the binary file
b = np.load('my_array.npy')
print(b)
# Output: [1 2 3 4 5]
In this example, the save
function saves the a
array to a binary file called my_array.npy
, and the load
function loads the array from the file and stores it in the b
array.
NumPy also provides the savetxt
and loadtxt
functions for reading and writing arrays to and from text files:
import numpy as np
# Create a 1D array
a = np.array([1, 2, 3, 4, 5])
# Save the array to a text file
np.savetxt('my_array.txt', a, delimiter=',')
# Load the array from the text file
b = np.loadtxt('my_array.txt', delimiter=',')
print(b)
# Output: [1. 2. 3. 4. 5.]
In this example, the savetxt
function saves the a
array to a text file called my_array.txt
, and the loadtxt
function loads the array from the file and stores it in the b
array.
- NumPy Array Manipulation Techniques – vegibit (vegibit.com)
- Array manipulation routines — NumPy v1.24 Manual (numpy.org)
- 4 Techniques to Handle NumPy Arrays – Better (betterprogramming.pub)
- NumPy – Array Manipulation – Online Tutorials Library (www.tutorialspoint.com)
- How can I use numpy for an efficient array manipulation? (stackoverflow.com)
- NumPy Array Manipulation – Towards Data Science (towardsdatascience.com)
- Indexing and Slicing NumPy Arrays: A Complete Guide • datagy (datagy.io)
- The Only 45 Methods You Should Master To Become A NumPy Pro (medium.com)
- Building An One-stop-shop For Numpy – mayurji.github.io (mayurji.github.io)
- Image Processing in NumPy. Open and manipulate images in NumPy (python.plainenglish.io)
- NumPy Cheat Sheet: Data Analysis in Python | DataCamp (www.datacamp.com)
- An introduction to Numpy and Scipy – UCSB College of (sites.engineering.ucsb.edu)
- The Basics of NumPy Arrays – takiegor.github.io (takiegor.github.io)
- NumPy – Array Manipulation | i2tutorials (www.i2tutorials.com)