The numpy library is a cornerstone of the data science and numerical computing world in Python. One of its fundamental operations is appending elements or arrays. In this tutorial, we’ll explore the numpy.append() function through five practical examples, ranging from simple to more complex scenarios.
The Basics of numpy.append()
The numpy.append() function is used to concatenate elements or arrays at the end of a given array. Before diving into the examples, let’s quickly understand its syntax:
numpy.append(arr, values, axis=None)
- arr is the array to which values are appended.
- values are appended to the end of arr.
- If axis is specified, arrays are concatenated along the indicated axis. If not, both arr and values are flattened before use.
Example 1: Appending elements to a 1D array
Let’s start with the basics by appending a single element to a one-dimensional array.
import numpy as np
arr = np.array([1, 2, 3])
result = np.append(arr, 4)
print(result)
Output:
[1, 2, 3, 4]
Example 2: Appending multiple elements to a 1D array
Expanding on the first example, we can append multiple elements at once.
import numpy as np
arr = np.array([1, 2, 3])
result = np.append(arr, [4, 5, 6])
print(result)
Output:
[1, 2, 3, 4, 5, 6]
Example 3: Appending an array to another array
Next, we look at appending an entire array to another, demonstrating how the append operation deals with multi-dimensional data when the axis is not specified.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.append(arr1, arr2)
print(result)
Output:
[1, 2, 3, 4, 5, 6]
Example 4: Appending arrays along a specific axis
In this example, we append arrays along a specified axis, showcasing how numpy can concatenate arrays in more structured ways.
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
result = np.append(arr1, arr2, axis=0)
print(result)
Output:
[[1, 2]
[3, 4]
[5, 6]
[7, 8]]
Example 5: Complex appending with mixed dimensions
For our final example, we’ll demonstrate a more complex case of appending wherein dimensions of input arrays differ, and we have to manage these discrepancies mindfully.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([[4, 5], [6, 7], [8, 9]])
result = np.append(arr1, arr2, axis=0)
# This throws an error because arr1 and arr2 have different dimensions
This example illustrates an important caveat: appending along a specified axis requires input arrays to have compatible shapes. Attempting to append arrays of mismatched dimensions without flattening will result in an error.
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)
Conclusion
The numpy.append() function is a versatile tool for adding elements or combining arrays in Python’s numerical computing ecosystem. Through these examples, we’ve seen how it can handle a range of scenarios from simple element additions to more complex multi-dimensional array concatenations. Remember, the key to effectively using numpy.append() lies in understanding the dimensions and shapes of your arrays, particularly when working along a specific axis.