A short guide on NUMPY concepts

Sandhiya M
6 min readApr 9, 2022

--

◙ Hey everyone, welcome back all. So in this article we’ll see what a NumPy and shaping and about the dimensional arrays.

◙ NumPy is a library for the Python programming language. NumPy is short for “Numerical Python” and is a popular library that is used in data science. NumPy is used specifically to work with arrays as it provides various functions and support. In this tutorial, we will go over how we can shape our array, so our arrays will know how many dimensions and how many elements it will have. With this introduction out of the way, let’s get into it.

Why We Use NumPy ?

In Python we have lists that serve the purpose of arrays, but they are slow to process. NumPy aims to provide an array object that is up to 50x faster that traditional Python lists.

◘ The array object in NumPy is called nd-array, it provides a lot of supporting functions that make working with nd-array very easy.

◘ Arrays are very frequently used in data science, where speed and resources are very important.

◘ Numpy’s most useful feature is n dimension array object (Nd 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”.

To access NumPy and its functions import it in your Python code like this:

import numpy as np

We shorten the imported name to np for better readability of code using NumPy. This is a widely adopted convention that you should follow so that anyone working with your code can easily understand it.

Moving ahead in python numpy Concepts, let us understand what exactly is a single & multi-dimensional numPy arrays.

Ndarray

How do I start NumPy?

Let us see how it is implemented :

Single-dimensional Numpy Array:

EXAMPLE

Multi-dimensional Array:

EXAMPLE

dtype:
You can find the data type of the elements that are stored in an array. So, if you want to know the data type of a particular element, you can use ‘dtype’ function which will print the datatype along with the size. In the below code, I have defined an array where I have used the same function.

EXAMPLE

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

EXAMPLE

⊛➔ 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 concepts.

reshape:
Reshape is when you change the number of rows and columns which gives a new view to an object. Now, let us take an example to reshape the below array:

➠ As you can see in the above image, we have 3 columns and 2 rows which has converted into 6 columns and 1 rows. Let me show you practically how it’s done.

In this 10 columns and 1 rows has been converted into 5 columns and 2rows.

slicing:
As you can see the ‘reshape’ function has showed its magic. Now, let’s take another operation i.e Slicing. Slicing is basically extracting particular set of elements from an array. This slicing operation is pretty much similar to the one which is there in the list as well.

Consider the following simple example:

We have an array and we need a particular element (say 3) out of a given array.

EXAMPLE-INPUT
OUTPUT

EXPLANATION: Here, the array(1,2,3) is your index 0 and (4,5,6) is index 1 and (7,8,9) is index 2 of the python numpy array. Therefore, we have printed the first element from the second index.

Let’s consider one more example with the same input :

output

EXPLANATION: As you can see in the above code, only 1 and 4 gets printed. Now when I have written 0:2, this does not include the second index of the third row of an array. Therefore, only 1 and 4gets printed else you will get all the elements i.e [1,4, 7].

◈ max/ min & sum/transpose:
Next, we have some more operations in numpy such as to find the minimum, maximum as well the sum of the numpy array. Let’s go ahead in python numpy concepts and execute it practically.

NOTE: The input is same for all the example.

Example for TRANSPOSE:

INPUT
OUTPUT

Example for MIN and MAX :Here it prints minimum & maximum element in an entire array.

OUTPUT

Example for SUM: Here it prints sum of all the elements in an entire array

Let’s see what a concatenate operation does..!

numpy.concatenate() function concatenate a sequence of arrays along an existing axis.

OUTPUT

Conclusion

In this article, I have presented some of my best tips for learning to NUMPY concepts.

★If you really want to become a programmer or add a coding skill to your skills, learning Python is a great place to start. I recommend that you learn the basics with an interactive course before moving on to more difficult concepts. Take notes and make sure to review them regularly and try to practice writing code as often as possible.

★Connect with your colleagues and start helping others when they have problems that can be a great review, and working with someone else’s code is a great way to learn new things.

If you do all of this, nothing can stop you!…Will catch you all with the next blog.

If you liked then appreciate the writing, 👏👏 clap, and share.

You can connect with me on LinkedIn.

--

--