Python

How to Use Python NumPy Array

Many libraries exist in Python to perform different types of tasks. NumPy is one of them. The full form of NumPy is Numerical Python, and it is mainly used for scientific computing. Multi-dimensional array objects can be defined by using this library that is called the Python NumPy array. Different types of functions exist in the NumPy library to create the array. NumPy array can be generated from the python list of numeric data, range of data, and random data. How NumPy array can be created and used to do different operations types have shown in this tutorial.

Advantage of using NumPy Array

NumPy array is better than the Python list for various reasons. Some significant advantages of using NumPy array are given below.

  1. It consumes less memory compared to the python list.
  2. It works faster than the python list for the same amount of data.
  3. It is more suitable to use instead of the python list for some specific tasks.

Pre-requisites

NumPy library is not installed in Python by default. So, you have to install this library before practicing the examples shown in this tutorial. Python 3+ is used in this tutorial. Run the following command from the terminal to install NumPy in python 3.

$ sudo apt-get install python3-numpy

NumPy Array Attributes

NumPy array has many attributes to retrieve different types of information about the array. Some of the useful attributes of this array are described below.

  1. ndarray.ndim – This attribute returns the number of dimensions of the NumPy array named ndarray.
  2. ndarray.shape – This attribute returns the size of each dimension of the NumPy array named ndarray.
  3. ndarray.size – This attribute returns the total number of elements of the NumPy array named ndarray.
  4. ndarray.itemsize – This attribute returns the size of each element of the NumPy array named ndarray.
  5. ndarray.dtype – This attribute returns the data type of elements of the NumPy array named ndarray.
  6. ndarray.nbytes – This attribute returns the total number of bytes consumed by the elements of the NumPy array named ndarray.

Use of NumPy Array

The ways of declaring one-dimensional, two-dimensional, and three-dimensional NumPy array is shown in this part of the tutorial.

Example-1: Use of one-dimensional NumPy array

The following example shows three ways of creating a one-dimensional NumPy array. array() function has been used to create the first one-dimensional array of 10 integer numbers. arrange() function has been used to create the second one-dimensional array of 10 sequential numbers. rand() function has been used to create the third one-dimensional array of 10 random float numbers. Next, the print() function has used to print the different attributes and three arrays’ values.

# Import NumPy

import numpy as np

# Declare NumPy array in three different array

oneArray1 = np.array([7, 3, 19, 6, 3, 1, 12, 8, 11, 5])

oneArray2 = np.arange(10)

oneArray3 = np.random.rand(10)

# Print different attributes of three NumPy arrays

print("\nThe dimension of the the first NumPy array is:", oneArray1.ndim)

print("The size of the second NumPy array is:", oneArray2.size)

print("The data type of the third NumPy array is:", oneArray3.dtype)

# Print the values of the three NumPy array

print("\nThe values of the first array are:\n", oneArray1)

print("The values of the second array are:\n", oneArray2)

print("The values of the third array are:\n", oneArray3)

Output:

The following output will appear after executing the above script. The output shows that the first array is 1, the size of the second array is 10, and the data type of the third array is float64. Three arrays have been printed later.

Example-2: Use of two-dimensional NumPy array

The following example shows two ways of creating a two-dimensional NumPy array. array() function has been used to create a two-dimensional array of 2 rows and 3 columns with integer data. rand() function has been used to create a two-dimensional array of 2 rows and 4 columns with float data. Next, the print() function has used to print the size attribute and both arrays’ values.

# Import NumPy

import numpy as np

# Declare two-dimensional array using lists

twoArray1 = np.array([[12, 2, 27], [40, 15, 6]])

# Declare two-dimensional array using random values

twoArray2 = np.random.rand(2, 4)

# Print the size of both arrays

print("The size of the first array:", twoArray1.size)

print("The size of the second array:", twoArray2.size)

# Print the values of both arrays

print("The values of the first array are:\n", twoArray1)

print("The values of the second array are:\n", twoArray2)

Output:

The following output will appear after executing the above script. The output shows that the first array’s size is 6 (2×3), and the size of the second array is 8 (2×4). Both arrays have been printed later.

Example-3: Use of three-dimensional NumPy array

The following example shows two ways of creating a three-dimensional NumPy array. array() function has been used to create a three-dimensional array of integer data. rand() function has been used to create a three-dimensional array of float data. Next, the print() function has used to print the dimension and the values of both arrays.

# Import NumPy

import numpy as np

# Create a three-dimensional array using the list

threeArray1 = np.array([[[3, 6, 7], [7, 5, 9], [8, 5, 2]]])

# Create a three-dimensional array using random values

threeArray2 = np.random.rand(2, 4, 3)

# Print the dimension of both arrays

print("The dimension of the first array:", threeArray1.ndim)

print("The dimension of the second array:", threeArray2.ndim)

# Print the values of both arrays

print("The values of the first array are:\n", threeArray1)

print("The values of the second array are:\n", threeArray2)

Output:

The following output will appear after executing the above script. The output shows that the dimension of both arrays is 3. Both arrays have been printed later.

Conclusion

Creating different types of NumPy arrays has been explained in this tutorial by using multiple examples. I hope the readers will be able to create NumPy arrays after practicing the examples of this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.