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.
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:
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
Post a Comment