Overview of Python Numpy

9 0 0
                                    

In this article, I am going to discuss what is a NumPy in Python? Why is the benefit of using NumPy arrays in Python? And what are the operations which exist in NumPy? Before digging in let's have a quick outline of the list of the content topics that will be covered in this article.

Contents

What is NumPy?

What is a NumPy Array?

Python NumPy Arrays and List

Python NumPy Operations

To learn more about python, you can get the training course available online at Python Certification.

What is Numpy?

Numpy is a python package which stands for 'Numerical Python'. It is the core library for scientific computing which has an n-dimensional array. It is helpful in linear algebra, random number capability etc.

What is a NumPy Array?

Numpy object is an n-dimensional array that exists in the form of rows and columns. We can initialize NumPy arrays from nested Python lists and access its elements.

We can install the NumPy package from the command prompt terminal by using the following command "pip install numpy". After installation type the statement "import numpy as np" on your IDE like PyCharm.

Single-dimensional Numpy Array

import numpy as np

a=np.array([1,2,3])

print(a)

Output:

[1 2 3]

Code Description:

The statement "import numpy" will first import the functionalities of the package numpy and "as np", here "np" will be used as an alias name for the numpy package that will be used in the program code to implement its functionalities. If you are declaring the statement by using the method as "np.array([1,2,3])", then it means you are converting these elements into an array. To store this array you need a variable and an assignment operator. Now if you are using "a=np.array([1,2,3])",it means you are storing the array elements into a variable "a". Next, you will be using a statement "print(a)" which displays the elements in the python terminal window as the output of this code execution. The output displays an array "[1 2 3]".

The above example is a single-dimensional array having the set of elements as 1, 2 and 3. Similarly, we can also use multi-dimensional arrays as shown below with two sets of elements as (1,2,3) and (4,5,6). The below image is an example for storing the elements in a multidimensional array.

Multi-dimensional array

The multi-dimensional array uses the set of elements instead of using a single dimension.

import numpy as np

a=np.array([(1,2,3),(4,5,6)])

print(a)

Output:

[[1 2 3]

[4 5 6]]

Code Description:

In this program, we are using the brackets "( , , )" for separating the elements into sets. You can use as many sets in an array by using a comma "," in between the two sets of elements in an array. The program code is similar to the previous code, the only difference is in using the sets in an array. Execution of this code will now display the set of elements as [1 2 3] and [4 5 6] in the python terminal.

Overview of Python NumPyWhere stories live. Discover now