Mastering NumPy: Part 2 (Array Manipulation)
This is the 3rd blog post in our comprehensive series (Access all posts of this series here) on NumPy!
Note: Don’t forget to bookmark the full 50 examples series — Mastering NumPy: A 5 Parts Series to Master Numpy
Introduction
Once you have created arrays, the next step is to manipulate and transform them to suit your specific needs. NumPy offers an extensive set of functions for array manipulation, including reshaping, slicing, concatenating, and more. In this blog post, we will explore ten practical examples of array manipulation using NumPy, showcasing the power and flexibility of these operations.
1. Accessing specific elements in an array using indexing and slicing
import numpy as np
# Create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5])
# Accessing individual elements
print("Element at index 0:", arr[0])
print("Element at index 3:", arr[3])
# Slicing the array
print("Elements from index 1 to 3:", arr[1:4])
print("Elements from index 2 to the end:", arr[2:])
print("Elements from the start to index 3:", arr[:4])
print("Every second element:", arr[::2])
# Create a 2-dimensional array
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Accessing individual elements in a 2D array
print("Element at row 1, column 2:", arr2d[1, 2])
print("Element at row 0, column 1:", arr2d[0, 1])
# Slicing the 2D array
print("Row 1:", arr2d[1])
print("Column 2:", arr2d[:, 2])
print("Subarray from row 0 to 1, and column 1 to 2:")
print(arr2d[:2, 1:3])
In this example, we create a NumPy array arr
with elements [1, 2, 3, 4, 5]. We then access a specific element, next we use slicing to access a range of elements.
2nd part of the program shows slicing and accesing of array elements of a 2D array.
2. Reshaping an array using numpy.reshape()
In NumPy, the reshape()
function allows you to change the shape of an array without modifying its data. It returns a new array with the specified shape. The elements in the original array are used in the same order to fill the new array.
The syntax for reshape()
is as follows:
numpy.reshape(array, new_shape, order='C')
Here, array
is the original array that you want to reshape, new_shape
is a tuple specifying the new shape, and order
is an optional parameter that determines the order in which the elements are read from the original array. The default value is 'C', which means row-major order.
Let’s look at an example to illustrate how to use reshape()
:
import numpy as np
# Create a 1-dimensional array with 12 elements
original_array = np.arange(12)
print("Original array:")
print(original_array)
# [ 0 1 2 3 4 5 6 7 8 9 10 11]
# Reshape the array to a 3x4 matrix
reshaped_array = np.reshape(original_array, (3, 4))
print("\nReshaped array:")
print(reshaped_array)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
In this example, the original array has 12 elements, and we reshape it into a 3x4 matrix using reshape()
. The resulting reshaped array has the elements filled row by row from the original array.
Note that the total number of elements in the original array must match the total number of elements in the new shape. Otherwise, a ValueError
will be raised.
3. Flattening a multidimensional array using numpy.flatten().
In NumPy, the flatten()
function is used to transform a multidimensional array into a 1D array. It returns a new array containing the elements of the original array in a flattened sequence. The order in which the elements are flattened is row-major (C-style).
Here’s an example of how to use flatten()
in NumPy:
import numpy as np
# Create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Flatten the array
flattened_arr = arr.flatten()
# Print the flattened array
print(flattened_arr)
# [1 2 3 4 5 6]
As you can see, the flatten()
function has transformed the 2D array arr
into a 1D array flattened_arr
. The elements of flattened_arr
are in the same order as they appear in arr
, but in a flattened sequence.
It’s important to note that flatten()
always returns a copy of the original array. If you want to flatten the array in-place (modify the original array itself), you can use the ravel()
function instead:
arr = np.array([[1, 2, 3], [4, 5, 6]])
arr = arr.ravel()
Both flatten()
and ravel()
achieve the same result of flattening the array, but ravel()
returns a view whenever possible, which means it might share data with the original array instead of creating a copy.
4. Transposing an array using numpy.transpose()
In NumPy, you can use the numpy.transpose()
function to transpose an array, which swaps the dimensions of the array. The numpy.transpose()
function takes the array as the first argument and an optional axes
parameter that specifies the new order of dimensions.
Here’s an example of how to use numpy.transpose()
to transpose an array:
import numpy as np
# Create a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Transpose the array
transposed_arr = np.transpose(arr)
# Print the original and transposed arrays
print("Original Array:")
print(arr)
print("\nTransposed Array:")
print(transposed_arr)
# Output
# Original Array:
# [[1 2 3]
# [4 5 6]]
# Transposed Array:
# [[1 4]
# [2 5]
# [3 6]]
In this example, the original array has dimensions (2, 3). After transposing, the dimensions become (3, 2), swapping the rows and columns.
5. Concatenating two arrays horizontally using numpy.hstack()
horizontally, which means stacking them side by side. The numpy.hstack()
function takes a sequence of arrays as arguments and returns a single array formed by stacking the arrays horizontally.
Here’s an example of how to use numpy.hstack()
to concatenate two arrays horizontally:
import numpy as np
# Create two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Concatenate the arrays horizontally
concatenated_arr = np.hstack((arr1, arr2))
# Print the concatenated array
print("Concatenated Array:")
print(concatenated_arr)
# Concatenated Array:
# [1 2 3 4 5 6]
In this example, the arrays arr1
and arr2
are both 1-dimensional. By using numpy.hstack()
, they are concatenated horizontally, resulting in a single 1-dimensional array [1, 2, 3, 4, 5, 6]
.
6. Concatenating two arrays vertically using numpy.vstack()
In NumPy, you can use the numpy.vstack()
function to concatenate two arrays vertically, which means stacking them on top of each other. The numpy.vstack()
function takes a sequence of arrays as arguments and returns a single array formed by stacking the arrays vertically.
Here’s an example of how to use numpy.vstack()
to concatenate two arrays vertically:
import numpy as np
# Create two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Concatenate the arrays vertically
concatenated_arr = np.vstack((arr1, arr2))
# Print the concatenated array
print("Concatenated Array:")
print(concatenated_arr)
#Concatenated Array:
#[[1 2 3]
# [4 5 6]]
In this example, the arrays arr1
and arr2
are both 1-dimensional. By using numpy.vstack()
, they are concatenated vertically, resulting in a 2-dimensional array [[1, 2, 3], [4, 5, 6]]
. The first array arr1
becomes the first row, and the second array arr2
becomes the second row of the concatenated array.
7. Splitting an array into multiple subarrays using numpy.split().
To split an array into multiple subarrays using numpy.split()
, you need to specify the array you want to split and the indices where you want the split to occur. The numpy.split()
function returns a list of subarrays.
Here’s the syntax for using numpy.split()
:
numpy.split(array, indices_or_sections, axis=0)
array
: The input array you want to split.indices_or_sections
: It can be an integer, indicating the number of equally-sized subarrays to create, or a 1-D array of indices where the splits should occur.axis
(optional): The axis along which to split the array. By default, it is 0, indicating the split is done vertically.
Let’s see some examples to illustrate the usage:
import numpy as np
# Example 1: Splitting into equally-sized subarrays
arr1 = np.array([1, 2, 3, 4, 5, 6])
subarrays1 = np.split(arr1, 3)
print(subarrays1)
# Output: [array([1, 2]), array([3, 4]), array([5, 6])]
# Example 2: Splitting at specified indices
arr2 = np.array([1, 2, 3, 4, 5, 6])
subarrays2 = np.split(arr2, [2, 4])
print(subarrays2)
# Output: [array([1, 2]), array([3, 4]), array([5, 6])]
# Example 3: Splitting a 2D array along axis 1
arr3 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
subarrays3 = np.split(arr3, 3, axis=1)
print(subarrays3)
# Output: [array([[1], [4], [7]]), array([[2], [5], [8]]), array([[3], [6], [9]])]
In the first example, we split the array arr1
into 3 equally-sized subarrays. In the second example, we split the array arr2
at indices 2 and 4. In the third example, we split the 2D array arr3
along axis 1, resulting in three subarrays.
8. Repeating elements within an array using numpy.repeat().
To repeat elements within an array using numpy.repeat()
, you can specify the array and the number of repetitions for each element. The numpy.repeat()
function returns a new array with repeated elements.
Here’s the syntax for using numpy.repeat()
:
numpy.repeat(array, repeats, axis=None)
array
: The input array you want to repeat elements from.repeats
: It can be an integer, indicating the number of repetitions for each element, or a 1-D array specifying the number of repetitions for each element individually.axis
(optional): The axis along which to repeat the elements. By default, it is None, indicating the array is flattened before repeating.
Let’s see some examples to illustrate the usage:
import numpy as np
# Example 1: Repeating each element a fixed number of times
arr1 = np.array([1, 2, 3])
repeated1 = np.repeat(arr1, 2)
print(repeated1)
# Output: [1 1 2 2 3 3]
# Example 2: Repeating each element a different number of times
arr2 = np.array([1, 2, 3])
repeats2 = np.array([3, 2, 4])
repeated2 = np.repeat(arr2, repeats2)
print(repeated2)
# Output: [1 1 1 2 2 3 3 3 3]
# Example 3: Repeating along a specified axis in a 2D array
arr3 = np.array([[1, 2], [3, 4]])
repeated3 = np.repeat(arr3, 2, axis=1)
print(repeated3)
# Output: [[1 1 2 2]
# [3 3 4 4]]
In the first example, we repeat each element of arr1
twice. In the second example, we repeat each element of arr2
a different number of times specified by the repeats2
array. In the third example, we repeat the elements of the 2D array arr3
twice along axis 1, resulting in a new array with repeated elements.
9. Removing duplicate elements from an array using numpy.unique()
In NumPy, you can use the numpy.unique()
function to remove duplicate elements from an array. The numpy.unique()
function returns a sorted, unique copy of an array.
Here’s an example of how to use numpy.unique()
to remove duplicates from an array:
import numpy as np
# Example array with duplicate elements
arr = np.array([1, 2, 3, 2, 4, 1, 3, 5])
# Removing duplicates using numpy.unique()
unique_arr = np.unique(arr)
print(unique_arr)
# output: [1 2 3 4 5]
In the above example, the array arr
contains duplicate elements. By calling np.unique(arr)
, we obtain a new array unique_arr
that only contains the unique elements from arr
.
Note that numpy.unique()
returns the sorted unique elements by default. If you want to preserve the original order of the elements, you can pass return_index=True
and use the returned indices to obtain the unique elements in the original order:
import numpy as np
# Example array with duplicate elements
arr = np.array([1, 2, 3, 2, 4, 1, 3, 5])
# Removing duplicates while preserving the order
unique_elements, indices = np.unique(arr, return_index=True)
unique_arr = arr[np.sort(indices)]
print(unique_arr)
# [1 2 3 4 5]
In this case, unique_elements
contains the unique elements, and indices
contains the indices of the first occurrences of those unique elements in the original array. By sorting the indices and indexing the original array arr
, we obtain unique_arr
with the original order of the unique elements.
10. Shuffling the elements within an array using numpy.random.shuffle().
In NumPy, you can use the numpy.random.shuffle()
function to shuffle the elements within an array. The numpy.random.shuffle()
function randomly rearranges the elements of the input array along the first axis.
Here’s an example of how to use numpy.random.shuffle()
to shuffle the elements within an array:
import numpy as np
# Example array
arr = np.array([1, 2, 3, 4, 5])
# Shuffling the array using numpy.random.shuffle()
np.random.shuffle(arr)
print(arr)
# [3 2 4 1 5]
In the above example, the array arr
contains elements [1, 2, 3, 4, 5]
. By calling np.random.shuffle(arr)
, the elements of arr
are randomly rearranged along the first axis. As a result, arr
is shuffled, and the output may vary each time you run the code.
Note that numpy.random.shuffle()
shuffles the array in-place, meaning it modifies the original array directly. If you want to create a shuffled copy of the array without modifying the original array, you can use the numpy.random.permutation()
function instead:
import numpy as np
# Example array
arr = np.array([1, 2, 3, 4, 5])
# Creating a shuffled copy using numpy.random.permutation()
shuffled_arr = np.random.permutation(arr)
print(shuffled_arr)
# [4 2 1 5 3]
In this case, numpy.random.permutation(arr)
creates a shuffled copy of the array arr
, leaving the original array arr
unchanged.
Conclusion
In this blog post, we explored the array manipulation capabilities of NumPy, enabling us to reshape, slice, concatenate, and split arrays as per our requirements. These manipulation techniques are crucial for organizing and transforming data, allowing you to extract meaningful insights and patterns efficiently.
Next Blog Post
In the next blog post Mastering NumPy: Part 3 (Mathematical Operations) , we will dive into the world of mathematical operations with NumPy. We will cover basic arithmetic operations, statistical calculations, trigonometric functions, and more, demonstrating the powerful numerical computing capabilities of NumPy.
Note: Don’t forget to bookmark the full 50 Numpy examples series — Mastering NumPy: A 5 Parts Series to Master Numpy