Skip to main content

Basics of NumPy arrays

In this post we will cover the basics of numpy arrays. We will also see how to declare, access,view the size , shape and dimension of the numpy arrays.What you will not find is the operations to be performed on or using the arrays.
 Hello Friends,

Today we are going to take a look at the basics of Numpy arrrays. But before proceeding further if you have no detail about what numpy is then no worries, there is a post specifically for this purpose.It introduces you to numpy and gives you a brief idea about what it is.
The link for that post is -->> Introduction to NumPy

Moving on lets get into the details of the arrays.

For using the numpy arrays first step is have numpy. As in you should first import numpy. I am going to show the following code in jupyter notebook which was installed during anaconda installation.NumPy was later installed using the following command: pip install numpy


Now we are done with the initial step.

Next step is to define numpy array.



Good. Here we have our numpy array declared. It is very easy to declare the arrays. With just np.array() we are able to create them.

Next we will take a look at how to access the elements of the array.

As we had previously declared the array, we are now going to access the element at 2nd row 2nd column.

How are we going to access that element???.

If you have any experience with Java, you would say that we can write it in the following way: array_name[rowIndex][columnIndex].
But,but,but there is also a second way to access the elements within the array. (The output of the access operation should be 6.)

Following code snippet specifies the code for the same:

A. Method 1

B. Method 2 

The output of both the methods is same but the syntax is different.

If we want to access only a particular row or a particular column ?

Can that be achieved in NumPy??....
.
.
.
.
Yes, We can do it using the following method.

1.Accessing only the first row.
Following code snippet shows the same:

2.Accessing only the third column.
Following code snippet shows the same:


Using the above 2 methods we can access any elements inside the array.
  
Next we are going to have a look at some other ways to access details about the size,shape and dimension of the NumPy array.

1.We will first see how to get the count of total number of elements within the array.
There is function with following syntax: arrayRef.size

Following code snippet shows the total number of elements in the array declared initially:
There are total 8 elements inside which can be seen from the above exectued code.

2.Next is how can we get the shape of the n dimension array in numpy?.....

We can use the following method.The general syntax is given as follows: arrayRef.shape
Following code snippet shows the same:
From the above result we can see that the array has shape of 2x4.
 
3.Lastly we are going to see how to know the dimensions of the numpy array.

Following is the general syntax to list the dimension of the array: arrayRef.ndim

The code snippet below shows the same:
From the code we can see that the dimension of the array declared above is 2.


Thank you.

That's all for this post!!
Thank you for reading this post.
If you have any suggestions regarding the post contents or if you need some more details on any other topic, please post it in the comments section.
Your suggestions are too valuable so they should not be missed.

Comments

Popular posts from this blog

PyMuPDF vs PDFMiner

 As a developer , I was tasked to extract specific data from a PDF. Upon analysing it further, certain patterns were found based on keywords in the document. Since I was using Python language for the task I found 2 tools quite useful which are PyMuPDF and PDFMiner. These tools can then be used to extract the text from a page on which regular expression can be applied to further extract relevant data.     Next, we are going to take a deeper look into these tools, specifically focusing on the pros and cons of each.     PyMuPDF   Docs , PIP package Pros Simple and understandable API Extensive tools to work with text, images, and graphics Available as a PIP package (pip install PyMuPDF) Better support for a range of symbols comparer to PyPDF2   Cons Parsed text is not in sequence Dependency on other package-Fitz Text sequence information lost during extraction     PDFMiner   Docs ,  PIP package ...

Finding difference between 2 files in Python

In this post, we will take a look at how to compare two files using Python.   I was tasked to compare 2 files and then list the differences between them using Python. Initially, I started with filecmp module, but even with the function parameter ‘ shallow’ set to false, the Boolean result was not enough. Sure, it can act as an indicator to take some action, but it will not list the differences.   I was looking for something more visual, something like color coding and not like the git diff output, which is not very user-friendly. But, another Python internal module, difflib helped me to get the job done.   Inside Difflib, HtmlDiff is what I was looking for. The differences were highlighted with 3 different colors and also the line numbers were indicated in a table to locate the differences. The results are quite self-explanatory and it is easier to explain the differences to other people. Code for generating the above difference table: Note: File1...

Adding existing Anaconda environment to Jupyter notebook

In this post we are going to take a look at adding Anaconda environment to Jupyter notebook. Recently, I was working on a CSV file and wanted to work with Pandas package for tabular data manipulation using Python. The problem was even if I install Pandas package, I would have to install other Data Science package as needed. But, the Anaconda environment was already setup on my laptop, which I want to reuse.   Today, we will look into how to reuse the Anaconda environment within the Jupyter Notebook.   There are 4 basic steps to be followed for adding the environment: 1. Create a conda environment Go to Conda command prompt(Run in Admin mode) Run the following command: conda create –-name newenv O/P:   What if there is an existing conda environment? Go to Conda command prompt(No need for Admin mode) Run the following command: conda env list O/P: Since there was only one environment, only one entry was displayed. ‘*’ indicates the cur...