Introduction To Numpy- Part 4

Numpy is your choice if you will work with arrays in python or apply mathematical calculations to your data. Also, it is the preferred library for data analysts and data science for processing their data and reshaping it, and more. We will complete our series of numpy libraries, and we will start learning about viewing your arrays.

1. Viewing The Numpy Array

We’ve learned in the previous article that copying an array from another array means creating a new one. Any changes to the new array won’t affect the original one, but the term is different for viewing the array. Let’s see an example:

import numpy as np

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

# View The Original Array
array2 = array.view()

# Change The Array Value
array2[3] = 77

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

You can notice that the .view() function has created a new array and stored its values in the array2 variable. Notice also that changing the value in array2 has changed the value in the original array, unlike the .copy() method. 

There is an excellent guy attribute I want you to know about, which is the .base that will tell you if an array owns that data or just viewing it from another array. Let’s see how you can perform this:

import numpy as np

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

# View The Original Array
array2 = array.view()

# Creating A Copy
array3 = array.copy()

# Print The Arrays
print(array2.base)
print(array3.base)

You can see that array2 is viewing the data because the first print statement will show you the array values of this variable. The other one, array3, will show you None, which means it owns the data.

2. The Numpy Array Shape

You can get the numpy array shape by using a built-in function in python known as .shape, which will return the number of values inside the array. Let’s try a simple example, and I will explain more about it:

import numpy as np

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

# Getting The Array Shape
print(array.shape)

The code will print this value (4,) which means this array has only four elements, but there is also another thing you will know from that value. It means this array is a one-dimensional array. Let’s try another example with a multi-dimensional array and see the differences:

import numpy as np

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

# Getting The Array Shape
print(array.shape)

You can see that it print the value (1, 1, 4), which means it is a multi-dimensional array since we’ve also specified the ndmin as 3, and the last number of this result is 4, meaning you have four elements in the last dimension.

3. Reshaping Array

The word reshaping is self-explanatory, and it means changing the shape of your array. The shape of your array means the dimension of it. This process is straightforward, and let’s see an example:

import numpy as np

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

# Reshaping The Array
array2 = array.reshape(2, 3)

# Print The Array
print(array2)

When you execute the previous code, the array will be a 2-D array instead of a 1-D array, and the .reshape() function is used for changing its dimension. The first argument of this is the dimension of your new array, and it is 2, and the other one is the number of elements in every list of your array. But do you know this new shape is viewed or copied? Let’s find out:

import numpy as np

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

# Reshaping The Array
array2 = array.reshape(2, 3)

# Print The Array
print(array2.base)

The code will print out the original one, which means it is viewed, and the new variable doesn’t own the data. Any change to this new array will affect the original one.

4. Iterating Over Array

You already heard of the term iterating when you’ve learned about lists or the for loops, which means looping over the list items and printing them out. The same can be applied to the numpy arrays. Let’s see an example:

import numpy as np

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

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

The code will work fine, and I think most of you understand how it works since you have already learned them before. But what about looping in the 2-D arrays? Let’s see an example:

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 array:
    for y in x:
        print(y)

It will iterate over the numpy arrays and print the total values on the screen. You can notice that we’ve used two for loops to iterate over these two lists inside the array. Also, if you have a 3-dimensional array, you will need four for loops and the same for the other arrays dimensions. 

Conclusion

Thanks for reading! Numpy is still a massive library to learn, even with our many articles so far. However, we are still going to cover many other options, and we are going to make a long series of this library to help you understand more on how to deal with arrays in python.

Introduction To Numpy- Part 5
Introduction To Numpy- Part 3
Scroll to Top