Introduction To Numpy- Part 2

You can say that the numpy library is the core science of data analysis and many fields. The name is an abbreviation of Numerical Python and is used for scientific computing in python. It can perform many basic and advanced operations on arrays. We’ve discussed part 1 of this series, and we will complete diving more into this library.

1. 3-D Arrays

It is always used in deep learning and working with tensors. Working with 3-D arrays is a bit complicated. When you increase the dimensions, it will be much harder to index elements or even for a developer to understand them. Let’s see an example of the 3-D array:

import numpy as np

# Creating The 3-D Array
array = np.array([[[10, 20, 30], [100, 200, 300]], [[1000, 2000, 3000], [10000, 20000, 30000]]])

# Printing The Array
print(array)

When you print the 3-D array, you will see it looks not very easy, and this is what you will work with when you get involved in artificial intelligence. Also, when working with tensors in the Tensorflow library. You can perform indexing to the 3-D array:

import numpy as np

# Creating The 3-D Array
array = np.array([[[10, 20, 30], [100, 200, 300]], [[1000, 2000, 3000], [10000, 20000, 30000]]])

# Printing The Array
print(array[0][1])

The previous code will access the second list of the first array. You can change the indexing numbers to understand more about how it works.

2. Checking The Numpy Array Dimension

Sometimes you will work with arrays that you don’t know their dimension, but you need to perform indexing, for example, so you will have to know its dimension first. Let’s see an example:

import numpy as np

# Creating The 3-D Array
array = np.array([[[10, 20, 30], [100, 200, 300]], [[1000, 2000, 3000], [10000, 20000, 30000]]])

# Printing The Array Dimension
print(array.ndim)

We’ve used the .ndim attribute provided with numpy to know the dimension of this array which will print the value 3 in the screen, meaning it is a 3-D dimension array.

3. Change Array Dimension

For example, you can create a simple array that is a 1-D dimension and change its dimension with a simple argument for any reason. Let’s see an example:

import numpy as np

# Creating an Arra
array = np.array([100, 200, 300], ndmin=7)

# Printing The Array
print(array)

# Print The Array Dimension
print(array.ndim)

You could see from the previous example that we’ve created a 1-D array, but we’ve added an argument called ndmin which will change the dimension of your array. 

We’ve specified 7 dimensions for the previous example, and when you print the array, you will see it has 7 square brackets instead of 1, and the ndim attribute tells you that your array is indeed a 7-D dimension array.

4. Indexing The Array Elements

We’ve learned how to perform indexing in the previous tutorial to give more examples. You can perform operations on the array elements. Let’s see an example:

import numpy as np

# Create a 2-D dimension Array
array = np.array([[10, 20, 30, 40], [100, 200, 300, 400]])

# Access The Array Elements & Perform Multiplication
print(array[1][2] * array[0][2])

The above code will access two numbers from different array lists and perform multiplications on them. Sometimes this kind of logic will be helpful in your program, and I want you to know about it. You can also perform negative indexing. Let’s see an example to understand what is this negative indexing:

import numpy as np

# Create a 2-D dimension Array
array = np.array([[10, 20, 30, 40], [100, 200, 300, 400]])

# Access The Array Elements
print(array[1][-1])

When you execute this code, it will print the number 400, which is the index -1 and the indexing usually start counting from 0 from left to right. The negative indexing starts from -1 because there is no -0 and strat counting from right to left. Try the index number -2 instead of -1, and it will print the 300 value. See this code also:

import numpy as np

# Create a 2-D dimension Array
array = np.array([[10, 20, 30, 40], [100, 200, 300, 400]])

# Access The Array Elements
print(array[-1][-2])

We’ve told python to start from the second list, which is at index -1 with the second element from right to left, and it will print the 300 value.

5. Extracting Multiple Elements

The previous examples of indexing show you how to access elements inside an array, but you only grab one number. But what if you need to extract many numbers and create a new list from these numbers? Well, that’s what’s known as slicing. Let’s see an example:

import numpy as np

# Create an Array
array = np.array([100, 200, 300, 400, 500])

# Extract Part of an Array (Slicing)
print(array[1:4])

It prints a part of that array from the number 200 to the 400 number. We’ve specified in the print statement the section we want to extract, which is [1:4], meaning starting from the second element till the fourth one. You can also do this in slicing:

import numpy as np

# Create an Array
array = np.array([100, 200, 300, 400, 500])

# Extract Part of an Array (Slicing)
print(array[1:])

The previous code will print from the second number to the last one even if the list has too many values because if you didn’t specify a number after the “:” then it will assume you want to access them all.

Conclusion

Thanks for reading! Numpy is an extensive library with many attributes and functions for working with arrays, changing their shapes, etc. You can’t learn many other fields like data science or artificial intelligence without understanding arrays and numpy.

Scroll to Top