Python NumPy Tutorial – Learn NumPy Arrays With Examples

Last updated on Mar 03,2023 157.3K Views
A technophile with a passion for unraveling the intricate tapestry of the... A technophile with a passion for unraveling the intricate tapestry of the tech world. I've spent over a decade exploring the fascinating world of...

Python NumPy Tutorial – Learn NumPy Arrays With Examples

edureka.co

In my previous blog, you have learned about Arrays in Python and its various fundamentals like functions, lists vs arrays along with its creation. But, those were just the basics and with Python Certification being the most sought-after skill in the programming domain today, there’s obviously so much more to learn. In this Python NumPy tutorial, you will understand each aspect of Numpy in the following sequence:

 So, let’s get started! :-)

What are NumPy Arrays?

NumPy is a Python package that stands for ‘Numerical Python’. It is the core library for scientific computing, which contains a powerful n-dimensional array object.

Where is NumPy used?

Python NumPy arrays provide tools for integrating C, C++, etc. It is also useful in linear algebra, random number capability etc. NumPy array can also be used as an efficient multi-dimensional container for generic data. Now, let me tell you what exactly is a Python NumPy array.

Python NumPy Array: Numpy array is a powerful N-dimensional array object which is in the form of rows and columns. We can initialize NumPy arrays from nested Python lists and access it elements. In order to perform these NumPy operations, the next question which will come in your mind is:

How do I install NumPy?

To install Python NumPy, go to your command prompt and type “pip install numpy”. Once the installation is completed, go to your IDE (For example: PyCharm) and simply import it by typing: “import numpy as np”

Moving ahead in python numpy tutorial, let us understand what exactly is a multi-dimensional numPy array.

Here, I have different elements that are stored in their respective memory locations. It is said to be two dimensional because it has rows as well as columns. In the above image, we have 3 columns and 4 rows available.

How do I start NumPy?

Let us see how it is implemented in PyCharm:

Single-dimensional Numpy Array:

import numpy as np
a=np.array([1,2,3])
print(a)

Output – [1 2 3]

Multi-dimensional Array:

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

O/P – [[ 1 2 3]
[4 5 6]]

Many of you must be wondering that why do we use python NumPy if we already have Python list? So, let us understand with some examples in this python NumPy tutorial.

Python NumPy Array v/s List

Why NumPy is used in Python?

We use python NumPy array instead of a list because of the below three reasons:

  1. Less Memory
  2. Fast
  3. Convenient

The very first reason to choose python NumPy array is that it occupies less memory as compared to list. Then, it is pretty fast in terms of execution and at the same time, it is very convenient to work with NumPy. So these are the major advantages that Python NumPy array has over list. Don’t worry, I am going to prove the above points one by one practically in PyCharm. Consider the below example:


import numpy as np

import time
import sys
S= range(1000)
print(sys.getsizeof(5)*len(S))

D= np.arange(1000)
print(D.size*D.itemsize)

O/P –  14000

4000

The above output shows that the memory allocated by list (denoted by S) is 14000 whereas the memory allocated by the NumPy array is just 4000. From this, you can conclude that there is a major difference between the two and this makes Python NumPy array as the preferred choice over list.

 

Next, let’s talk how python NumPy array is faster and more convenient when compared to list.


import time
import sys

SIZE = 1000000

L1= range(SIZE)
L2= range(SIZE)
A1= np.arange(SIZE)
A2=np.arange(SIZE)

start= time.time()
result=[(x,y) for x,y in zip(L1,L2)]
print((time.time()-start)*1000)

start=time.time()
result= A1+A2
print((time.time()-start)*1000)

O/P – 380.9998035430908
49.99995231628418

 

In the above code, we have defined two lists and two numpy arrays. Then, we have compared the time taken in order to find the sum of lists and sum of numpy arrays both. If you see the output of the above program, there is a significant change in the two values. List took 380ms whereas the numpy array took almost 49ms. Hence, numpy array is faster than list. Now, if you noticed we had run a ‘for’ loop for a list which returns the concatenation of both the lists whereas for numpy arrays, we have just added the two array by simply printing A1+A2. That’s why working with numpy is much easier and convenient when compared to the lists.

Therefore, the above examples proves the point as to why you should go for python numpy array rather than a list!

Moving forward in python numpy tutorial, let’s focus on some of its operations.

You may go through this recording of Python NumPy tutorial where our instructor has explained the topics in a detailed manner with examples that will help you to understand this concept better.

Python NumPy Tutorial | NumPy Array | Python Training | Edureka

Python NumPy Operations

import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(a.ndim)

Output – 2

Since the output is 2, it is a two-dimensional array (multi dimension).

import numpy as np
a = np.array([(1,2,3)])
print(a.itemsize)

Output – 4

So every element occupies 4 byte in the above numpy array.

 

import numpy as np
a = np.array([(1,2,3)])
print(a.dtype)

Output – int32

As you can see, the data type of the array is integer 32 bits. Similarly, you can find the size and shape of the array using ‘size’ and ‘shape’ function respectively.

import numpy as np
a = np.array([(1,2,3,4,5,6)])
print(a.size)
print(a.shape)

Output – 6 (1,6)

Next, let us move forward and see what are the other operations that you can perform with python numpy module. We can also perform reshape as well as slicing operation using python numpy operation. But, what exactly is reshape and slicing? So let me explain this one by one in this python numpy tutorial.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 22nd June,2024

22nd June

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Prompt Engineering Explained