Introduction To Numpy- Part 5

Numpy is a very extensive library with many attributes and functions to help you work with mathematical calculations and arrays. You need to understand at least the basics of this library if you work in a field involving dealing with data like data science. We’ve created many parts for this library. You can check it out to understand more.

1. Iterating Over Numpy Array

We’ve learned in the previous tutorial how you can iterate over a numpy array and even 2-D arrays but imagine that you will loop over a 5-D array. This means you need to make 5 for loops to loop over the whole array values, and it will be a daunting task. The nice thing is that numpy developers have created a function inside the numpy to fix these issues. Let’s see how:

import numpy as np

# Create an Array
array = np.array([[10, 20, 30, 40], [50, 60, 70, 80]])

# Iterating Over The Numpy Array
for x in np.nditer(array):
    print(x)

You can see that I’ve used the .nditer() function to loop over this 2-D array with only one for loop instead of two as the previous example.

2. Joining Numpy Arrays

Jining two arrays mean concatenating the arrays’ values together in one array. Numpy will join arrays by axes, and let’s see how you can perform this on a one-dimensional array:

import numpy as np

# Create Two Arrays
array = np.array([10, 20, 30, 40])
array2 = np.array([50, 60, 70, 80])

# Joining Two Arrays
array3 = np.concatenate((array, array2))

# Printing The New Array
print(array3)

# Checking If Copy or View
print(array.base)

We’ve used the .concatenate() function to join the two arrays, and you specify the first array to start and the other ones. You can also use the .base attribute to confirm if this newly created entity owns the data or views it. 

It will tell you that this newly created owns the newly created array. We’ve seen how you join the one-dimensional array, but what about the other kind of arrays like 2-dimensional arrays? Let’s see an example of how numpy deal with concatenating 2-dimensional arrays:

import numpy as np

# Create Two 2-D Arrays
array = np.array([[10, 20], [30, 40]])
array2 = np.array([[100, 200], [300, 400]])

# Joining The Two Arrays
array3 = np.concatenate((array, array2), axis=1)

# Printing The New Array
print(array3)

When you run the previous code, you can see that the first list of the first array will be on the same list with the second array and the same for the other lists. The newly created one will also be a 2-dimensional array.

3. The Stack Function

The stack() function is the same as the concatenate() function, where it joins two arrays or more and makes them as one numpy array. But they are different in the way of joining them. Let us see a simple example:

import numpy as np

# Create Two Arrays
array = np.array([10, 20, 30])
array2 = np.array([100, 200, 300])

# Stacking The Arrays
array3 = np.stack((array, array2), axis=1)

# Printing The Array
print(array3)

After running it, you can see from the code that the .stack() function will join the first number of the first list with the first number of the second list. The newly created array will have three lists if the arrays have three elements. There is another function in numpy for stacking the arrays:

import numpy as np

# Create Two Arrays
array = np.array([10, 20, 30])
array2 = np.array([100, 200, 300])

# Stacking The Arrays
array3 = np.hstack((array, array2))

# Printing The Array
print(array3)

The newly created array using the .hstack() function will be a 1-dimensional array, and it will put the values of the first array then followed by the second array. So it will stock them along the rows instead of columns like the previous function. Let’s see another function to stack the arrays along with the columns:

import numpy as np

# Create Two Arrays
array = np.array([10, 20, 30])
array2 = np.array([100, 200, 300])

# Stacking The Arrays
array3 = np.vstack((array, array2))

# Printing The Array
print(array3)

You can see that the .vstack() function will stack the elements along with the columns. It will create a new array, but every array will have its data or values, not like the other ones where it mixes them, so that it will be helpful in many situations.

4. Splitting The Arrays

While joining marks multiple arrays and creates a new one, the splitting will reverse the operation. You can split an array and create many other ones as small arrays. Let’s see an example:

import numpy as np

# Create an Array
array = np.array([10, 20, 30, 40, 50, 60])

# Splitting The Array
array2 = np.array_split(array, 2)

# Printing The Array
print(array2)

We’ve specified in the .array_split() that we need two small arrays from the original array, and it will print out that you can two arrays created from the original one. This newly created one doesn’t own the data, and any changes will affect the original one. Let’s prove this:

import numpy as np

# Create an Array
array = np.array([10, 20, 30, 40, 50, 60])

# Splitting The Array
array2 = np.array_split(array, 2)

# Checking The Data For The Second Sub-Array
print(array2[1].base)

# Change The Array Value
array2[1][1] = 44

# Print The Second Sub-Array
print(array)

You can see it prints the whole original array when we tried to check the data owning for the second sub-array, which means it is viewing, not copying, and does not own the data.

Conclusion

Thanks for reading! Numpy is one of the best libraries to learn and use, and we did many parts for this library, and we couldn’t even cover a small portion of what it is capable of. We will still make many other parts for this unique library and explore more functions.

Introduction To Numpy- Part 6
Introduction To Numpy- Part 4
Scroll to Top