Python NumPy Tutorial: Learn Python Numpy With Examples

If you’ve been studying Python for some time, you must’ve come across NumPy. And you must’ve wondered what it is and why it is so important. In this Python Numpy tutorial, you’ll get to learn about the same. You’ll get to understand NumPy as well as NumPy arrays and their functions. 

Having mastery over Python is necessary for modern-day programmers. And this Python NumPy tutorial will help you in understanding Python better. It’s quite detailed, so we recommend adding this page to your bookmarks for future reference. 

What is Python NumPy?

NumPy stands for ‘Numerical Python.’ As you would’ve guessed, it focuses on numerical operations and computing. Numpy is a Python package and is the main library for scientific computations. It has n-dimensional array objects and tools to integrate other dominant languages such as C. You can use the NumPy array as an enormous multi-dimensional container for data. 

What is a NumPy Array?

The NumPy array is a fantastic n-dimensional array object. It has rows and columns, and you can use it to access the elements of a Python list. There are many operations you can perform on a NumPy array. We’ve discussed them later in the article, but before that, you must understand how to install NumPy in your system. Without installing it, you wouldn’t be able to use it. 

How to install NumPy?

You’ll have to go to the command prompt and enter ‘pip install numpy’ to install Python NumPy. After the installation is complete, you’ll have to go to the IDE and import numpy through ‘import numpy as np’. And that’s how you install Numpy on your system.

You can create arrays in NumPy easily through the following code:

1 import numpy as np

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

3 print(a) 

The output of the above code – [1 2 3]

The code above would give you a one-dimensional array. If you want to create a multidimensional array, you’d have to write something similar to the example present below:

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

2 print(a)

The output of the above code – [[ 1 2 3]

[4 5 6]]

Read more: 25 Exciting Python Project Ideas & Topics for Beginners

Operations in NumPy

Python NumPy has many operations. They all perform specific functions. Here are those functions with a brief description:

itemsize:

With the help of this function, you can find out the byte size of the elements of your array. Take a look at the following example:

1 import numpy as np

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

3 print(a.itemsize)

The output of the above code – 4

ndim:

The ndim function helps you find the dimension of the array. You should know that you can have one dimensional, two dimensional, as well as three-dimensional arrays. Here’s an example of this function:

1 import numpy as np

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

3 print(a.ndim)

The output of the above code – 2

reshape:

With the help of the reshape operation, you can change the number of rows and columns present in an array. Suppose the one array has three columns and two rows. Through reshape, you can change them to 2 columns and three rows. See it in action through the following example:

1 import numpy as np

2 a = np.array([(8,9,10),(11,12,13)])

3 print(a)

4 a=a.reshape(3,2)

5 print(a)

Output of the above code – [[ 8 9 10] [11 12 13]] [[ 8 9] [10 11] [12 13]]

slicing:

By using the slicing operation, you can extract a specific set of elements from the required array. In other words, you can ‘slice’ the array and get a portion of the same. Suppose you have an array and want to extract a specific element from it, you’d go about it in the following way:

1 import numpy as np

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

3 print(a[0,2])

The output of the above code – 3

In the example above, the index of the first array was 0, and for the second one, it was 1. So, the code says that it should print the second element of the first array (that has the index 0). Suppose you need the second element from the first and the zeroth index of the array. Then we would use the following code:

1 import numpy as np

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

3 print(a[0:,2])

The output of the above code– [3 5]

Also read: Python Developer Salary in India

dtype:

WIth the dtype function, you have the option of finding the data type of the elements of an array. It gives you the data type and the size of the required component. Take a look at the following example to see how it works:

1 import numpy as np

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

3 print(a.dtype)

The output of the above code – int32

You can use the ‘shape’ and ‘size’ functions to find the shape and size of the array as well. Take a look at this example of our Python NumPy tutorial to understand these functions properly:

1 import numpy as np

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

3 print(a.size)

4 print(a.shape)

The output of the above code – 6 (1,6)

linspace:

With the help of the linspace operation, you can get evenly spaced numbers spread according to your mentioned interval. The linspace function has its uses, and here’s an example of how you can use it:

1 import numpy as np

2 a=np.linspace(1,3,10)

3 print(a)

Output of the above code– [ 1. 1.22222222 1.44444444 1.66666667 1.88888889 2.11111111 2.33333333 2.55555556 2.77777778 3. ]

square root and standard deviation

Python NumPy enables you to perform various mathematical operations. And one of those operations is deriving the square root of the required array. You can also obtain the standard deviation of your NumPy array. Here’s a detailed example to help you in this regard:

1 import numpy as np

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

3 print(np.sqrt(a))

4 print(np.std(a))

The output of the above code– [[ 1. 1.41421356 1.73205081]

[ 1.73205081 2. 2.23606798]]

1.29099444874

max/min

You can find the maximum, minimum, and the sum of an array as well through the specific operations. Finding the maximum and the minimum can help you a lot in performing complex operations. Here is how you can find the maximum, minimum, and the sum of the array you have:

1 import numpy as np

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

3 print(a.min())

4 print(a.max())

5 print(a.sum())

The output of the above code – 1 3 6

Horizontal and vertical stacking

You might want to combine two arrays but not add them, i.e., you might just want to concatenate them. For that purpose, you can either stack them vertically or horizontally. Here is the example code for doing so:

1 import numpy as np

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

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

4 print(np.vstack((x,y)))

5 print(np.hstack((x,y)))

Output of the above code – [[1 2 3] [3 4 5] [1 2 3] [3 4 5]]

[[1 2 3 1 2 3] [3 4 5 3 4 5]]

Read more: Operators in Python: A Beginner’s Guide to Arithmetic

Addition

You can add NumPy arrays as well. Apart from addition, you can also perform subtraction, division, and multiplication of two matrices. Here’s an example of addition in Python NumPy:

1 import numpy as np

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

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

4 print(x+y)

The output of the above code – [[ 2 4 6] [ 6 8 10]]

Like we mentioned earlier, you can perform other mathematical operations on NumPy arrays as well, including subtraction and division. Here’s how:

1 import numpy as np

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

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

4 print(x-y)

5 print(x*y)

6 print(x/y)

Output of the above code– [[0 0 0] [0 0 0]]

[[ 1 4 9] [ 9 16 25]]

[[ 1. 1. 1.] [ 1. 1. 1.]]

ravel

The ravel operation lets you convert a NumPy array into a ravel, which is a single column. Here’s an example:

1 import numpy as np

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

3 print(x.ravel())

The output of the code – [ 1 2 3 3 4 5]

Conclusion

We’re sure that you have found this Python NumPy tutorial quite informative. By now, you’d have understood what Python NumPy is and what its functions are. If you have any more questions about this topic, feel free to let us know. 

If you are curious to learn about data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

Prepare for a Career of the Future

PG DIPLOMA FROM IIIT-B, 100+ HRS OF CLASSROOM LEARNING, 400+ HRS OF ONLINE LEARNING & 360 DEGREES CAREER SUPPORT
Learn More @ UPGRAD

Leave a comment

Your email address will not be published. Required fields are marked *

×
Aspire to be a Data Scientist
Download syllabus & join our Data Science Program and develop practical knowledge & skills.
Download syllabus
By clicking Download syllabus,
you agree to our terms and conditions and our privacy policy.