This is my 5th blog in "Let's Talk Python" and in am very glad to announce that we have covered Numpy, Pandas, and many other modules in python which I shall be discussing soon. This article is going to be entirely dedicated to Numpy.
Numpy is an extension to the python programming language, adding support for large, multidimensional arrays and matrices, along with a large library of high-level mathematical functions to operate on these arrays.
Numpy, python, etc are like those small bones and joints without which a snake as python cannot coil any of its prey. Well, if it can't coil... What's python about it?
Without wasting any more time...let's start this article.
Let's begin with, "What are Multidimensional arrays? And why do we need them?"
Multidimensional arrays are used to store data in a matrix form, or a tabular form. To say, what are the dimensions of your room? The answer is height, Length, and breadth. Simple!
Now, a single array will store values for a single room only. But what if you have a penthouse? Won't it be a shame that despite having so many rooms you can use ...only one? Well, multidimensional arrays are here to rescue. An N-d array can store the dimensions of each room in it. That means now you can access each room of your home wholeheartedly.
They can be used to store the values of the railway schedule, tabular data. Etc.
Consider the following image to know what exactly these multidimensional arrays are.
A 1D array stores values in a row. That means, either height, or breadth, or length of all your rooms.
A 2D array stores values in rows and columns. That means any pair out of height, length, and breadth.
Now, a 3D array stores values in rows, columns, and depths. That means it will have all the three required dimensions stored for all rooms.
How to create a Numpy array in python?
See the image below:
We can simply pass a list through numpy’s inbuilt array function arr=np.array(list).
What are some of the methods used in arrays?
The arr.arange() is a built in function of python that works as a range and returns an ndarray.
The arr.min() prints the minimum value in the array.
The arr.max() prints the maximum value passed in the array.
The arr.all() and arr.any() check for conditions where arr.all() works as “&” and arr.any() works as “|”.
Aside from this, we studied about views and copies.
To save memory, python creates a view of two arrays pointing at the same location. That means, it will show up as two arrays that are a copy of each other but in actual they are a view of the same location.
Deep Copy & Shallow copy
There are two ways we can copy in python. They are shallow and deep.
In a deep copy, a new collection of objects is made. All the objects of the original are copied and put in here. So, any changes done to the copy do not change anything in the original.
In shallow copy, a new collection of objects in made but objects are filled with references to the original. So, any changes done to the copy do change things in the original.
Aside from this, the following arithmetical functions can also be performed here:
These all functions can be performed on arrays.
Numpy provides us with many functions such as average, mean, median, mode, etc. This extension has a huge value for the mathematical understanding of any data. And we all know, maths is accurate.
Numpy and pandas together are required by data analysts for data visualization. So, it becomes necessary to master these two libraries. After this, we paced forward towards pandas that I shall be discussing in my next blog. I hope this blog was worth a while for you. Stay tuned to know what makes pandas so interesting.Let's Talk Python.