Numpy Basics — 2. Arrays Operations

Previously, I’ve introduced the concept of n-D arrays and some manipulations we can do with them.
Today, I’ll introduce some new operations to make our lives easier with NumPy.
Merging Arrays
Just like you can merge lists, you can merge arrays, meaning put the content of two or more arrays in a single array.
To join arrays, the most basic way is to use the concatenate function, which allows you to combine multiple arrays along a specified axis. For example, to join two arrays a and b along the rows, you can use the following code:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[7, 8, 9], [10, 11, 12]])
c = np.concatenate((a, b), axis=0)This will create a new array c that contains the rows of a followed by the rows of b. The resulting array will have the shape (4, 3).
Another way to join arrays is to use the stack function, which allows you to stack arrays along a new axis. For example, to stack a and b along the column axis, you can use the following code:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.stack((a, b), axis=1)This will create a new array c that contains the columns of a followed by the columns of b. The resulting array will have the shape (3, 2).
Finally, you can also use the vstack and hstack functions to stack arrays vertically or horizontally, respectively. For example, to stack a and b vertically, you can use the following code:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[7, 8, 9], [10, 11, 12]])
c = np.vstack((a, b))This will create a new array c that contains the rows of a followed by the rows of b. The resulting array will have the shape (4, 3). Similarly, you can use hstack to stack arrays horizontally.
Splitting Arrays
The opposite of merging is splitting, meaning breaking one array into multiple arrays.
One way to split an array is to use the split function, which allows you to divide an array into a specified number of subarrays along a specified axis. For example, to split an array a into three subarrays along the rows, you can use the following code:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
subarrays = np.split(a, 3, axis=0)This will create a list of three subarrays, each containing one or two rows of a.
Another way to split an array is to use the vsplit and hsplit functions, which allow you to split an array vertically or horizontally, respectively. For example, to split a into two subarrays along the columns, you can use the following code:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
subarrays = np.hsplit(a, 2)This will create a list of two subarrays, each containing two columns of a. Similarly, you can use vsplit to split an array vertically.
Finally, you can also use indexing to split an array. For example, to split a into two subarrays along the rows, you can use the following code:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
subarray1 = a[:2, :]
subarray2 = a[2:, :]This will create two subarrays, subarray1 and subarray2, containing the first two rows and the last two rows of a, respectively.
Searching in Arrays
Searching allows you to search for a specific value in arrays and get the indexes that match.
Most of the time, you will use the where function, which returns the indices of elements that meet a specified condition. For example, to find the indices of all elements in an array a that are greater than 5, you can use the following code:
import numpy as np
a = np.array([1, 3, 5, 7, 9, 11])
indices = np.where(a > 5)This will return an array of indices [3, 4, 5].
You can also use the argmax and argmin functions, which return the indices of the maximum and minimum elements, respectively. For example, to find the index of the maximum element in an array a :
import numpy as np
a = np.array([1, 3, 5, 7, 9, 11])
index = np.argmax(a)You can also use the in1d function to search for elements in one array that are also present in another array. For example, here is how to find the elements in array b that are also present in array a :
import numpy as np
a = np.array([1, 3, 5, 7, 9, 11])
b = np.array([5, 7, 9])
indices = np.in1d(b, a)This will return a boolean array [True, True, True], indicating that all elements of b are present in a.
Sorting Arrays
To sort an array with NumPy, you can use the sort function, which sorts the elements of an array in ascending order.
For example, to sort an array a with NumPy, you can use the following code:
import numpy as np
a = np.array([5, 2, 3, 1, 4])
sorted_a = np.sort(a)By default, the sort function sorts the elements of an array in ascending order. However, you can also specify the kind parameter to control the type of sorting algorithm used. For example, you can use a stable sorting algorithm that preserves the relative order of equal elements:
import numpy as np
a = np.array([5, 2, 3, 1, 4])
sorted_a = np.sort(a, kind='stable')You can also use the argsort function to get the indices of the sorted elements, rather than the sorted elements themselves. For example, to get the indices of the sorted elements of a :
import numpy as np
a = np.array([5, 2, 3, 1, 4])
indices = np.argsort(a)This will return an array of indices [3, 1, 2, 4, 0], indicating the positions of the sorted elements in the original array.
I rarely use that, but it can be useful.
Finally, you can also use the sort function to sort an array along a specific axis. For example, to sort the rows of a 2D array a in ascending order.
import numpy as np
a = np.array([[5, 2, 3], [1, 4, 6]])
sorted_a = np.sort(a, axis=1)Filtering Arrays
The last operation I’ll talk about is the filter operation, allowing you to filter arrays using a criteria.
To filter a NumPy array, you can use a boolean index array to select the elements that meet a certain condition.
Here is an example of a boolean index array, if yo don’t know what it is:
import numpy as np
a = np.array([1, 3, 5, 7, 9, 11])
filtered_a = a[a > 5]This will create a new array filtered_a that contains only the elements of a that are greater than 5, i.e. [7, 9, 11].
It looks a bit like things we can do with Pandas DataFrames.
You can also use the where function to create a boolean index array based on a certain condition. Then you can use this index to get a filtered array. For example, to create a array that indicates which elements of a are greater than 5:
import numpy as np
a = np.array([1, 3, 5, 7, 9, 11])
index = np.where(a > 5)
filtered_a = a[index]You can also use the extract function to select elements of an array based on a certain condition. For example, to select the elements of a that are greater than 5, you can use the following code:
import numpy as np
a = np.array([1, 3, 5, 7, 9, 11])
filtered_a = np.extract(a > 5, a)Final Note
You have now some more tools to add to your NumPy toolbox! Here is the recap:
- You can use NumPy to join arrays using functions like
concatenate,stack,vstack, andhstack - You can split arrays using functions like
split,vsplit, andhsplit - You can also search for elements in arrays using functions like
where,argmax,argmin, andin1d, and sort arrays using functions likesort,argsort, andextract. - Finally, you can use boolean indexing and functions like
whereandextractto filter arrays and select elements that meet certain conditions.
To explore more of my Python stories, click here! You can also access all my content by checking this page.
If you liked the story, don’t forget to clap, comment, and maybe follow me if you want to explore more of my content :)
You can also subscribe to me via email to be notified every time I publish a new story, just click here!
If you’re not subscribed to Medium yet and wish to support me or get access to all my stories, you can use my link:






