Introduction To Numpy- Part 3

Numpy is the most used library in python for working with arrays and performing mathematical calculations. It is an acronym of Numeric Python and is written in the C language. We’ve learned in the past articles how to work with this library and perform calculations and many other functions. This article will complete the other two articles and learn more functionalities for this module.

1. Extracting Multiple Elements

We’ve learned in the previous tutorial that you can perform slicing gon the numpy arrays, which means extracting a part of the array instead of one value (number), but we didn’t explore all of its options. Let’s see some the examples of doing slicing on the arrays:

import numpy as np

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

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

The previous code will start from the first number at the left till the fourth element of the list. If you didn’t specify the first number before “:” it will assume you want to print all the values at the left. Let’s also apply what we’ve learned before about negative indexing and make it negative slicing:

import numpy as np

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

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

We’ve started slicing from the number in the index -4, which is 30, until the index number -1, which is the first element at the right. Sometimes this kind of slicing is beneficial if you have a long array and you will extract only some numbers of the right part of the array.

2. Extracting Multiple Elements With Steps

Did you know that you can extract multiple elements but with steps? Well, you can do that and specify how many numbers you will jump over it in your slicing. Maybe you didn’t understand but let me show you an example of how you can perform slicing with steps:

import numpy as np

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

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

When you run the code, you will see it prints only three numbers [30 50 70] instead of [30 40 50 60 70], and you guess it. When we’ve specified the number 2 in the slicing at the right, it will ignore the next number and show the other number. So it shows the number 30 but skipped the number 40 and again shows the 50 but skipped the number 60 and finally shows the number 70.

3. Exploring Numpy Data Types

For now, we’ve seen only using numpy with numbers and specifically integers, but it can accept more. Numpy arrays can have also string values, floats, boolean, datetime, objects, and more than I can list here. Let’s try to check the data type of the arrays:

import numpy as np

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

# Checking The Array Type
print(array.dtype)

This code will tell you that the data type of this array is int64 which means integers. Let’s try to create another array with another data type:

import numpy as np

# Create an Array
array = np.array(["Berlin", "London", "Paris"])

# Checking The Array Type
print(array.dtype)

You will get the value “<U6” which means this is a 64 character Unicode, and that’s a standard string in python numpy.

4. Change The Array Data Type

For example, you can change the array type if you want to, like making an integer as a float. Let’s see how you can convert an integer to a float:

import numpy as np

# Create an Array
array = np.array([10, 20, 30, 40], dtype="f")

# Checking The Array Type
print(array.dtype)

You can see now that it tells you this is a float32 and before it was int64 when we’ve used the same value. If you tried, for instance, converting a string to an integer, then you would probably get an error:

import numpy as np

# Create an Array
array = np.array(["Berlin", "London", "Paris"], dtype="i")

# Checking The Array Type
print(array.dtype)

Running the previous code will give you a ValueError telling you that you can convert a string to an integer, which is a number. But you can make the opposite and convert an integer to a string:

import numpy as np

# Create an Array
array = np.array([10, 20, 30, 40], dtype="S")

# Checking The Array Type
print(array.dtype)

# Print The Array
print(array)

The code will work fine, and these numbers will be converted to an integer. You can explore more about the Numpy data type in the official documentation of this library.

5. Copying The Numpy Array

You can copy another array to store the same values on this newly created array, and any change on this new array won’t affect the original one. Let’s see an example:

import numpy as np

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

# Create a Copy
array2 = array.copy()

# Print The Array
print(array2)

You can notice that we’ve used the .copy() function to make a copy of the original one and store its values on the second array. Let’s try to change the second array and what’s will happen to the original one:

import numpy as np

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

# Create a Copy
array2 = array.copy()
array2[1] = 55

# Print The Array
print(array2)
print(array)

You can notice that the second element of the second array is changed to 55 after we change it by assigning a new value to it. The first one never changes because it doesn’t get affected by the second one.

Conclusion

Thanks for reading! Numpy is an excellent library to learn and use when working with arrays and performing mathematical calculations. You will never regret spending a long time learning these skills, primarily if you work in a field dealing with data.

Scroll to Top